After executing the code here below, I was wondering:
1- Why "A.line" and "B.line" variables appear in the geom_point() legend.
2- why there are four colors in the legend.
I guess both answers are related, but I can not tell what is going on.
I would like to have the legend just with "A.points" and "B.points".
I would also like the same colors in both lines and points (I guess this I can do manually).
Thanks in advance for your help.
Best, David
data.frame(x = rep(1:2,2),
names.points = rep(c("A.point","B.point"), 2),
y.point = c(2, 4, 7, 9),
names.lines = rep(c("A.line","B.line"), each = 2),
y.line = c(3, 3, 8, 8)) %>%
ggplot()
geom_point(aes(x = x, y = y.point, group = names.points, colour = names.points), size = 5)
geom_line(aes(x = x, y = y.line, group = names.lines, colour = names.lines), show.legend = FALSE)
CodePudding user response:
Legends are not related to geom
s but to the scale
s and display the categories (or the range of the values) mapped on an aesthetic. Hence, you get four colors because you have four categories mapped on the color aesthetic. The geom
s used are only displayed in the legend key via the so called key glyph which is a point for geom_point
and a line for geom_line
. And show.legend=FALSE
only means to not display the key glyph for geom_line
in the legend key, i.e. the legend keys shows only a point but no line.
To remove the categories related to the lines from your legend use e.g. the breaks
argument of scale_color_discrete
instead.
library(ggplot2)
library(dplyr)
data.frame(
x = rep(1:2, 2),
names.points = rep(c("A.point", "B.point"), 2),
y.point = c(2, 4, 7, 9),
names.lines = rep(c("A.line", "B.line"), each = 2),
y.line = c(3, 3, 8, 8)
) %>%
ggplot()
geom_point(aes(x = x, y = y.point, group = names.points, colour = names.points), size = 5)
geom_line(aes(x = x, y = y.line, group = names.lines, colour = names.lines), show.legend = FALSE)
scale_color_discrete(breaks = c("A.point", "B.point"))
UPDATE To fix your issue with the colors you could use a named color vector:
pal_col <- rep(c("darkblue","darkred"), 2)
names(pal_col) <- c("A.point", "B.point", "A.line", "B.line")
data.frame(
x = rep(1:2, 2),
names.points = rep(c("A.point", "B.point"), 2),
y.point = c(2, 4, 7, 9),
names.lines = rep(c("A.line", "B.line"), each = 2),
y.line = c(3, 3, 8, 8)
) %>%
ggplot()
geom_point(aes(x = x, y = y.point, group = names.points, colour = names.points), size = 5)
geom_line(aes(x = x, y = y.line, group = names.lines, colour = names.lines), show.legend = FALSE)
scale_color_manual(breaks = c("A.point", "B.point"),
values = pal_col)