Differentiate lines in geom_dumbbel by colour using levels of a variable

天大地大妈咪最大 提交于 2021-02-19 07:44:12

问题


I am trying to differentiate the lines by a variable "Trial_type", which has two levels. One of the levels is retention and I want that line to be blue, and the other level, transfer I want it to be red. I hope this produces a legend to make the plot more understandable. I have the plot made just missing those key ingredients.

library(ggplot2) 
library(ggalt)   
library(tidyverse)
library(dplyr)
data10 <- structure(list(GROUP = c("RLR", "LRL", "RLR", "LRL", "RLR", "LRL", 
"RRR", "LLL", "RRR", "LLL", "RRR", "LLL"), conditon2 = structure(1:12, .Label = c("T-No Midline crossing_RLR", "T-No Midline crossing_LRL", "T-Midline_RLR", "T-Midline_LRL", 
"T-Midline crossing_RLR", "T-Midline crossing_LRL", "R-No Midline crossing_RRR", 
"R-No Midline crossing_LLL", "R-Midline_RRR", "R-Midline_LLL", 
"R-Midline crossing_RRR", "R-Midline crossing_LLL"), class = "factor"), 
    condition = c("No Midline crossing", "No Midline crossing", 
    "Midline", "Midline", "Midline crossing", "Midline crossing", 
    "No Midline crossing", "No Midline crossing", "Midline", 
    "Midline", "Midline crossing", "Midline crossing"), Trial_type = c("transfer", 
    "transfer", "transfer", "transfer", "transfer", "transfer", 
    "retention", "retention", "retention", "retention", "retention", 
    "retention"), Jam = c(11, 8, 10, 7, 12, 9, 1, 5, 3, 4, 2, 
    6), Training = c("left", "right", "left", "right", "left", 
    "right", "right", "left", "right", "left", "right", "left"
    ), N = c(8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8), MovementTime_102 = c(1690.390625, 
    2266.875, 1649.21875, 1623.59375, 3128.660714, 2608.4375, 
    1701.40625, 2200.234375, 2018.75, 1940.625, 2505.703125, 
    1950.078125), MovementTime_104 = c(1616.71875, 2013.359375, 
    1390.859375, 1435.15625, 2399.765625, 2302.109375, 1336.40625, 
    1658.046875, 1410.625, 1601.875, 1684.53125, 1573.839286), 
    Pathlength_102 = c(2.823023969, 3.450548063, 3.057751688, 
    3.184808844, 3.831920395, 2.779296859, 2.790851078, 3.872267719, 
    2.991207031, 2.8680385, 3.276254563, 2.808966672), Pathlength_104 = c(2.628399641, 
    3.408170609, 2.655326156, 3.017285625, 2.975724094, 2.625024781, 
    2.661606391, 2.945664516, 2.636576609, 2.65516175, 2.782565766, 
    2.568061634), NormalizedJerk_102 = c(1853.413926, 3900.411917, 
    1411.201689, 1391.973644, 6830.819063, 4015.516784, 1340.465366, 
    2981.812369, 2310.589311, 2060.157118, 5617.967587, 2089.925187
    ), NormalizedJerk_104 = c(1394.847247, 2335.718672, 847.952528, 
    914.3836443, 3915.019566, 2563.167235, 765.2752941, 1508.285476, 
    1023.170254, 1092.701687, 1705.629422, 1269.670456), AveResultantVel_102 = c(2.162818172, 
    2.037483406, 2.374258766, 2.449869109, 1.467132962, 1.198650234, 
    1.983419094, 1.994460031, 2.101220406, 2.021215719, 1.595565484, 
    1.789839578), AveResultantVel_104 = c(2.113288797, 2.213037, 
    2.39646225, 2.822947859, 1.481807047, 1.311399453, 2.54589975, 
    2.193130625, 2.644351188, 2.317185313, 2.014750766, 2.113643324
    ), EndpointError_102 = c(2.03515938, 1.83768956, 1.374642, 
    1.57970041, 2.58900025, 2.14219202, 1.64171472, 2.05362611, 
    2.28107478, 1.62285542, 2.13489883, 2.27036917), EndpointError_104 = c(1.593606016, 
    1.404618047, 1.01018125, 1.239002656, 1.672676594, 1.796644641, 
    1.503123406, 1.477764016, 1.483629813, 0.979220453, 1.370374047, 
    1.621229031), MT_diff = c(-73.67, -253.52, -258.36, -188.44, 
    -728.9, -306.33, -365, -542.19, -608.13, -338.75, -821.17, 
    -376.24), PL_diff = c(-0.19, -0.04, -0.4, -0.17, -0.86, -0.15, 
    -0.13, -0.93, -0.35, -0.21, -0.49, -0.24), NJ_diff = c(-458.57, 
    -1564.69, -563.25, -477.59, -2915.8, -1452.35, -575.19, -1473.53, 
    -1287.42, -967.46, -3912.34, -820.25), RV_diff = c(-0.05, 
    0.18, 0.02, 0.37, 0.01, 0.11, 0.56, 0.2, 0.54, 0.3, 0.42, 
    0.32), EE_diff = c(-0.44, -0.43, -0.36, -0.34, -0.92, -0.35, 
    -0.14, -0.58, -0.8, -0.64, -0.76, -0.65), country = structure(c(5L, 
    8L, 6L, 9L, 4L, 7L, 2L, 11L, 3L, 12L, 1L, 10L), .Label = c("R-Midline crossing_RRR", 
    "R-No Midline crossing_RRR", "R-Midline_RRR", "T-Midline crossing_RLR", 
    "T-No Midline crossing_RLR", "T-Midline_RLR", "T-Midline crossing_LRL", 
    "T-No Midline crossing_LRL", "T-Midline_LRL", "R-Midline crossing_LLL", 
    "R-No Midline crossing_LLL", "R-Midline_LLL"), class = "factor")), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

data10conditon2 <- reorder(data10$conditon2, data10$Trial_type)
data10$conditon2 <- factor(data10$conditon2, levels = levels(data10$conditon2))

ggplot() +
geom_segment(data=data10, aes(y=conditon2, yend=conditon2, x=1200, xend=3250, col = "Trial_type"), color="#b2b2b2", size=0.15)+
  labs(x=NULL, y=NULL, title="Change in Movement time (ms)") +

geom_dumbbell(data=data10, aes(y=conditon2, x=MovementTime_102, xend=MovementTime_104, colour = Trial_type),
              size=1.5, color="#b2b2b2", size_x=3, size_xend = 3,  colour_x = "grey40", colour_xend = "black")+ 
 

geom_text(data=filter(data10, conditon2=="R-Midline crossing_LLL"),
              aes(x=MovementTime_104, y=conditon2, label="Pre-test"),
              color="black", size=3, vjust=-3,hjust=0.75) +
geom_text(data=filter(data10, conditon2=="R-Midline crossing_LLL"),
            aes(x=MovementTime_102, y=conditon2, label="Post-test"),
            color="grey40", size=3, vjust=-3,hjust=-0.1) +
geom_rect(data=data10, aes(xmin=3500, xmax=4000, ymin=-Inf, ymax=Inf), fill="grey") +
  geom_text(data=data10, aes(label=paste0(MT_diff), y=conditon2, x=3750), fontface="bold",size=3) +
  geom_text(data=filter(data10, conditon2=="R-Midline crossing_LLL"), 
            aes(x=3750, y=conditon2, label="Difference"),
            color="black", size=3.1, vjust=-2, fontface="bold") +
  scale_x_continuous(expand=c(0,0), limits=c(1000, 4100)) +
  scale_y_discrete(expand=c(0.1,0))  + 
  theme_bw()  

```[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/qeQy6.png

回答1:


Do you mean something like this? Besides adding a $ in data10$conditon2 I have put the aestheics into the main ggplot call so that geom_dumbell - and maybe other geoms - can inherit from this.

The color is then manually defined by a named vector in scale_color_manual.


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=MovementTime_102, xend=MovementTime_104, 
           colour = Trial_type)) +
  labs(x=NULL, y=NULL, title="Change in Movement time (ms)") +
  geom_dumbbell() +

  scale_color_manual(values = c('retention' = ' blue', 'transfer' = 'red'))+

  geom_text(data=filter(data10, conditon2=="R-Midline crossing_LLL"),
            aes(x=MovementTime_104, y=conditon2, label="Pre-test"),
            color="black", size=3, vjust=-3,hjust=0.75) +
  geom_text(data=filter(data10, conditon2=="R-Midline crossing_LLL"),
            aes(x=MovementTime_102, y=conditon2, label="Post-test"),
            color="grey40", size=3, vjust=-3,hjust=-0.1) +
  geom_rect(data=data10, aes(xmin=3500, xmax=4000, ymin=-Inf, ymax=Inf), fill="grey") +
  geom_text(data=data10, aes(label=paste0(MT_diff), y=conditon2, x=3750), fontface="bold",size=3) +
  geom_text(data=filter(data10, conditon2=="R-Midline crossing_LLL"), 
            aes(x=3750, y=conditon2, label="Difference"),
            color="black", size=3.1, vjust=-2, fontface="bold") +
  scale_x_continuous(expand=c(0,0), limits=c(1000, 4100)) +
  scale_y_discrete(expand=c(0.1,0))  + 
  theme_bw()  



来源:https://stackoverflow.com/questions/62948392/differentiate-lines-in-geom-dumbbel-by-colour-using-levels-of-a-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!