Home > Software design >  Sort values in dumbbell plot horizontal bar in R
Sort values in dumbbell plot horizontal bar in R

Time:09-22

I am trying to sort my plot values on the vertical axis in a similar order in both facets. For instance in the plot attached under visible the order for the conditions are ("IPS", "CTR", and "CEN"), I want the same arrangement for the facet - remembered. Any ideas on how to arrange both in the same pattern. enter image description here

data10 <- structure(list(Group = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Visible", 
"Remembered"), class = "factor"), Condition = structure(c(1L, 
2L, 3L, 1L, 2L, 3L), .Label = c("CEN", "IPS", "CTR"), class = "factor"), 
    `Trial block` = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Pretest", 
    "Posttest"), class = "factor"), N = c(12, 12, 12, 12, 12, 
    12), PL_102 = c(5.855241667, 5.985275, 4.771216667, 7.188145833, 
    7.7207375, 6.468), PL_104 = c(8.225225, 7.84795, 8.1799875, 
    6.4192625, 7.0971125, 5.937570833), MT_102 = c(1089.966667, 
    954.5625, 829.5083333, 993.475, 961.4375, 748.5416667), MT_104 = c(1229.9125, 
    1153.7625, 1244.570833, 843.625, 901.4375, 669.8041667), 
    ARV_102 = c(5.375858333, 6.293454167, 6.341808333, 8.060779167, 
    7.644770833, 8.711075), ARV_104 = c(6.793625, 6.905570833, 
    7.594933333, 7.8801125, 7.526154167, 8.449425), NJ_102 = c(358.9311375, 
    449.9810292, 238.589375, 281.1215333, 268.6619583, 173.4592417
    ), NJ_104 = c(264.9741292, 283.7545833, 320.4076625, 249.2320625, 
    258.952725, 145.4800333), IDE_102 = c(19.66653759, 13.13362506, 
    20.17670876, 20.2698144, 16.98748243, 23.74337103), IDE_104 = c(12.47567853, 
    13.04815886, 19.25615657, 16.74588523, 15.6534457, 21.23477082
    ), EE_102 = c(7.333368735, 7.09299324, 7.034015836, 6.646904232, 
    6.532291162, 5.877550525), EE_104 = c(6.803615461, 7.856943584, 
    6.988436234, 6.76286594, 6.597801229, 6.294069392), PL_DIFF = c(2.369983333, 
    1.862675, 3.408770833, -0.768883333, -0.623625, -0.530429167
    ), MT_DIFF = c(139.9458333, 199.2, 415.0625, -149.85, -60, 
    -78.7375), ARV_DIFF = c(1.417766667, 0.612116667, 1.253125, 
    -0.180666667, -0.118616667, -0.26165), NJ_DIFF = c(-93.95700833, 
    -166.2264458, 81.8182875, -31.88947083, -9.709233333, -27.97920833
    ), IDE_DIFF = c(-7.190859062, -0.085466206, -0.920552189, 
    -3.523929171, -1.334036734, -2.508600214), EE_DIFF = c(-0.529753274, 
    0.763950345, -0.045579602, 0.115961708, 0.065510067, 0.416518866
    )), row.names = c(NA, -6L), spec = structure(list(cols = list(
    Group = structure(list(), class = c("collector_double", "collector"
    )), Condition = structure(list(), class = c("collector_double", 
    "collector")), `Trial block` = structure(list(), class = c("collector_double", 
    "collector")), N = structure(list(), class = c("collector_double", 
    "collector")), PL_102 = structure(list(), class = c("collector_double", 
    "collector")), PL_104 = structure(list(), class = c("collector_double", 
    "collector")), MT_102 = structure(list(), class = c("collector_double", 
    "collector")), MT_104 = structure(list(), class = c("collector_double", 
    "collector")), ARV_102 = structure(list(), class = c("collector_double", 
    "collector")), ARV_104 = structure(list(), class = c("collector_double", 
    "collector")), NJ_102 = structure(list(), class = c("collector_double", 
    "collector")), NJ_104 = structure(list(), class = c("collector_double", 
    "collector")), IDE_102 = structure(list(), class = c("collector_double", 
    "collector")), IDE_104 = structure(list(), class = c("collector_double", 
    "collector")), EE_102 = structure(list(), class = c("collector_double", 
    "collector")), EE_104 = structure(list(), class = c("collector_double", 
    "collector")), PL_DIFF = structure(list(), class = c("collector_double", 
    "collector")), MT_DIFF = structure(list(), class = c("collector_double", 
    "collector")), ARV_DIFF = structure(list(), class = c("collector_double", 
    "collector")), NJ_DIFF = structure(list(), class = c("collector_double", 
    "collector")), IDE_DIFF = structure(list(), class = c("collector_double", 
    "collector")), EE_DIFF = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))
library(tidyverse)
library(ggplot2) 
library(rlang)
library(utils)
library(data.table)
library(dumbbell)

dumbbell::dumbbell(data10, id="Condition", key="Group", leg = "Trial block", column1 = "EE_102", column2="EE_104", delt=1, lab1="Pretest", lab2="Posttest", p_col1 = "black", p_col2 = "grey40", textsize = 3, segsize = 1.5, pointsize = 2.5, title = "Change in Endpoint error (cm)")    
  xlim(5.5,8.5)   facet_wrap( Group ~., scales="free", ncol=1)  
  theme(axis.text.x = element_text(size = 11,face="bold"),
        axis.text.y = element_text(size = 10, face = "bold"))   theme(legend.position="right")    annotate('text', x = 2.8, y = "IPS", label="*", size =6)


CodePudding user response:

One option to achieve your desired result would be to manually set the limits of the y scale in your desired order like so:

library(tidyverse)
library(ggplot2)
library(dumbbell)

dumbbell::dumbbell(data10, id = "Condition", key = "Group", leg = "Trial block", 
                   column1 = "EE_102", column2 = "EE_104", 
                   delt = 1, lab1 = "Pretest", lab2 = "Posttest", 
                   p_col1 = "black", p_col2 = "grey40", 
                   textsize = 3, segsize = 1.5, pointsize = 2.5, 
                   title = "Change in Endpoint error (cm)")  
  scale_y_discrete(limits = c("CEN", "CTR", "IPS"))   
  xlim(5.5, 8.5)  
  facet_wrap(Group ~ ., scales = "free", ncol = 1)  
  theme(
    axis.text.x = element_text(size = 11, face = "bold"),
    axis.text.y = element_text(size = 10, face = "bold")
  )   theme(legend.position = "right")   annotate("text", x = 2.8, y = "IPS", label = "*", size = 6)

  • Related