ggplot中的geom_bar - “旋转和翻转”?

7
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()
1个回答

12

可能不完全符合您所描述的,但或许已经足够接近了?

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

诀窍在于将xlimylim参数的顺序颠倒。您的轴位置仍然是底部和左侧...

enter image description here


@nzcoops 这有帮助吗? - Andrie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接