如何旋转ggplot2中的轴标签? [英] How to rotate the axis labels in ggplot2?

查看:1274
本文介绍了如何旋转ggplot2中的轴标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggplot2生成的以下图形

我将 finalPlot 作为ggplot对象。添加使用的标签

  finalPlot + stat_bin()+ scale_x_continuous('解决方案成本')+ scale_y_continuous('解决方案数量' )`

如何更改y轴标签的方向以使其看起来水平并且如果可能跨越两行,如

  
解决方案的数量

解决方案

对于轴文本的旋转角度,您需要使用 element_text()。有关示例,请参阅这篇文章。对于分隔两行,我会在要放置换行符的字符串中添加一个\\\



这将为y轴文本设置正确的方向并强制换行:

  finalPlot + ylab (Number of \solutions)+ 
theme(axis.title.y = element_text(angle = 0))


I have the following graph that I generated using ggplot2

I had finalPlot as the ggplot object. To add labels I used

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

Number of
Solutions

解决方案

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.

This will set the correct orientation for the y axis text and force a line break:

finalPlot + ylab("Number of\nSolutions") + 
    theme(axis.title.y = element_text(angle = 0))

这篇关于如何旋转ggplot2中的轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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