扩大ggplot2图例

3

我用 Rggplot2 制作了这个图:

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


请查看此处可更改的图例主题:http://docs.ggplot2.org/0.9.2.1/theme.html - user20650
1个回答

6
尝试一下
# 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_legend这里

这将给你:

enter image description here

您可以使用keywidthkeyheight来控制图例在两个方向上的“拉伸”程度。通过title.positiondirection等进一步优化图例。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接