本文介绍了ggplot2 boxplot的翻转坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想翻转ggplot2箱线图的坐标,在此我也进行坐标转换。

I would like to flip the coords for a ggplot2 boxplot where I also do a coordinate transformation.

library(ggplot2)

dat = data.frame(group = factor(c(rep(1,3),rep(2,6))),
                 vals   = c(c(0.1,2.25,1000), c(0.11,0.21,0.21,4.55,5.06,29.48)))


   ggplot(dat,aes(group,vals)) + geom_boxplot()
   ggplot(dat,aes(group,vals)) + geom_boxplot()  + coord_trans(y="log10")

如果我现在仅添加 + coord_flip(),则将丢失轴的对数缩放比例。.

If I just add now "+ coord_flip()", the log scaling of the axis is lost..

 ggplot(dat,aes(group,vals)) + geom_boxplot()  + coord_trans(y="log10") + coord_flip()

有什么方法可以翻转坐标吗?

Any way to achieve flipping of coords?

感谢您的任何评论!

最好,
Stefanie

Best,Stefanie

推荐答案

也许可以解决。您可以使用 viewport()翻转绘图。编辑:使用包 cowplot ,可以切换y轴。

maybe there is workaround. You can flip your plot using viewport(). using package cowplot, you can switch the y axis.

library(ggplot2)
library(grid)
library(cowplot)
p <- ggplot(dat,aes(group,vals)) + geom_boxplot()  + coord_trans(y="log10") + theme(axis.text.x = element_text(angle = 90, hjust = 1), axis.title.x = element_text(angle = 90, hjust = 1), axis.text.y = element_text(angle = 90, hjust = 1))

pp <- ggdraw(switch_axis_position(p, axis = 'y'))

grid.newpage()
print(pp, vp = viewport(angle = -90, width = 0.7, height = 0.8))

这篇关于ggplot2 boxplot的翻转坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:16