Reputation: 2797
I want to draw a point-line chart of x-y-variables and highlight two groupings. I know some of the options to distinguish factors, like fill
, shape
, col
or group
. For the first group I would like to have color and for the second shape (which may or may not have the same color). And I need a legend to distinguish both groupings (which I already have). Perhaps I have to put aes in geom_line or geom_point, but I'm not sure. Since later on I would like to adjust size of the shapes (to better distinguish those).
Here is my code:
library(ggplot2)
data <- data.frame(id1=c(1,1,1,2,2,2,3,3,3,4,4,4),
id2=seq(1:3), year=seq(from=2007, to=2018, by=1),
variable=rep(c(5:8), each=3))
# two groups by color and shape, but it drops the line (seperate legends, thats nice)
ggplot(data, aes(x=year, y=variable, col=factor(id1), shape=factor(id2))) +
geom_line() + geom_point()
Upvotes: 4
Views: 23219
Reputation: 173793
Based on further information in comments from the OP, we are looking for something like this:
ggplot(data, aes(x=year, y=variable, col=factor(id1))) +
geom_line() +
geom_point(aes(shape=factor(id2), size = factor(id2))) +
labs(shape = "group 2", colour = "group 1", size = "group 2")
Upvotes: 7