r - 在水平图例标题的标题后添加换行符
问题描述
当 时theme(legend.position = "top")
,图例是水平的并且可能有点太宽。
这是一个例子:
library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_point() +
xlim(5,6) +
coord_fixed() +
theme(legend.position = "top")
由reprex 包(v0.3.0)于 2021 年 1 月 14 日创建
为了减少图例的宽度,我想在“物种”标题后添加一个换行符。
我知道guides(guide_legend(ncol=2,nrow=2,byrow=TRUE))
,它非常好,但会产生不同的输出。
解决方案
我们可以使用in中的title.position
参数:guide_legend
scale_colour_discrete
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_point() +
xlim(5,6) +
coord_fixed() +
theme(legend.position = "top")+
scale_colour_discrete(guide = guide_legend(title.position = "top"))
推荐阅读
- python - 使用递归的回文
- django - Django REST (DRF) - api_view 中的错误处理
- c++ - 试图在主函数中引用类的预定义实例
- java - 使用 3rd 方 API 调用测试服务
- export-to-csv - 为什么导出到 csv 后日期错误?
- php - 当我在索引中调用它时,我的另一个需要一旦 php 页面出错
- java - JPA CriteriaBuilder 计数返回错误数据
- r - 在R中合并数据框而不重复
- node.js - 邮递员的简单删除请求不起作用
- javascript - 如何更新对象数组中的 React 状态?如果 id 存在则更新,否则追加