r - 如何将 ggplot2 colourbar 的宽度指定为面板宽度的一半?
问题描述
使用 ggplot2,有没有办法指定相对于绘图面板大小缩放的颜色条宽度?
即可以向legend.key.width 提供什么以使键的相对宽度保持不变,而不管绘图的大小如何?
ggplot(mpg) +
geom_point(aes(cty, hwy, color = year)) +
theme(legend.position = "bottom",
legend.key.width = unit(0.1, "npc")) # not relative
# or could it be done with a custom theme?
my_theme <- function() {
theme_bw() %+replace%
theme(legend.position = "bottom", ?)
ggplot(mpg) +
geom_point(aes(cty, hwy, color = year)) +
my_theme()
解决方案
您可以查询设备大小并进行legend.key.width
相应更改。所以你的my_theme
功能可能是:
my_theme <- function() {
theme_bw() +
theme(legend.position = "bottom",
legend.key.width = unit(dev.size()[1] / 10, "inches"))
}
所以现在当我们这样做时:
ggplot(mpg) +
geom_point(aes(cty, hwy, color = year)) +
my_theme()
我们得到:
但是如果我们缩小情节并再次调用上面的代码,我们会得到
或者,如果我们把它弄得很宽并再次调用它,我们会得到:
推荐阅读
- python - 杀死在 python subprocess.Popen 中打开并被 subprocess.kill 杀死的进程后,进程状态为 sombi
- reactjs - 安装和卸载组件
- c# - 除非必要,否则如何不评估三元运算符的第三部分
- python - 调用 pandas.get_dummies() 后的疑惑结果
- java - Wildfly 中奇怪的请求/响应机构?
- c# - c#服务器套接字数据没有正确接收
- angularjs - 如果 Angular Js 中的控制器相同,如何将数据从 1 个视图传递到另一个视图?
- python - Python Kivy 更新 KV 文件中子部件的文本
- python - Firefox 不会多次更新视图。(姜戈)
- aframe - 标记处于垂直位置时如何配置ar.js