Skip to content

How does one set up logical functions?

5 messages · Neotropical bat risk assessments, Rui Barradas, jim holtman +1 more

#
Hello,

See if the following is what you want.


dat <-
structure(list(DF = c("4/3/13 19:20", "4/4/13 21:03", "4/6/13 17:40",
"4/6/13 17:40", "4/6/13 22:48", "4/6/13 22:48", "4/7/13 5:32",
"4/7/13 5:32"), BG = c(105L, 74L, 81L, 82L, 106L, 102L, 87L,
103L), test_strip = c("Aviva-491350", "Aviva-491350", "Aviva-491640",
"Aviva-491350", "Aviva-491640", "Aviva-491350", "Aviva-491350",
"Aviva-491640")), .Names = c("DF", "BG", "test_strip"), class = 
"data.frame", row.names = c(NA,
-8L))

idx <- dat$DF < 100
HighLimit <- LowLimit <- numeric(nrow(dat))
HighLimit[idx] <- dat$BG[idx] + 15
LowLimit[idx] <- dat$BG[idx] - 15
HighLimit[!idx] <- dat$BG[!idx] + dat$BG[!idx]*0.15
LowLimit[!idx] <- dat$BG[!idx] - dat$BG[!idx]*0.15

x <- as.POSIXct(dat$DF, format = "%m/%d/%y %H:%M")
yl <- range(c(dat$BG, HighLimit, LowLimit))
plot(x, dat$BG, ylim = yl, type = "b")
lines(x, HighLimit)
lines(x, LowLimit)


Hope this helps,

Rui Barradas

Em 07-05-2013 15:02, Neotropical bat risk assessments escreveu:
#
On Tue, May 7, 2013 at 10:02 AM, Neotropical bat risk assessments
<neotropical.bats at gmail.com> wrote:
Here it is using gglot2:


library(ggplot2)
library(gridExtra)

DF2 <- transform(DF,
datetime = as.POSIXct(DF2[[1]], format = "%m/%d/%y %H:%M"),
lower = ifelse(BG < 100, BG - 15, BG * 0.85),
upper = ifelse(BG < 100, BG + 15, BG * 1.15))


ggplot(DF2, aes(datetime, BG)) +
   geom_point() +
   geom_line() +
   geom_smooth(aes(ymin = lower, ymax = upper), stat = "identity") +
   geom_linerange(aes(ymin = lower, ymax = upper)) +
   annotation_custom(tableGrob(DF2, gp = gpar(cex = 0.5)), ymin = 120) +
   coord_cartesian(ylim = c(60, 150)) +
   xlab("") +
   ylab("Blood Glucose") +
   ggtitle("Blood Glucose Levels")


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
#
On Tue, May 7, 2013 at 11:06 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
Here it is again with some fixes and also reading the input data so
its all self contained:

library(ggplot2)
library(gridExtra)

Lines <- "date time BG test_strip
4/3/13 19:20  105     Aviva-491350
4/4/13 21:03   74      Aviva-491350
4/6/13 17:40   81      Aviva-491640
4/6/13 17:40   82      Aviva-491350
4/6/13 22:48   106     Aviva-491640
4/6/13 22:48   102     Aviva-491350
4/7/13 5:32    87      Aviva-491350
4/7/13 5:32    103     Aviva-491640"

DF <- read.table(text = Lines, header = TRUE)

DF2 <- transform(DF,
   datetime = as.POSIXct(paste(date, time), format = "%m/%d/%y %H:%M"),
   lower = ifelse(BG < 100, BG - 15, BG * 0.85),
   upper = ifelse(BG < 100, BG + 15, BG * 1.15))

ggplot(DF2, aes(datetime, BG)) +
   geom_point() +
   geom_line() +
   geom_smooth(aes(ymin = lower, ymax = upper), stat = "identity") +
   geom_linerange(aes(ymin = lower, ymax = upper)) +
   annotation_custom(tableGrob(DF2, gp = gpar(cex = 0.5)), ymin = 120) +
   coord_cartesian(ylim = c(60, 150)) +
   xlab("") +
   ylab("Blood Glucose") +
   ggtitle("Blood Glucose Levels")



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com