Iterate with a for loop a conditional mapping (e.g. aes(colour>1.5)) with ggplot2

406 views
Skip to first unread message

JOSE BARTOLOMEI

unread,
Jun 12, 2016, 12:43:45 PM6/12/16
to ggplot2
 Dear R users,

# I need to iterate a conditional mapping (e.g. aes(colour>1.5)) with ggplot2.
# I'll describe my case below.
 
# Let say I have a longitudinal data set with i=22 individuals;  j=10 time period; 2 outcome measures (ma and mb); 
# a factor variable with two values (Code) and thresholds for the outcome measures (e.g., 4.5 and 5).

df <- data.frame(id = sort(rep(1:22, 10)),
time = rep(1:10, 22),
ma = rep(round(rnorm(1:10, mean = 5), 22), 2),
mb = rep(round(rnorm(1:10, mean = 5), 22), 2),
Code = rep(c(1, 2), 110)
)

# The below procedure produce the desired plot.

library(ggplot2)
# plot 1
ggplot(data = df, aes(x = time, y = m.a, colour = m.a > 5)) +
geom_point() +
facet_wrap(~Code) +
scale_colour_manual(name = 'FVC > 4.77', values = setNames(c('red','green'),c(T, F))) +
xlab("Measurement Day") 

# Nevertheless when I am going to iterate I can not find the adequate place 
# of the conditional value, colour = m.a >5.  An example bellow:

# plot 2

nV <- names(df[3:4])

indx <- c(5, 4.5)
library(ggplot2)

for(i in 1:length(nV)){
nam <- paste("plot", nV, sep = "_")
assign(nam[i], ggplot(data = spiro, aes(x = time)) +
geom_point(aes_string(y = nV[i], colour = nV[i] > indx[i]))  +
scale_colour_manual(name = paste(nV[i], ">", indx[i], sep = " "),
values = setNames(c('red','green'), c(T, F))) +
xlab("Measurement Day")
}
plot_ma
plot_mb

# I understand that the > indx[i] can not go within the aes_string() but I can not find 
# the adequate place or function to make this work.

# When I treat > indx[i] as continuous variable I can produced more or less the result I want,
# as the following plot 3:

# plot 3
# Rangees
uprg <- apply(df[, c("ma", "mb")], 2,
function(x) range(x, na.rm = TRUE))[2,] # substract the 20%

lorg <- apply(df[, c("ma", "mb")], 2,
function(x) range(x, na.rm = TRUE))[1,] 

for(i in 1:length(nV)){
nam <- paste("plot", nV, sep = "_")
assign(nam[i], ggplot(data = df, aes(x = time)) +
geom_point(aes_string(y = nV[i], colour = nV[i]))  +
scale_colour_continuous(limits = c(lorg[i], uprg[i]), low = "green", high = "red") +
xlab("Measurement Day") +
facet_wrap(~Code)
}

plot_ma
plot_mb


# Nevertheless, I want only two colors. One above (red) and one bellow (green) the threshold.
# Any guidance is appreciated.

Thanks,
Jose Bartolomei

Hadley Wickham

unread,
Jun 13, 2016, 11:38:10 AM6/13/16
to JOSE BARTOLOMEI, ggplot2
I'd recommend reading http://rpubs.com/hadley/97970

You'll need to read "Indirectly referring to variables" and then something like:

val <- 1.5
substitute(x < val, list(val = val))


Hadley
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://hadley.nz
Reply all
Reply to author
Forward
0 new messages