adding summary data to all plots in a facet (lattice) plot

40 views
Skip to first unread message

Emilio Xavier Esposito

unread,
Apr 26, 2015, 9:05:55 PM4/26/15
to ggp...@googlegroups.com
Hi
Using the data below and the provided ggplot2 commands a collection of facet plots was created; see below. I would like to add the IQRs and median, or other summary statistics of the complete dataset, to each plot. I have tried the stat_summary function, see below, but the error, "Error: geom_line requires the following missing aesthetics: y" is returned. 

I am open to other options within ggplot2 to 'overlay' or 'add' various summaries to each of the lattice plots. Please let me know if additional information is needed.

Thank you for your time and help
Emilio


## data for individual plots
data.melt <- structure(list(entry = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("ent1", 
"ent2", "ent3", "ent4", "ent5", "ent6", "ent7", "ent8", "ent9", 
"ent10"), class = "factor"), ratio = c(0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 
0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 
0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 
0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.7, 0.7, 0.7, 
0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 
0.8, 0.8, 0.8, 0.8, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 
0.9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), count = c(0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 4, 0, 0, 4, 0, 0, 0, 0, 4, 4, 21, 4, 4, 10, 9, 4, 
9, 4, 10, 9, 22, 11, 4, 17, 13, 4, 12, 6, 17, 13, 22, 20, 14, 
24, 26, 14, 24, 16, 24, 25, 27, 29, 18, 27, 26, 18, 27, 17, 27, 
28, 27, 30, 21, 29, 28, 23, 30, 25, 29, 28, 28, 30, 29, 29, 30, 
30, 30, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30)), .Names = c("entry", 
"ratio", "count"), row.names = c(NA, -110L), class = "data.frame")


## summary data to be plotted on all plots
summary.melt <- structure(list(entry = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("IQ1", "median", 
"IQ3"), class = "factor"), ratio = c(0, 0, 0, 0.1, 0.1, 0.1, 
0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.6, 
0.6, 0.6, 0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.9, 0.9, 0.9, 1, 1, 
1), count = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 9, 10, 6, 
12.5, 17, 16, 23, 24, 18, 27, 27, 25, 28, 29, 29, 29, 30, 30, 
30, 30)), .Names = c("entry", "ratio", "count"), row.names = c(NA, 
-33L), class = "data.frame")


## the BASE lattice plots 
plots <- ggplot(data=data.melt, aes(x=ratio, y=count))
plots <- plots + geom_point()
plots <- plots + facet_wrap(~entry, ncol=5)
plots

## add the IQR1, median, and IQR3 plots
plots <- ggplot(data=data.melt, aes(x=ratio, y=count))
plots <- plots + geom_point()
plots <- plots + facet_wrap(~entry, ncol=5)
plots <- plots + geom_line(data=summary.melt, aes(x=ratio, y=count))
plots

returns the 10 plots in the lattice PLUS individual IQR1, median, and IQR3 plots.


plots <- ggplot(data=data.melt, aes(x=ratio, y=count))
plots <- plots + geom_point()
plots <- plots + facet_wrap(~entry, ncol=5)
plots <- plots + stat_summary(fun.data="mean", colour="red", geom="line", width=0.2)
plots

returns the error, "Error: geom_line requires the following missing aesthetics: y"





Emilio Xavier Esposito

unread,
Apr 30, 2015, 11:42:02 AM4/30/15
to ggp...@googlegroups.com
Hi
This stackoverflow post provided instructions on creating the plots of interest:

I used a separate "summary" data.frame for the IQR1, median, and IQR3 lines. It was constructed by hand to include the x-axis values along with the summary data as columns. The summary data.frame was NOT melted.

plots <- ggplot(data=data.melt, aes(x=ratio, y=count))
plots <- plots + geom_line(data=summary, aes(x=x, y=IQR1), colour="#1b9e77")    ## green
plots <- plots + geom_line(data=summary, aes(x=x, y=median), colour="#d95f02")  ## orange
plots <- plots + geom_line(data=summary, aes(x=x, y=IQR3), colour="#7570b3")    ## purple
plots <- plots + geom_point()
plots <- plots + facet_wrap(~entry, ncol=5)
plots

Emilio
Reply all
Reply to author
Forward
0 new messages