rggplot2ggalt

Formatting geom dumbel plot in R after reordering data in a specific order


I want the retention trials on top and the transfer trials at the bottom. I tried reordering the data but it is doing exactly the opposite of what I want. Thus the grey lines on top and the black at the bottom.

I want the retention trials on top and the transfer trials at the bottom. I tried reordering the data but it is doing exactly the opposite of what I want. Thus the grey lines on top and the black at the bottom.

Current plot

data10 <- structure(list(GROUP = c("LLL", "LRL", "RLR", "RRR", "LLL", "LRL", 
"RLR", "RRR", "LLL", "LRL", "RLR", "RRR"), conditon2 = c("R-Midline_LLL", 
"T-Midline_LRL", "T-Midline_RLR", "R-Midline_RRR", "R-No Midline crossing_LLL", 
"T-No Midline crossing_LRL", "T-No Midline crossing_RLR", "R-No Midline crossing_RRR", 
"R-Midline crossing_LLL", "T-Midline crossing_LRL", "T-Midline crossing_RLR", 
"R-Midline crossing_RRR"), condition = c(1, 1, 1, 1, 2, 2, 2, 
2, 3, 3, 3, 3), Trial_type = c("Retention", "Transfer", "Transfer", 
"Retention", "Retention", "Transfer", "Transfer", "Retention", 
"Retention", "Transfer", "Transfer", "Retention"), Training = c("left", 
"right", "left", "right", "left", "right", "left", "right", "left", 
"right", "left", "right"), AveResultantVel_102 = c(2.021215719, 
2.389143988, 2.317206313, 2.101220406, 1.994460031, 2.008862717, 
2.162818172, 1.983419094, 1.789839578, 1.178901484, 1.444998757, 
1.595565484), AveResultantVel_104 = c(2.281623618, 2.648132625, 
2.326539432, 2.644351188, 2.156819721, 2.103392923, 2.088311036, 
2.497797074, 2.113643324, 1.301035183, 1.481807047, 1.996934752
), EndpointError_102 = c(1.569026787, 1.372961825, 1.301714076, 
2.008901245, 1.979128047, 1.64400094, 1.891724673, 1.571540547, 
1.878836891, 2.099148779, 2.426338581, 1.872127873), EndpointError_104 = c(0.979220453, 
1.187221906, 0.940806623, 1.483629813, 1.477764016, 1.362604837, 
1.456501911, 1.241231281, 1.409004828, 1.724199558, 1.672676594, 
1.370374047), Pathlength_102 = c(2.822461771, 3.106740763, 3.007654446, 
2.79222754, 3.353013154, 3.411759275, 2.729179389, 2.733244547, 
2.78574039, 2.784003452, 3.290560629, 3.156035284), Pathlength_104 = c(2.615339959, 
2.955880176, 2.691606083, 2.558571102, 2.945664516, 3.266290452, 
2.628399641, 2.629707674, 2.597245428, 2.666422279, 2.968108005, 
2.758916433), NormalizedJerk_102 = c(1585.195804, 1201.912525, 
1203.902331, 1602.550867, 2645.919832, 3053.514635, 1294.166833, 
1151.292095, 1913.713201, 3626.035498, 5851.394788, 3593.940565
), NormalizedJerk_104 = c(999.934117, 890.20336, 847.952528, 
907.134951, 1438.14043, 2335.71867, 1168.53531, 738.136837, 1173.04004, 
2563.16723, 3564.38342, 1705.62942), MovementTime_102 = c(1851.149554, 
1595.078125, 1512.109375, 1857.02753, 2067.526042, 2177.3125, 
1646.96875, 1655.502232, 1934.073661, 2670.412946, 2956.741071, 
2370.65625), MovementTime_104 = c(1531.5625, 1439.955357, 1389.0625, 
1387.946429, 1658.046875, 2013.359375, 1601.316964, 1336.40625, 
1573.839286, 2302.109375, 2399.765625, 1684.53125), RV_diff = c(0.2604079, 
0.258988638, 0.00933312, 0.543130781, 0.16235969, 0.094530207, 
-0.074507136, 0.51437798, 0.323803746, 0.122133699, 0.03680829, 
0.401369268), EE_diff = c(-0.589806333, -0.185739919, -0.360907453, 
-0.525271432, -0.501364031, -0.281396103, -0.435222762, -0.330309266, 
-0.469832063, -0.374949221, -0.753661987, -0.501753826), PL_diff = c(-0.207121812, 
-0.150860586, -0.316048364, -0.233656438, -0.407348638, -0.145468822, 
-0.100779749, -0.103536873, -0.188494962, -0.117581173, -0.322452624, 
-0.397118851), NJ_diff = c(-585.2616874, -311.7091655, -355.9498032, 
-695.4159158, -1207.779402, -717.7959647, -125.6315232, -413.1552577, 
-740.6731613, -1062.868268, -2287.011368, -1888.311145), MT_diff = c(-319.5870536, 
-155.1227679, -123.046875, -469.0811013, -409.4791668, -163.953125, 
-45.65178575, -319.0959821, -360.234375, -368.3035714, -556.9754464, 
-686.125)), class = "data.frame", row.names = c(NA, -12L), spec = structure(list(
    cols = list(GROUP = structure(list(), class = c("collector_character", 
    "collector")), conditon2 = structure(list(), class = c("collector_character", 
    "collector")), condition = structure(list(), class = c("collector_double", 
    "collector")), Trial_type = structure(list(), class = c("collector_character", 
    "collector")), Training = structure(list(), class = c("collector_character", 
    "collector")), AveResultantVel_102 = structure(list(), class = c("collector_double", 
    "collector")), AveResultantVel_104 = structure(list(), class = c("collector_double", 
    "collector")), EndpointError_102 = structure(list(), class = c("collector_double", 
    "collector")), EndpointError_104 = structure(list(), class = c("collector_double", 
    "collector")), Pathlength_102 = structure(list(), class = c("collector_double", 
    "collector")), Pathlength_104 = structure(list(), class = c("collector_double", 
    "collector")), NormalizedJerk_102 = structure(list(), class = c("collector_double", 
    "collector")), NormalizedJerk_104 = structure(list(), class = c("collector_double", 
    "collector")), MovementTime_102 = structure(list(), class = c("collector_double", 
    "collector")), MovementTime_104 = structure(list(), class = c("collector_double", 
    "collector")), RV_diff = structure(list(), class = c("collector_double", 
    "collector")), EE_diff = structure(list(), class = c("collector_double", 
    "collector")), PL_diff = structure(list(), class = c("collector_double", 
    "collector")), NJ_diff = structure(list(), class = c("collector_double", 
    "collector")), MT_diff = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

library(tidyverse)
library(ggalt)


data10$conditon2 <- reorder(data10$conditon2, data10$Trial_type)

data10$conditon2 <- factor(data10$conditon2, levels = levels(data10$conditon2))


ggplot(data = data10,
       aes(y=conditon2, x=EndpointError_102, xend=EndpointError_104)) +
  labs(x=NULL, y=NULL, title="Change in Endpoint error (cm)",fontface="bold") +
  
  geom_dumbbell(aes(colour = factor(Trial_type)), size_x = 2.5, size_xend = 2.5)+
  
  
  scale_color_manual(values = c('Retention' = ' grey40', 'Transfer' = 'black'))+
  
  geom_text(data=filter(data10, conditon2=="R-Midline_RRR"),
            aes(x=EndpointError_104, y=conditon2, label="Post-test"),
            color="black", size=3,  vjust=-3,hjust=1.0) +
  geom_text(data=filter(data10, conditon2=="R-Midline_RRR"),
            aes(x=EndpointError_102, y=conditon2, label="Pre-test"),
            color="grey40", size=3, vjust=-3,hjust=-0.0000) +
  geom_rect(data=data10, aes(xmin=3.1, xmax=3.6, ymin=-Inf, ymax=Inf), fill="grey") +
  geom_text(data=data10, aes(label=paste0(EE_diff), y=conditon2, x=3.35), fontface="bold",size=3) +
  geom_text(data=filter(data10, conditon2=="R-Midline_RRR"), 
            aes(x=3.35, y=conditon2, label="Difference"),
            color="black", size=3.1, vjust=-2, fontface="bold") +
  scale_x_continuous(expand=c(0,0), limits=c(0.7, 3.7)) +
  scale_y_discrete(expand=c(0.1,0))  + 
  theme_bw() +
  theme(axis.text.x = element_text(size = 11,face="bold"),
        axis.text.y = element_text(size = 10, face = "bold")) +
  
  
  guides(col=guide_legend(override.aes = list(size = 4),title="Experimental groups")) 

  
  
 

     


Solution

  • To get the right order you can make use of forcats::fct_reorder to reorder conditon2 in descending order. I also added the condition conditon2 == levels(conditon2)[length(levels(conditon2))] to put the difference label automatically on top of the last level. Try this:

    library(tidyverse)
    library(ggalt)
    
    data10$conditon2 <- forcats::fct_reorder(data10$conditon2, data10$conditon2, .desc = TRUE)
    
    ggplot(data = data10,
           aes(y=conditon2, x=EndpointError_102, xend=EndpointError_104)) +
      labs(x=NULL, y=NULL, title="Change in Endpoint error (cm)",fontface="bold") +
      
      geom_dumbbell(aes(colour = factor(Trial_type)), size_x = 2.5, size_xend = 2.5)+
      
      
      scale_color_manual(values = c('Retention' = ' grey40', 'Transfer' = 'black'))+
      
      geom_text(data=filter(data10, conditon2=="R-Midline_RRR"),
                aes(x=EndpointError_104, y=conditon2, label="Post-test"),
                color="black", size=3,  vjust=-3,hjust=1.0) +
      geom_text(data=filter(data10, conditon2=="R-Midline_RRR"),
                aes(x=EndpointError_102, y=conditon2, label="Pre-test"),
                color="grey40", size=3, vjust=-3,hjust=-0.0000) +
      geom_rect(data=data10, aes(xmin=3.1, xmax=3.6, ymin=-Inf, ymax=Inf), fill="grey") +
      geom_text(data=data10, aes(label=paste0(EE_diff), y=conditon2, x=3.35), fontface="bold",size=3) +
      geom_text(data=filter(data10,  conditon2 == levels(conditon2)[length(levels(conditon2))]), 
                aes(x=3.35, y=conditon2, label="Difference"),
                color="black", size=3.1, vjust=-2, fontface="bold") +
      scale_x_continuous(expand=c(0,0), limits=c(0.7, 3.7)) +
      scale_y_discrete(expand=c(0.1,0))  + 
      theme_bw() +
      theme(axis.text.x = element_text(size = 11,face="bold"),
            axis.text.y = element_text(size = 10, face = "bold")) +
      
      
      guides(col=guide_legend(override.aes = list(size = 4),title="Experimental groups"))