Re: manually condense the legend

53 views
Skip to first unread message

Kohske Takahashi

unread,
Sep 26, 2012, 6:14:55 PM9/26/12
to Peng Zhang, ggp...@googlegroups.com
Could you please provide a reproducible example:
https://github.com/hadley/devtools/wiki/Reproducibility

2012/9/19 Peng Zhang <pcz...@gmail.com>:
> Dear all,
>
> I am plotting paths for seven subgroup (g). I specify color=g, so they will
> have different colors. Next I want to enhance one of them by choosing
> different size for one of seven subgroups, I then create a manual scale for
> size according to g==1.
>
> In the plot, I will have both legends for color and size. My question is if
> there is a way that I can manually condense those two legends into one. (six
> lines with the regular size and different colors and one more line with
> enhanced size and another color)
>
> Thanks,
> Peng
>
> --
> 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



--
Kohske Takahashi <takahash...@gmail.com>

Assistant Professor,
Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Peng Zhang

unread,
Sep 26, 2012, 9:56:07 PM9/26/12
to Kohske Takahashi, ggp...@googlegroups.com
Thank you for your reply.

The following small code gives me two legends (one for color and one for
size). Is it possible that I only have one legend?

thick red line ---- Cubic
thin green line ---- Linear
thick blue line ----- Quadratic

Thanks,
Peng

x = 1:9
a = rep(x, 3)
b = c(x, x^2, x^3)
col = c(rep("Linear", 9), rep("Quadratic", 9), rep("Cubic", 9))
size = c(rep("Linear", 9), rep("Non-Linear", 18))

d = data.frame(a, b, col, size)
library(ggplot2)
ggplot(data = d, aes(a, b, color = col, size = size)) + geom_path() +
labs(color = "", size = "")

Kohske Takahashi

unread,
Sep 26, 2012, 9:58:58 PM9/26/12
to Peng Zhang, ggp...@googlegroups.com
you can suppress to show guide by

ggplot(data = d, aes(a, b, color = col, size = size)) + geom_path() +
labs(color = "", size = "") + guides(size = "none")


2012/9/27 Peng Zhang <pcz...@gmail.com>:
> x = 1:9
> a = rep(x, 3)
> b = c(x, x^2, x^3)
> col = c(rep("Linear", 9), rep("Quadratic", 9), rep("Cubic", 9))
> size = c(rep("Linear", 9), rep("Non-Linear", 18))
>
> d = data.frame(a, b, col, size)
> library(ggplot2)
> ggplot(data = d, aes(a, b, color = col, size = size)) + geom_path() +
> labs(color = "", size = "")



Peng Zhang

unread,
Sep 26, 2012, 10:06:03 PM9/26/12
to Kohske Takahashi, ggp...@googlegroups.com
Thanks. I kind of still want size, but to show it together with color.
(by changing the size of line in the color legend)

Best,
Peng

Kohske Takahashi

unread,
Sep 26, 2012, 10:16:25 PM9/26/12
to Peng Zhang, ggp...@googlegroups.com
then try this:

x = 1:9
a = rep(x, 3)
b = c(x, x^2, x^3)
col = c(rep("Linear", 9), rep("Quadratic", 9), rep("Cubic", 9))

d = data.frame(a, b, col)
d$col <- factor(d$col, levels = c("Linear", "Quadratic", "Cubic"))
library(ggplot2)
ggplot(data = d, aes(a, b, color = col, size = col)) + geom_path() +
scale_size_manual(labels = levels(d$col), values = c(1, 6, 6))



2012/9/27 Peng Zhang <pcz...@gmail.com>:

Peng Zhang

unread,
Sep 26, 2012, 10:21:55 PM9/26/12
to Kohske Takahashi, ggp...@googlegroups.com
That is exactly what I am looking for. Thank you for your help!

Best,
Peng
Reply all
Reply to author
Forward
0 new messages