在 ggplot2 中翻转和保持图表的纵横比
Flipping and maintaining aspect ratio of a chart in ggplot2
我想创建一个水平直方图,并使用 ggplot2
调整图表纵横比。
例如,假设我的图表是 dia <- ggplot(diamonds, aes(x=color)) + geom_bar()
。
我可以使用 dia + coord_flip()
将其翻转为水平。
我还可以调整宽高比,例如dia + coord_fixed(ratio=.001)
.
但是当我将它们组合起来时dia + coord_flip()+ coord_fixed(ratio=.001)
,图表不再是水平的。
有什么方法可以使用 ggplot2
实现我想要的效果吗?
See this answer on the ggplot2 mailing list :
You can only use one coord_*() function on a given ggplot since it
changes the coordinate system after everything else has been done. To
change the aspect ratio, you can use the corresponding argument in the
theming system:
+ coord_flip() + theme(aspect.ratio = 1)
我想创建一个水平直方图,并使用 ggplot2
调整图表纵横比。
例如,假设我的图表是 dia <- ggplot(diamonds, aes(x=color)) + geom_bar()
。
我可以使用 dia + coord_flip()
将其翻转为水平。
我还可以调整宽高比,例如dia + coord_fixed(ratio=.001)
.
但是当我将它们组合起来时dia + coord_flip()+ coord_fixed(ratio=.001)
,图表不再是水平的。
有什么方法可以使用 ggplot2
实现我想要的效果吗?
See this answer on the ggplot2 mailing list :
You can only use one coord_*() function on a given ggplot since it changes the coordinate system after everything else has been done. To change the aspect ratio, you can use the corresponding argument in the theming system:
+ coord_flip() + theme(aspect.ratio = 1)