I have the following graph that I generated using ggplot2
我有如下图,是我用ggplot2生成的。
I had finalPlot
as the ggplot object. To add labels I used
我把finalPlot作为ggplot对象。添加我使用的标签。
finalPlot + stat_bin() + scale_x_continuous('Solution Cost') + scale_y_continuous('Number of Solutions')`
How can I change the orientation of the y axis label to make it appear horizontal and if possible span it across two lines like
如何改变y轴标签的方向使它看起来是水平的,如果可能跨越两行?
Number of
Solutions
2 个解决方案
#1
5
For the rotation angle of the axis text you need to use element_text()
. See this post on SO for some examples. For spacing over two lines I would add a "\n"
on the location in the string where you want to put the newline.
对于axis文本的旋转角度,您需要使用element_text()。看看这个帖子,一些例子。对于间隔超过两行,我将在字符串中添加一个“\n”,在这个位置上您想要放置换行符。
This will set the correct orientation for the y axis text and force a line break:
这将为y轴文本设置正确的方向,并强制换行:
finalPlot + ylab("Number of\nSolutions") +
theme(axis.title.y = element_text(angle = 0))
#2
13
The syntax has changed in recent versions of ggplot2; if you try the above answer, you'll get
在ggplot2的最近版本中,语法发生了变化;如果你尝试以上的答案,你会得到。
Error: Use 'theme' instead. (Defunct; last used in version 0.9.1)
错误:使用“主题”。(破产;最后使用的版本0.9.1)
These days you should use
这些天你应该使用。
finalPlot + ylab("Number of\nSolutions") + theme(axis.title.y = element_text(angle=0))
#1
5
For the rotation angle of the axis text you need to use element_text()
. See this post on SO for some examples. For spacing over two lines I would add a "\n"
on the location in the string where you want to put the newline.
对于axis文本的旋转角度,您需要使用element_text()。看看这个帖子,一些例子。对于间隔超过两行,我将在字符串中添加一个“\n”,在这个位置上您想要放置换行符。
This will set the correct orientation for the y axis text and force a line break:
这将为y轴文本设置正确的方向,并强制换行:
finalPlot + ylab("Number of\nSolutions") +
theme(axis.title.y = element_text(angle = 0))
#2
13
The syntax has changed in recent versions of ggplot2; if you try the above answer, you'll get
在ggplot2的最近版本中,语法发生了变化;如果你尝试以上的答案,你会得到。
Error: Use 'theme' instead. (Defunct; last used in version 0.9.1)
错误:使用“主题”。(破产;最后使用的版本0.9.1)
These days you should use
这些天你应该使用。
finalPlot + ylab("Number of\nSolutions") + theme(axis.title.y = element_text(angle=0))