具有独立轴R的背对背条形图 [英] back-to-back barplot with independent axes R

查看:49
本文介绍了具有独立轴R的背对背条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制背对背的条形图,但是每一侧都在一个独立的轴上.我可以通过取一组负数来将它们背对背绘制,但这会使它们处于相同的访问方式,并且因为p值较小,所以它们的条形几乎无法表示.

I am wanting to plot back-to-back barplot, however each side is on an independent axes. I can plot them back to back by taking the negative of one set, but that leaves them on the same access and because pvalues are smaller their bars are barely represented.

library(ggplot2)
df <-structure(list(Description = c("a", "b", "c", "d", "e", "f", 
    "g", "h", "a", "b", "c", "d", "e", "f", "g", "h"), test = c("size", 
    "size", "size", "size", "size", "size", "size", "size", "p", 
    "p", "p", "p", "p", "p", "p", "p"), value = c(0.1, 0.1, 0.125, 
    0.1, 0.075, 0.1, 0.075, 0.125, 0.000230705311441713, 0.000314488619269942, 
    0.00106639822095382, 0.00108290238851994, 0.00114723539549198, 
    0.00160204850890075, 0.0019276388745184, 0.00320371567547557)), .Names = c("Description", 
    "test", "value"), row.names = c(NA, -16L), class = "data.frame")

df$value[df$test == 'p'] <- -(df$value[df$test == 'p'])

ggplot(df, aes(x=Description, y= value, group=test, fill=test)) + geom_col() +coord_flip()

理想情况下,我希望每个组都在独立的轴上,以便条形点在零点处(在绘图区域的中间)相交,但是在本例中,ylim的标度不同,因此ylim类似于ylim(0,0.13)和pvaluec(0,0.0035)

Ideally I would like each group on independent axes so that the bars meet at zero (in the middle of the plot region) but be on different scales for this example ylim would be something like ylim(0,0.13) and for pvalue c(0, 0.0035)

推荐答案

您可以通过以下方式进行操作:调整构面以消除构面之间的间距:

You can do this by using facets, and tweaking to remove the spacing between facets:

ggplot(df, aes(x=Description, y= value, fill=test)) + 
    facet_wrap(~ test, scales = "free_x") + 
    geom_col() + 
    coord_flip() +
    scale_y_continuous(expand = c(0, 0)) +
    theme(panel.spacing.x = unit(0, "mm"))

这可能会导致轴标签出现一些问题,要解决这些问题会有些棘手.在这种情况下,在小平面之间保留一些空间可能会更容易,但要以没有在中间相遇为代价.

It might create some issues with axis labels, and these would be a bit tricky to solve. In that case, it might be easier to keep some space between the facets, at the expense of not having the bars meet in the middle.

输出:

PS:您还可以使用以下方法删除负轴标签:

PS: you can also remove the negative axis labels with something like:

scale_y_continuous(
    expand = c(0, 0), 
    labels = function(x) signif(abs(x), 3)
)

这篇关于具有独立轴R的背对背条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆