Home > database >  Reduce the width of ggplot2 boxplot
Reduce the width of ggplot2 boxplot

Time:09-16

I have the data below and I create a box plot but it is unecessarily wide.How can I reduce its width?

throu<-structure(list(case_id = c("WC4132791", "WC4130879", "WC4128064", 
"WC4121569", "WC4121568", "WC4130112", "WC4131829", "WC4130841", 
"WC4130306", "WC4130417", "WC4124741", "WC4130114", "WC4131990", 
"WC4121986", "WC4128062", "WC4122478", "WC4130337", "WC4125822", 
"WC4127231", "WC4124761", "WC4129398", "WC4131040", "WC4123072", 
"WC4131822", "WC4120712", "WC4121978", "WC4130110", "WC4123522", 
"WC4130307", "WC4122643", "WC4130383", "WC4122248", "WC4122299", 
"WC4122727", "WC4126769", "WC4131186", "WC4125978", "WC4129089", 
"WC4121339", "WC4126469", "WC4131800", "WC4125572", "WC4132378", 
"WC4123345", "WC4130314", "WC4127722", "WC4129978", "WC4131838", 
"WC4130812", "WC4126953"), throughput_time = c(134.283333333333, 
93.0756944444445, 83.5340277777778, 67.7833333333333, 65.3069444444444, 
63.5402777777778, 59.6861111111111, 56.9791666666667, 55.9048611111111, 
54.3826388888889, 52.6958333333333, 52.5125, 51.1680555555556, 
50.9520833333333, 50.5402777777778, 49.9291666666667, 49.8201388888889, 
49.7375, 49.0916666666667, 46.3069444444444, 45.30625, 45.2451388888889, 
44.9722222222222, 44.8215277777778, 44.8048611111111, 43.0701388888889, 
42.6840277777778, 42.6576388888889, 42.55, 42.2868055555556, 
42.2805555555556, 41.9027777777778, 41.7409722222222, 41.6506944444444, 
41.3527777777778, 40.7305555555556, 40.2861111111111, 40.2159722222222, 
40.0854166666667, 40.0486111111111, 39.7930555555556, 39.6576388888889, 
39.4638888888889, 39.4527777777778, 39.3569444444444, 39.3513888888889, 
39.1854166666667, 39.0791666666667, 39.0743055555556, 39.0055555555556
)), row.names = c(NA, 50L), class = "data.frame")

# A really basic boxplot.
ggplot(throu, aes( y=throughput_time))   
  geom_boxplot(fill="slateblue", alpha=0.2,width=0.5)   
  xlab("") ylab("Case duration in days")  theme_classic()  
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank())

CodePudding user response:

You can use xlim to change the limits of the x-axis, thus reducing the width:

ggplot(throu, aes(y = throughput_time))   
  geom_boxplot(fill="slateblue", alpha=0.2, width = .01)   
  xlim(-0.1, 0.1)  
  xlab("")   
  ylab("Case duration in days")  
  theme_classic()   
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank())

enter image description here

CodePudding user response:

You can try adjust the margins of the plot:

ggplot(throu, aes( y=throughput_time))   
  geom_boxplot(fill="slateblue", alpha=0.2,width=0.5)   
  xlab("") ylab("Case duration in days")  theme_classic()  
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        plot.margin = margin(t = 0.5,  # Top margin
                             r = 5.5,  # Right margin
                             b = 0.5,  # Bottom margin
                             l = 5.5,  # Left margin
                             unit = "cm"))
  • Related