[R-sig-teaching] Choice of graphics package

Ista Zahn istazahn at gmail.com
Sat Nov 28 15:27:53 CET 2015


Hi rjp,

Arg, I totally botched the example last night. Here is a working
example; I think you're right that this is easier in lattice, but it
is definitely not true that you need to add this stuff to each plot.

library(ggplot2)

theme_set(theme_bw() +
          theme(rect = element_rect(fill = "gray10"),
                panel.background = element_rect(fill = "gray10")))

scale_fill_discrete <- function(...) scale_fill_manual(values =
c("orange", "purple",
"pink", "cyan"))

ggplot(mtcars, aes(x = hp, fill = factor(gear))) + geom_bar()

Best,
Ista


On Sat, Nov 28, 2015 at 9:03 AM, Randall Pruim <rpruim at calvin.edu> wrote:
> When I run your code, the colors in my plot are not set to the colors chosen
> unless I do
>
> ggplot(mtcars, aes(x = hp, fill = factor(gear))) + geom_bar() +
> scale_discrete
>
> Did your code example really work for you?
>
> I think the point is that lattice and ggplot2 differ in terms of what is
> called part of the theme.  In lattice, the colors used for discrete
> variables are part of the theme, and so can be included in the global
> default.  In ggplot2, they are not part of the theme and cannot be set with
> theme_set().
>
> I get that you can save your scales information as an object and add it on
> to any plot so you only have to specify the colors (or whatever) once.   But
> I think you still need to add the scale information onto each plot.
>
> —rjp
>
>
> On Nov 27, 2015, at 9:18 PM, Ista Zahn <istazahn at gmail.com> wrote:
>
> Woops, messed up the example! I meant
>
> theme_set(theme_bw() +
>          theme(rect = element_rect(fill = "gray10")))
>
> scale_discrete <- scale_fill_manual(values = c("orange", "purple",
> "pink", "cyan"))
>
> ggplot(mtcars, aes(x = hp, fill = factor(gear))) + geom_bar()
>
> i.e., the whole point is you don't need to add this stuff to every plot.
>
> Best,
> Ista
>
> On Fri, Nov 27, 2015 at 9:13 PM, Ista Zahn <istazahn at gmail.com> wrote:
>
> Hi Randal,
>
> To be honest I don't know the lattice theme system at all, so it is
> entirely possible that you can easily do things there that are more
> complicated in ggplot2. However, it is easy to set theme and scale[1]
> defaults in ggplot2, e.g.,
>
> library(ggplot2)
>
> theme_set(theme_bw() +
>          theme(rect = element_rect(fill = "gray10")))
>
> scale_discrete <- scale_fill_manual(values = c("orange", "purple",
> "pink", "cyan"))
>
> ggplot(mtcars, aes(x = hp, fill = factor(gear))) + geom_bar() +
>  theme_bw() +
>  scale_fill_manual(values = c("orange", "purple", "pink", "cyan"))
>
>
> Best,
> Ista
>
> [1] In ggplot2 theme elements are distinct from scales, and AFAIK they
> have to be set separately.
>
> On Fri, Nov 27, 2015 at 6:24 PM, Randall Pruim <rpruim at calvin.edu> wrote:
>
>
> On Nov 27, 2015, at 3:37 PM, Ista Zahn <istazahn at gmail.com> wrote:
>
> (Theming in ggplot2 generally requires you to do something to each plot,
> one of the downsides of ggplot2.)
>
>
> That is completely untrue. Whatever downsides ggplot2 may have, this is not
> one of them.
>
>
> Ista,
>
> My guess is that you didn’t understand my claim.  Either that or you know
> something about ggplot2 that I’ve not been able to locate anywhere.  (Since
> Hadley is on  this list, he can chime in if I’m missing something.)
>
> So let me explain what I mean with an example.
>
> Suppose I want to create six plots of various sorts.  Each of them uses
> color according to a factor a with three levels.  That's groups = a in
> lattice and colour = a in ggplot2.  So far so good.
>
> But after I have created all of these plots with the default color choices,
> I decide I want to have the colors be blue, red, and 50% gray.  In lattice,
> I can put this information into the default theme with
>
> trellis.par.set( superpose.symbol = list(col = c("blue", "red", "gray50")) )
>
> and without any adjustments to any of the plotting code, all the plots will
> update to the new color scheme.   show.settings() will even show me what my
> default theme looks like.
>
> One line of code changes in one place and all the plots are using the new
> color scheme.  And if my new choices are choices I will use frequently, I
> can put the theme into a package and do something like
>
> trellis.par.set(theme = theme.mosaic())
>
> There is also the option to add these themes to individual plots using the
> par.settings argument — this is more like the ggplot2 way.
>
> In ggplot2, I need to add on scale_colour_manual() to EACH PLOT, or write
> some sort of wrapper that does the job for me.  In either case, if I started
> out using the defaults and want to make document-wide changes, I need to
> edit EACH plot to get the desired affect.  In lattice, this is one line of
> code and all the plots are good to go.
>
> Last I looked for this, ggplot2 did not provide a way to set these sorts of
> defaults and generally prefers a system where all of this sort of theming is
> located local to each plot.  So plots end up having a bunch of theme stuff
> added on to them like
>
> + theme_minimal()
> + scale_colour_manual(values = c(“blue”, “red”, “gray50”)
> + etc.
>
> See the examples at
>
> http://docs.ggplot2.org/dev/vignettes/themes.html
>
> Especially for teaching, I find this to be a real problem.  I don’t want to
> clutter up early examples with all this sort of formatting, but I do want to
> control the choice of colors used so that they work well for printing or
> projecting in my local environment.  When I’m producing a journal or some
> other sort of report, this is less important because I generally don’t show
> the code in the final document — only the plot.  But it still violates the
> DRY principle (Don’t Repeat Yourself).
>
> —rjp
>
> PS.  I should have mentioned one commonly held, but poorly justified, reason
> to prefer ggplot2 over lattice:  the default colors.  lattice makes it easy
> to adjust the defaults.  latticeExtra even provides a ggplot2 look-alike
> theme that will make your lattice plots look a lot like ggplot2 plots.  (The
> won’t match exactly because they don’t include all of the same elements.
> Liking elements available in one system but not in the other is a possible
> reason to prefer one over the other.)  On the other hand, if you don’t like
> the defaults in ggplot2 — you’re out of luck, you don’t get the change the
> defaults.
>
>
>
>
>



More information about the R-sig-teaching mailing list