Manual color and linetype, and additional geom with fixed color

511 views
Skip to first unread message

Sebastian Schubert

unread,
Jun 24, 2014, 6:53:05 AM6/24/14
to ggp...@googlegroups.com
Hi,

I have many lines in one plot and to differentiate between them I want to combine color and linetype following the suggestion here:

This works fine. However, I want to add another geom with a fixed color. This color overwrites all other colors, but only when using the combination of color and linetype as described above. Here is an example:

#############################

library(ggplot2)
library(RColorBrewer)

d <- data.frame(x=c(rep(1,10), rep(2,10)),
                y=c(seq(1,10), seq(2,11)),
                model=letters[seq(1, 10)])

p1 <- ggplot(d, aes(x=x, y=y, color=model)) + geom_line()
p2 <- ggplot(d, aes(x=x, y=y, color=model, linetype=model)) + geom_line() +
    scale_linetype_manual(values = c(rep("solid", 5), rep("dashed", 5))) +
    scale_color_manual(values = c(brewer.pal(5, "Paired"), brewer.pal(5, "Paired")))

## similar colors
p1 
## works as expected but similar colors
p1 + stat_summary(fun.y=mean, colour="black", geom="line")

## easier to distinguish 
p2 
## all elements black 
p2 + stat_summary(fun.y=mean, colour="black", geom="line")

#############################

Is this a bug or a feature? What can I do?

Thanks a lot!
Sebastian

Ista Zahn

unread,
Jun 24, 2014, 11:12:23 AM6/24/14
to Sebastian Schubert, ggplot2
I would consider that a bug. Fortunately it's easy to work around:

ggplot(d, aes(x=x, y=y, color=model, linetype=model)) +
stat_summary(fun.y=mean, color="black", geom="line") +
geom_line() +
scale_linetype_manual(values = c(rep("solid", 5), rep("dashed", 5))) +
scale_color_manual(values = c(brewer.pal(5, "Paired"),
brewer.pal(5, "Paired")))


Note that the order matters--geom_line() has to come after stat_summary().

Best,
Ista
> --
> --
> 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.

Sebastian Schubert

unread,
Jun 24, 2014, 11:30:15 AM6/24/14
to Ista Zahn, ggplot2
As far as I can see, this solves the color issue but there is no line from
stat_summary(fun.y=mean, color="black", geom="line")
anyway. I've just realized that
p2 + stat_summary(fun.y=mean, colour="black", geom="line")
in my example does also not create the line...

So apparently not only the color is a problem here.

Thanks
Sebastian

Ito, Kaori (Groton)

unread,
Jun 24, 2014, 11:47:04 AM6/24/14
to Sebastian Schubert, Ista Zahn, ggplot2
Maybe the easiest fix for this problem is calculate mean outside:

tab <- ddply(d, .(x), summarize, y=mean(y))
p2 + geom_line(data=tab, aes(color=NULL, linetype=NULL))

Ista Zahn

unread,
Jun 24, 2014, 11:49:01 AM6/24/14
to Sebastian Schubert, ggplot2
I take it back, the only bug was in my brain, probably due to
insufficient coffee. The issue is simply that stat_summay is creating
separate lines for each model, because of the linetype mapping. You
can make it create only a single line by overriding the group mapping,
like this:

p2 + stat_summary(aes(group = 1), fun.y=mean, colour="black", geom="line")

Best,
Ista

On Tue, Jun 24, 2014 at 6:53 AM, Sebastian Schubert
<schube...@gmail.com> wrote:

Sebastian Schubert

unread,
Jun 24, 2014, 4:21:00 PM6/24/14
to Ista Zahn, ggplot2
Hi Ista,

thanks a lot, this explains the behavior. Maybe more consistent is to
explicitly set the linetype as well:

p2 + stat_summary(fun.y=mean, colour="black", linetype=1, geom="line")

Best wishes,
Sebastian
Reply all
Reply to author
Forward
0 new messages