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

Hadley Wickham h.wickham at gmail.com
Fri Oct 30 12:34:44 CET 2015


I'd recommend reading the ggplot2 book - learning more about how
scales work in ggplot2 will help you understand why this isn't
possible.
Hadley

On Thu, Oct 29, 2015 at 6:31 PM, sbihorel
<Sebastien.Bihorel at cognigencorp.com> wrote:
> Thank for your reply,
>
> I may accept your point about the mapping consistency when the different
> geom's use the same data source. However, as pointed out in my example code,
> this does not have to be the case. Hence my question about the geom-specific
> control of group-dependent graphical settings.
>
> Sebastien
>
>
> On 10/29/2015 4:49 PM, Jeff Newmiller wrote:
>>
>> I think a fundamental design principle of ggplot is that mapping of values
>> to visual representation are consistent within a single plot, so reassigning
>> color mapping for different elements would not be supported.
>>
>> That being said, it is possible to explicitly control specific attributes
>> within a single geom outside of the mapping, though this usually does break
>> mappings in the legend.
>>
>> ---------------------------------------------------------------------------
>> Jeff Newmiller                        The     .....       .....  Go
>> Live...
>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
>> Go...
>>                                        Live:   OO#.. Dead: OO#..  Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#.
>> rocks...1k
>>
>> ---------------------------------------------------------------------------
>> Sent from my phone. Please excuse my brevity.
>>
>> On October 29, 2015 11:27:55 AM MST, sbihorel
>> <Sebastien.Bihorel at cognigencorp.com> wrote:
>>>
>>> Thank you for your reply.
>>>
>>> I do not have anything specific data/geom/grouping in mind, rather a
>>> framework in which users would just pile of each other layer after
>>> layer
>>> of geom each defined with specific settings. A minimum realistic
>>> scenario would a geom_point followed by a geom_smooth or a geom_path
>>> using different colors...
>>>
>>> Sebastien
>>>
>>> On 10/29/2015 1:34 PM, Ista Zahn wrote:
>>>>
>>>> I would say in a word, 'no'. What you seem to be implying is that you
>>>> want multiple color scales, multiple shape scales, etc. As far as I
>>>> know there is no support for that in ggplot2.
>>>>
>>>> Perhaps if you show us what you're actually trying to accomplish
>>>> someone can suggest a solution or at least a work-around.
>>>>
>>>> Best,
>>>> Ista
>>>>
>>>> On Thu, Oct 29, 2015 at 12:26 PM, sbihorel
>>>> <Sebastien.Bihorel at cognigencorp.com> wrote:
>>>>>
>>>>> 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
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide
>>>
>>> http://www.R-project.org/posting-guide.html
>>>>>
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>
>
> --
> Sebastien Bihorel
> Cognigen Corporation
> (t) +1 716 633 3463 ext 323
> Cognigen Corporation, a wholly owned subsidiary of Simulations Plus, Inc.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
http://had.co.nz/



More information about the R-help mailing list