r - ggplot geom_bar- 'rotate and flip'?

标签 r ggplot2

test <- data.frame(
    y=seq(18,41,1),
    x=24:1
)

ggplot(test, aes(y=y, x=x)) + geom_bar(stat="identity", aes(width=1)) + 
    opts( axis.text.x = theme_blank(), axis.ticks.x = theme_blank()) + 
    scale_x_continuous(breaks=NULL) + 
    coord_cartesian(ylim = c(17, 42))

就旋转和翻转而言,我希望此图中的y轴位于顶部,而x轴位于右侧下方。因此,条形图从图的右侧“伸出”,最长/最高的在顶部,最短的在底部。如果将其顺时针旋转90度,然后在垂直线上翻转即可实现此目的。

coord_flip()和scale_y_reverse()在正确的路径上有所下降。

编辑:

我想这很接近,只需要将y轴移到图形的顶部即可。
ggplot(test, aes(y=y, x=x)) + geom_bar(stat="identity", aes(width=1)) + 
    opts(axis.text.y = theme_blank(), axis.ticks.y = theme_blank()) + 
    scale_x_continuous(breaks=NULL) + scale_y_reverse() + coord_flip()  + scale_x_reverse()

最佳答案

不完全是您所描述的内容,但也许足够接近?

ggplot(test, aes(y=y, x=x)) + geom_bar(stat="identity", aes(width=1)) +
  coord_flip() +
  xlim(24, 1) +
  ylim(42, 0)

诀窍是使xlimylim参数具有相反的顺序。您的轴位置仍在底部和左侧...

关于r - ggplot geom_bar- 'rotate and flip'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11466131/

相关文章:

r - 在循环中将 data.frame 行绑定(bind)到另一个 data.frame 的有效方法?

r - ggplot2:多个带有框和标签的图

R:使用 'if' 的函数通过引用更新数据表列

r - 用 R 中的行更改 pch

r - 如何从R中其他列命名的列中选择数据

r - 在 ggplot 中,如何为 stat_bin2d 获取两个图例 ("gradient"类型)?

r - ggplot2 按因子对 geom_point 进行着色,但基于所有数据对 geom_smooth 进行着色

r - 调整facet的相对空间(不考虑坐标空间)

r - 如何在两种不同类型的几何图形上创建图例ggplot2

r - sf LINESTRING 的颜色部分(按变量)