0

I have code that plots paired points and also graphs half violin and box plots of pretest and posttest scores. I added lines between the paired points, but I am having difficulty with adding an arrow to the lines and changing the color depending on whether the score decreased or increased.

Below is my code and an image of my graph.

Graph

f5 <- ggplot(data = d, aes(y = y)) +
  
   #Add geom_() objects
   geom_point(data = d %>% filter(x =="1"), aes(x = xj), color = 'dodgerblue', size = 1.5, 
              alpha = .6) +
   geom_point(data = d %>% filter(x =="2"), aes(x = xj), color = 'darkorange', size = 1.5, 
              alpha = .6) +
   geom_line(aes(x = xj, group = id), color = 'lightgray', alpha = .3) +
  
   geom_half_boxplot(
     data = d %>% filter(x=="1"), aes(x=x, y = y), position = position_nudge(x = -.25), 
     side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, 
     fill = 'dodgerblue') +
   
   geom_half_boxplot(
     data = d %>% filter(x=="2"), aes(x=x, y = y), position = position_nudge(x = .15), 
     side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2, 
     fill = 'darkorange') +
  
   geom_half_violin(
     data = d %>% filter(x=="1"),aes(x = x, y = y), position = position_nudge(x = -.3), 
     side = "l", fill = 'dodgerblue') +
 
   geom_half_violin(
     data = d %>% filter(x=="2"),aes(x = x, y = y), position = position_nudge(x = .3), 
     side = "r", fill = "darkorange") +
  
  
   #Define additional settings
   scale_x_continuous(breaks=c(1,2), labels=c("Pre-Test", "Post-Test"), limits=c(0, 3)) +
   xlab("Test") + ylab("Score") +
   ggtitle("Before and After Scores with box- and violin plots") +
   theme_classic()+
   coord_cartesian(ylim=c(y_lim_min, y_lim_max))


stefan
  • 90,330
  • 6
  • 25
  • 51
joshual
  • 43
  • 7
  • It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Jul 27 '22 at 16:23
  • Hello, will do I will adjust the code asap – joshual Jul 27 '22 at 17:05

0 Answers0