我想在x轴上彼此相邻创建四个箱形图。我对ggplot中的箱形图没有太多经验,我不确定如何构造数据以获取与x轴相关的箱形图及其相关箱形图。
我对这种情况下如何处理y轴特别感兴趣。
col1<- c(0.43,0.78,-0.26,0.5,0.6,0.78,-0.2,0.1)
col2<- c(0.23,0.48,-0.76,0.1,0.9,0.73,-0.7,0.2)
col3<- c(0.83,0.18,-0.96,0.9,0.2,0.18,-0.79,0.3)
col4<- c(0.23,0.98,-0.16,0.4,0.3,0.49,-0.2,0.2)
test<-data.frame(col1,col2,col3,col4)
答案 0 :(得分:1)
答案 1 :(得分:1)
在ggplot2
中,您需要一个“长”,而不是宽数据格式。完整示例:
library(ggplot2)
col1<- c(0.43,0.78,-0.26,0.5,0.6,0.78,-0.2,0.1)
col2<- c(0.23,0.48,-0.76,0.1,0.9,0.73,-0.7,0.2)
col3<- c(0.83,0.18,-0.96,0.9,0.2,0.18,-0.79,0.3)
col4<- c(0.23,0.98,-0.16,0.4,0.3,0.49,-0.2,0.2)
df <- rbind(
data.frame(name='col1', values=col1),
data.frame(name='col2', values=col2),
data.frame(name='col3', values=col3),
data.frame(name='col4', values=col4))
ggplot(df, aes(x=name, y=values)) + geom_boxplot()
此处df
代表数据的“长”格式,其中每个度量都位于其自己的行上。