按特定顺序重新排序数据后在 R 中格式化 geom dumbel plot

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

我想要保留试验在顶部,转移试验在底部。我尝试重新排序数据,但它做的与我想要的完全相反。因此顶部的灰色线和底部的黑色线。

我想要保留试验在顶部,转移试验在底部。我尝试重新排序数据,但它做的与我想要的完全相反。因此顶部的灰色线和底部的黑色线。

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")) 

  
  
 

     

您将不得不尝试 data10$condition2 的关卡。

先做成一个因子,R默认按字母顺序排列:

data10$conditon2 <- factor(data10$conditon2)
levels(data10$conditon2)
# [1] "R-Midline crossing_LLL"    "R-Midline crossing_RRR"    "R-Midline_LLL"            
# [4] "R-Midline_RRR"             "R-No Midline crossing_LLL" "R-No Midline crossing_RRR"
# [7] "T-Midline crossing_LRL"    "T-Midline crossing_RLR"    "T-Midline_LRL"            
#[10] "T-Midline_RLR"             "T-No Midline crossing_LRL" "T-No Midline crossing_RLR"

现在您希望您的关卡以 Ts 开头,然后是 Rs。一种方法是您煞费苦心地重置关卡:

# data10$conditon2 <- factor(data10$conditon2,
#                            levels = c("T-Midline crossing_LRL", "T-Midline crossing_RLR", ...)

另一种方式是我知道R默认按字母顺序创建级别,前6个是Rs,后6个是Ls。所以你想要第一个 6 秒的向量,然后是第一个 6 秒的向量:

lvl <- levels(data10$conditon2)
lvl <- c(lvl[7:12], lvl[1:6])

现在您可以使用 lvl 作为水平分解 data10$condition2

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

# [1] "T-Midline crossing_LRL"    "T-Midline crossing_RLR"    "T-Midline_LRL"            
# [4] "T-Midline_RLR"             "T-No Midline crossing_LRL" "T-No Midline crossing_RLR"
# [7] "R-Midline crossing_LLL"    "R-Midline crossing_RRR"    "R-Midline_LLL"            
# [10] "R-Midline_RRR"             "R-No Midline crossing_LLL" "R-No Midline crossing_RRR"

一旦你 运行 你的 ggplot 代码,你现在在底部灰色顶部有黑色。请注意,还有其他方法可以重新排序关卡,这只是一种快速而肮脏的方法。

要获得正确的顺序,您可以使用 forcats::fct_reorder 以降序重新排序 conditon2。我还添加了条件 conditon2 == levels(conditon2)[length(levels(conditon2))] 以将 difference 标签自动放在最后一层的顶部。试试这个:

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")) 

使用哑铃R包

##Load packages
library(tidyverse)
library(ggplot2) 
library(rlang)
library(utils)
library(data.table)
library(dumbbell)


##Reformat data
data10$conditon2 <- forcats::fct_reorder(data10$conditon2, data10$conditon2, .desc = TRUE)
 
 data10A<-data10 %>% select(conditon2,Trial_type,EndpointError_102,EndpointError_104) %>% mutate("key"="Change In Endpoint Error (cm)")
 data10B<-data10A %>% mutate("diff"=EndpointError_102-EndpointError_104)%>% arrange(desc(diff))
 data10A$conditon2<-factor(data10A$conditon2, data10B$conditon2)
 
##Plot the data
 dumbbell::dumbbell(data10A, id="conditon2", key="Trial_type", column1 = "EndpointError_102", column2="EndpointError_104", delt=1, lab1="EndpointError_102", lab2="EndpointError_103", pt_val=1, leg="Change In Endpoint error (cm)")  + 
   xlim(0.7,2.6) + facet_wrap( Trial_type ~., scales="free", ncol=1)

您可以添加或删除一些花哨的功能,这里是一个可能对您有用的样式示例or/and其他人

我没有足够的票数来嵌入情节,所以link如下所示...

尽情享受吧!

dumbbell R package