Ggplot2 width of boxplot

I tried to make two separate sections that I want to represent side by side in my poster (I need to make them separate and not use facet_wrap). On one of the sites there are several boxes, and on the second site there is one. How can I manipulate the width of the drawers so that the second drawer is the same size as the width of any of the individual drawers in graph 1 when I put these two sections side by side? Playable example:

tvalues <- sample(1:10000,1200)
sex <- c(rep('M',600),rep('F',600))
region <- c('R1','R2','R3','R4','R5')
df1 <- data.frame(tvalues,sex,region)

tvalues2 <- sample(1:10000,200)
sex2 <- sample(c('M','F'),200,replace=T)
region2 <- 'R6'
df2 <- data.frame(tvalues2,sex2,region2)

p1 <- ggplot(data=df1,aes(x=region,y=tvalues,color=sex)) + 
geom_boxplot(width=0.5)
p2 <- ggplot(data=df2,aes(x=region2,y=tvalues2,color=sex2)) + 
geom_boxplot(width=0.5)

Plot 1 plot 1:

Plot2 plot 2:

+4
source share
1 answer

I suggest dividing the width of the boxes in the second chart by the number of categories regionin the first chart.

p2 <- ggplot(data=df2,aes(x=region2,y=tvalues2,color=sex2)) + 
geom_boxplot(width=0.5/length(unique(df1$region)))

enter image description here

+4
source

Source: https://habr.com/ru/post/1688093/


All Articles