r - 用 R (ggplot) 绘制二维箱线图

标签 r ggplot2

我已经用 2 个参数对数据进行了分组:

set.seed(1)
dat <- data.frame( xx=sample(10,9),
                   yy=sample(20,9),
                   group=c('A','B', 'C')  )

我可以为每个维度绘制箱线图:
ggplot(dat, aes(x=group, y=yy, fill=group)) + geom_boxplot() 
ggplot(dat, aes(x=group, y=xx, fill=group)) + geom_boxplot() + coord_flip()



现在我将结合这些并绘制反射(reflect)两个变量数据的箱线图,如下所示:

(此图像是在图形编辑器中手动制作的)

有没有什么简单的方法可以绘制这种箱线图?

最佳答案

这不是很好的表示方式。这可能非常令人困惑。通常这些数据是这样表示的。

datPlot <- melt(dat)
ggplot(datPlot, aes(x=group, y=value, fill=variable)) + geom_boxplot() 

enter image description here

或者如果两个变量的范围非常不同,您可以尝试分面
ggplot(datPlot, aes(x=group, y=value, fill=group)) + geom_boxplot()
 +facet_wrap(~variable,scale="free_y")

enter image description here

或这个
ggplot(dat, aes(x=xx, y=yy, color=group)) + geom_point(size=3) 

enter image description here

关于r - 用 R (ggplot) 绘制二维箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30072525/

相关文章:

r - 如何使直方图填充选项起作用

r - 使用 grid.arrange 创建的 ggplot 中标题上方的边距

如果您已经熟悉 R,则学习 SAS 的资源

R编程: How to find a difference in value for every two consecutive dates,给定特定ID

r - 如何覆盖ggplot2的轴格式?

r - ggplot 2中的多个条形图

r - 根据 R Shiny 中的其他选择动态更新两个 selectInput 框

在 R 中使用 shell() 运行 .bat 文件

r - R 中按组运行的数百个线性回归

r - 从 ggpairs 图中打印单行