gpt4 book ai didi

r - 放大 ggplot2 图例

转载 作者:行者123 更新时间:2023-12-04 02:11:54 24 4
gpt4 key购买 nike

我在 R 中用 ggplot2 制作了这个图 enter image description here

由以下代码绘制:

ggplot(mtcars) + 
geom_smooth(fill='grey', alpha=0.3, span=0.1, aes(x=mpg, y=hp, color='AAA',linetype='AAA')) +
geom_smooth(fill='grey', alpha=0.3, span=0.9, aes(x=mpg, y=hp, color='BBB',linetype='BBB')) +
scale_colour_manual(name='test', values=c('AAA'='chocolate', 'BBB'='yellow')) +
scale_linetype_manual(name='test', values=c('AAA'='dashed','BBB'='solid')) +
theme_minimal() +theme(legend.position = "top")

问题:从图例中,很难理解“AAA”线是虚线,因为框太小了。

如何放大?

我很想有类似的东西: enter image description here

最佳答案

尝试

# create your legend guide
myguide <- guide_legend(keywidth = unit(3, "cm"))
# now create your graph
ggplot(mtcars) +
geom_smooth(fill='grey', alpha=0.3, span=0.1,
aes(x=mpg, y=hp, color='AAA',linetype='AAA')) +
geom_smooth(fill='grey', alpha=0.3, span=0.9,
aes(x=mpg, y=hp, color='BBB',linetype='BBB')) +
scale_colour_manual(name='test',
values=c('AAA'='chocolate', 'BBB'='yellow'),
guide = myguide) +
scale_linetype_manual(name='test',
values=c('AAA'='dashed','BBB'='solid'),
guide = myguide) +
theme_minimal() + theme(legend.position = "top")

参见 ?guide_legendhere .

这会给你

enter image description here

您可以使用 keywidthkeyheight 来控制键向两个方向“延伸”的程度。使用 title.positiondirection 等,您可以进一步微调图例。

请注意,由于您有多个合并的图例,因此您需要指定所有合并比例的指南。我通过首先将外部指南创建为对象来简化这一过程。

关于r - 放大 ggplot2 图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37286297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com