Home > OS >  How to move geom_boxplot layer up a bit?
How to move geom_boxplot layer up a bit?

Time:09-17

I would like to create a plot grid which has two layers; One for display density and one for displaying outliers (via geom_boxplot).

I can do it by ggplot2's layered structure via this piece of code;

df %>%
ggplot(aes(x=value,color=variable)) 
facet_grid(~variable,scales = 'free') 
geom_density(aes(y=..scaled..)) 
geom_boxplot(aes(x=value,fill=variable),
             alpha=0.2,outlier.color = 'black',outlier.shape = 3,outlier.alpha = 1)

and it fields;

enter image description here

But I want the side of the boxplot which closest to the x-axis, to be at zero. If visualizing will make it easier to explain;

enter image description here

Here is my data structure which created by dput(df)

structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("hanehalki", 
"gsyih"), class = "factor"), value = c(2.92, 3.05, 2.82, 3.26, 
2.86, 2.88, 4.86, 4.92, 3.34, 3.56, 3.81, 3.53, 3.41, 2.68, 3.28, 
3.23, 3.34, 2.71, 2.98, 3.8, 2.77, 4.09, 3, 3.33, 3.06, 3.4, 
3.05, 3.39, 2.98, 3.1, 2.95, 5.39, 3.13, 3.75, 3.12, 4.88, 2.82, 
2.99, 3.42, 2.95, 3.41, 3.84, 3.28, 2.83, 3.58, 4.68, 2.76, 4.16, 
2.89, 5.03, 41820.1557045057, 29083.9295704606, 31554.2128202566, 
58956.7267073248, 41229.086549323, 38757.9798583395, 23327.2168931588, 
18708.4485132515, 43749.4317129513, 29105.5044386941, 30235.1079173834, 
30422.9283128681, 81228.0951424918, 55608.1583883872, 34800.2529973319, 
35914.5488124995, 33982.3005551545, 44302.0289524954, 46529.3034031361, 
31898.5500012572, 44088.1553908406, 37633.0337337249, 47287.5078938497, 
86798.4429060671, 60631.6357010175, 44210.7976900847, 35742.8253333139, 
36110.0565531365, 31586.3538734972, 33771.6554831237, 30267.3076817561, 
30192.1075499239, 37314.1420295662, 27322.3774815836, 47001.4874496515, 
16727.4244640104, 56462.8833985347, 43783.0846449189, 40892.6265785975, 
60553.5509558071, 32228.4494321578, 34105.7208652381, 34644.9683658929, 
38715.4350556293, 29966.7509482073, 23645.372107655, 42288.6478926083, 
22454.28959872, 37888.643411857, 26591.7884353541)), row.names = c(NA, 
-100L), class = c("data.table", "data.frame"))

Thanks in advance for your help.

CodePudding user response:

Does this do the trick? (you can easily modify y position using this:)

df %>%
  ggplot(aes(x=value,color=variable)) 
  facet_grid(~variable,scales = 'free') 
  geom_density(aes(y=..scaled..)) 
  geom_boxplot(aes(x=value,fill=variable, y= 0.37),
               alpha=0.2,outlier.color = 'black',outlier.shape = 3,outlier.alpha = 1)

output:

enter image description here

  • Related