0

I am using ggplot to plot the following graph (one example attached). What I want to achieve is to have one legend to show linetype 4 & shape 1 combined as "group 1" and linetype 1 & shape 2 combined as "group 2". Also, the legend name will be something such as "example legend" (based on df1,df2: separate into group 1 and group 2 and each group has 3 lines)

I tried scale_shape_manual and scale_linetype_manual. However, I didn't find the right way. The code is listed as below (the graph has no legend which I want to add correctly)

Thanks.

y1 <- x + 0.01
y2 <- x + 10
df1 <- data.frame(x, y1)
df2 <- data.frame(x, y2)

graph_1 <- subset(df1, x >= 0 & x <= 5)
graph_2 <- subset(df1, x >= 6 & x <= 10)
graph_3 <- subset(df1, x >= 11 & x <= 15)
graph_4 <- subset(df2, x >= 0 & x <= 5)
graph_5 <- subset(df2, x >= 6 & x <= 10)
graph_6 <- subset(df2, x >= 11 & x <= 15)

win.graph(width = 13, height = 6, pointsize = 8)
ggplot() + 
  geom_point(aes(x, y1), data = graph_1, shape = 1) + 
  geom_smooth(aes(x, y1), data = graph_1, method = "loess", linetype = 4) + 
  geom_point(aes(x, y1), data = graph_2, shape = 1) + 
  geom_smooth(aes(x, y1), data = graph_2, method = "loess", linetype = 4) + 
  geom_point(aes(x, y1), data = graph_3, shape = 1) + 
  geom_smooth(aes(x, y1), data = graph_3, method = "loess", linetype = 4) + 
  geom_point(aes(x, y2), data = graph_4, shape = 2) + 
  geom_smooth(aes(x, y2), data = graph_4, method = "loess", linetype = 1) + 
  geom_point(aes(x, y2), data = graph_5, shape = 2) + 
  geom_smooth(aes(x, y2), data = graph_5, method = "loess", linetype = 1) + 
  geom_point(aes(x, y2), data = graph_6, shape = 2) + 
  geom_smooth(aes(x, y2), data = graph_6, method = "loess", linetype = 1)
bouncyball
  • 10,631
  • 19
  • 31
Lernst
  • 13
  • 4
  • Something like this https://stackoverflow.com/a/50240153/786542? – Tung Aug 17 '18 at 16:27
  • Please share sample of your data using `dput()` (not `str` or `head` or picture/screenshot) so others can help. See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – Tung Aug 17 '18 at 16:28
  • yes, it's similar. But there is something different: I want two legends, for example "group 1" and "group 2". each group has 3 lines – Lernst Aug 17 '18 at 16:38
  • 2
    Also consider converting your data to long format to make it easier for plotting without manual typing every single data frame. See these examples: [one](https://stackoverflow.com/a/50206399/786542), [two](https://stackoverflow.com/a/48871624/786542), [three](https://stackoverflow.com/a/51752828/786542) – Tung Aug 17 '18 at 16:40
  • Thanks Tung, what I post here is just one example I created. I need to split the graph into 6 lines for other project purposes. I believe that there should be some method to create a legend correctly~~ – Lernst Aug 17 '18 at 16:43
  • 1
    as stated above ggplot expects data in long format. You will then find it much easier to specify shapes and linetypes for subsets of your data. Think about reshaping your dataset so that each variable is a column and each row an observation. – Chris Aug 17 '18 at 17:54

0 Answers0