一个图表中多个箱图的显示不正确

时间:2018-02-20 19:53:57

标签: r boxplot

我希望看到所有的箱形图都会在x轴上的25,85和125处重叠,但它们不是。请运行代码进行复制,我无法附加图像。非常感谢你。

library("ggplot2")
library("plyr")

df<-data.frame(T = c(25, 25, 25, 25, 25, 85, 85, 85, 125, 125, 125), V =c(1.03, 1.06, 1.1,1.08,1.87,1.56,1.75,1.82, 1.85, 1.90), type=c(2,2,2,2,2,2,2))
df1<-data.frame(T = c(25, 25,25,85, 85, 85, 85, 125, 125,125), V =c(1.13, 1.24,1.3,1.17, 1.66,1.76,1.89, 1.90, 1.95,1.97), type=c(5,5,5,5,5,5,5))
df2<-data.frame(T = c(25, 25, 25, 85, 85,85,125, 125,125), V =c(1.03, 1.06, 1.56,1.75,1.68,1.71,1.82, 1.85,1.88), type=c(7,7,7,7,7,7))

main <- rbind(df,df1,df2)
main$type <- as.factor(main$type)

main <- transform(main, type = revalue(type,c("2"="type2", "5"="type5", "7" = "type7")))

main$T <- as.factor(main$T)
ggplot(main, aes(T, V))+geom_boxplot(width=0.5/length(unique(main$type)),aes(color=type),size=.3)

1 个答案:

答案 0 :(得分:1)

首先,您需要对dfs进行一些更改。 a)并非所有变量都具有相同的长度。 b)不要将变量命名为T,它可能会将R与布尔值TRUE混淆。

所以也许你想要的是以下内容?

df<-data.frame(U = c(25, 25, 25, 25, 25, 85, 85, 85, 125, 125), 
               V =c(1.03, 1.06, 1.1,1.08,1.87,1.56,1.75,1.82, 1.85, 1.90), type=c(2,2,2,2,2,2,2,2,2,2)) 

df1<-data.frame(U = c(25, 25,25,85, 85, 85, 85, 125, 125,125), 
                V =c(1.13, 1.24,1.3,1.17, 1.66,1.76,1.89, 1.90, 1.95,1.97), type=c(5,5,5,5,5,5,5,5,5,5)) 

df2<-data.frame(U = c(25, 25, 25, 85, 85,85,125, 125,125), 
                V =c(1.03, 1.06, 1.56,1.75,1.68,1.71,1.82, 1.85,1.88), type=c(7,7,7,7,7,7,7,7,7))

main <- rbind(df,df1,df2) 
main$type <- as.factor(main$type)

main <- transform(main, type = revalue(type,c("2"="type2", "5"="type5", "7" = "type7")))

main$U <- as.factor(main$U) 

ggplot(main, aes(U, V,color=type))+geom_boxplot(width=0.5/length(unique(main$type)),size=.3,position="identity")

enter image description here