这是由以下代码绘制的:
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")
Run Code Online (Sandbox Code Playgroud)
问题:从图例中很难理解“AAA”线是虚线,因为盒子太小了。
我怎样才能放大它?
尝试
# 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")
Run Code Online (Sandbox Code Playgroud)
看到?guide_legend和在这里。
这会给你
您可以使用keywidth和keyheight来操纵键向两个方向“延伸”的程度。使用title.position、direction等,您可以进一步微调图例。
请注意,由于您有多个合并的图例,您需要指定所有合并比例的指南。我通过首先将外部指南创建为对象来简化了这一点。