[R] Achieve independent fine user control of ggplot geom settings when using groups in multiple geom's

sbihorel Sebastien.Bihorel at cognigencorp.com
Thu Oct 29 17:26:16 CET 2015


Hello,

Before I get to my question, I want to make clear that the topic of my 
present post is similar to posts I recently submitted to the list. 
Although I appreciate the replies I got, I believe that I did not 
correctly frame these previous posts to get to the bottom of things.
I also want to make clear that the code example that I have inserted in 
this post is meant to illustrate my points/questions and does not 
reflect a particular interest in the data or the sequence of ggplot 
geom's used (except otherwise mentioned). Actually, I purposefully used 
junk meaningless data, geom's sequence, and settings, so that we agree 
the plot is ugly and that we, hopefully, don't get hang on specifics and 
start discussing about the merit of one approach vs another.

So here are my questions:

1- Can a user independently control the settings of each geom's used in 
a ggplot call sequence when grouping is required?

By control, I mean: user defines the graphical settings (groups, symbol 
shapes, colors, fill colors, line types, size scales, and alpha) and 
does not let ggplot choose these settings from some theme default.
By independently, I mean: the set of graphical settings can be totally 
different from one group to the next and from one geom to the next.

If this fine control can be achieved, how would you go about it (please, 
be assured that I already spent hours miserably failing to get to 
anything remotely productive, so your help would be really appreciated)?

library(dplyr)
library(tidyr)
library(ggplot2)
set.seed(1234)
dummy <- data.frame(dummy = numeric())
data <- data.frame(x1 = rep(-2:2, each = 80) + rnorm(4000, sd = 0.1),
                    g1 = rep(1:4, each = 1000))
data <- data %>% mutate(y1 = -x1^2 + 2*x1 - 2 + g1 + rnorm(4000, sd = 0.25))
data2 <- data %>% select(x2=x1, y2=y1, g2=g1) %>% mutate(x2=-x2)
data3 <- data.frame(x3 = sample(seq(-2, 2, by = 0.1), 20, replace = TRUE),
                     y3 = runif(20, min=-8, max=4),
                     g3 = rep(1:4, each = 5)) %>% group_by(g3) %>% 
arrange(x3)

gplot <- ggplot(dummy) ### I know this line is not necessary in this 
particular example, please assume this is relevantin the actual 
framework I am trying to build
gplot <- gplot +
   geom_smooth(data = data2,
               aes(x2, y2, group = g2, color = factor(g2), linetype = 
factor(g2), size = 0.5*g2),
               method = 'loess') +
   geom_path(data = data3,
             aes(x3, y3, group = g3, color = factor(g3), linetype = 
factor(g3), shape = factor(g3), size = 0.5*g3)) +
   geom_point(data = data,
              aes(x1, y1, group = g1, color = factor(g1), fill = 
factor(g1), shape = factor(g1), size = g1))
gplot

2- Is the situation easier or more complex (ie, does ggplot make some 
decisions/assumptions for the user?) if the same x, y, and group 
variables are used in different geom's but the user still wants to 
provide independently graphical settings for each geom?

Thank you

Sebastien



More information about the R-help mailing list