Home > Software design >  A different shape for points in a line chart
A different shape for points in a line chart

Time:07-11

How to make a simple line graph where each line will have additional points of different shapes (geom_point). These points should be of the same color as the lines.

my_data <- data.frame(
  TIME = c("T1-3","T3-5","T6-8","T8-10","T1-3","T3-5","T6-8","T8-10"),
  GROUP = c("A","A","A","A","B","B","B","B"),
  M = as.numeric(c("7.52","7.85","8.6","9.2","7.55","7.85","8.61","9.22")),
  SD = as.numeric(c("0.19","0.16","0.19","0.26","0.2","0.22","0.2","0.26")))

In the graph below, the points are the same on both lines.

ggplot(my_data, aes(x = TIME, y = M, group = GROUP, color = GROUP))   
  geom_line(position = position_dodge(0.3))  
  geom_point(position = position_dodge(0.3))  
  scale_shape_manual(values = c(0, 1))  
  geom_errorbar(aes(x = TIME, ymin = M-SD, ymax = M SD), width = .2, position = position_dodge(0.3))  
  labs(col = "Group of hurdlers", x = "Phase distance", y = "Time (seconds)")  
  theme_light()  
  theme(legend.position = "top")

When I add the "shape = GROUP" aesthetic, two legends are created and I need one legend :-)

ggplot(my_data, aes(x = TIME, y = M, group = GROUP, color = GROUP, shape = GROUP))   
  geom_line(position = position_dodge(0.3))  
  geom_point(position = position_dodge(0.3))  
  scale_shape_manual(values = c(0, 1))  
  geom_errorbar(aes(x = TIME, ymin = M-SD, ymax = M SD), width = .2, position = position_dodge(0.3))  
  labs(col = "Group of hurdlers", x = "Phase distance", y = "Time (seconds)")  
  theme_light()  
  theme(legend.position = "top")

Can I ask for help in solving this problem?

CodePudding user response:

We could use guide_legend(override.aes = ... and set shape guide to none:

To get filled shapes just change shape 0 and 1:

enter image description here

to shape 15 and 16:

enter image description here

enter image description here

CodePudding user response:

I think that you can put aes(shape = GROUP) in geom_point, and set show.legend = F

ggplot(my_data, aes(x = TIME, y = M, group = GROUP, color = GROUP))   
  geom_line(position = position_dodge(0.3))  
  geom_point(aes(shape = GROUP), show.legend = F, position = position_dodge(0.3))  
  scale_shape_manual(values = c(0, 1))  
  geom_errorbar(aes(x = TIME, ymin = M-SD, ymax = M SD), width = .2, position = position_dodge(0.3))  
  labs(col = "Group of hurdlers", x = "Phase distance", y = "Time (seconds)")  
  theme_light()  
  theme(legend.position = "top")

enter image description here

  •  Tags:  
  • r
  • Related