[英]Plotting two boxplots at one x position using R and ggplot2
我想在ggplot2
中使用ggplot2
在R
之上/之下繪制多個箱圖,而不是在R
中ggplot2
。 這是一個例子:
library("ggplot2")
set.seed(1)
plot_data<-data.frame(loc=c(rep(1,200),rep(2,100)),
value=c(rnorm(100,3,.5),rnorm(100,1,.25),2*runif(100)),
class=c(rep("A",100),rep("B",100),rep("C",100)))
ggplot(plot_data,aes(x=loc,y=value,group=class)) +
geom_boxplot(fill=c("red","green","blue"))
這導致以下情節:
如您所見,藍色框圖以其loc
值(2.0)為中心,而紅色和綠色僅具有寬度的一半,並繪制在其共享的loc
值(1.0)的左側和右側。 我想讓它們都與藍色寬度相同,並將它們直接繪制在彼此之上。
我怎樣才能做到這一點?
請注意,我確信箱形圖對於我要顯示的數據不會重疊,就像它們不適用於給定的示例數據一樣。
使用position = "identity"
:
ggplot(plot_data,aes(x=loc,y=value,group=class)) +
geom_boxplot(fill=c("red","green","blue"),position = "identity")
geom_boxplot
的默認geom_boxplot
是使用position = "dodge"
。
主要討論是: 這里
簡而言之,可以使用geom_boxplot(position=position_dodge(0))
。 可以指定改變'position_dodge'值的方框之間的距離。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.