1
votes

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).

  • How to connect those points by colored lines?
  • How to adjust size of the shape group?

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()

enter image description here

1
You can add aes(group = id1) to the geom_line to connect them by group. Adding something like size = 2 outside of the aes call will change the size of points/lines.svenhalvorson

1 Answers

1
votes

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")

enter image description here