【问题标题】:Adjusting the width of legend for continuous variable调整连续变量的图例宽度
【发布时间】:2013-01-16 23:52:57
【问题描述】:

我在下面有一个脚本来说明我的问题:

temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE)
chart <- ggplot(data=temp.df,aes(x=x,y=y))
chart <- chart + geom_line(aes(colour=z))
chart <- chart + scale_colour_continuous(low="blue",high="red")
chart <- chart + theme(legend.position="bottom")
# so far so good, but I think the legend positioned at bottom with such a small size is a waste of space, so I want to "widen" it using the line below...
chart <- chart + guides(colour=guide_legend(keywidth=5,label.position="bottom"))
# oops, it changed to legend for categorical variable

如何扩大位于底部的“连续变量”图例?

【问题讨论】:

  • 由于@Didzis 给出了您正在寻找的答案,我想我会给出您可以设置的可能属性的链接。 Here it is

标签: r ggplot2 legend


【解决方案1】:

你应该使用函数theme()而不是函数guides()并设置legend.key.width=

temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE)
chart <- ggplot(data=temp.df,aes(x=x,y=y))
chart <- chart + geom_line(aes(colour=z))
chart <- chart + scale_colour_continuous(low="blue",high="red")
chart <- chart + theme(legend.position="bottom")
chart <- chart + theme(legend.key.width=unit(3,"cm"))

【讨论】:

    【解决方案2】:

    您可以在代码中使用guide_colourbar 而不是guide_legend

    temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE)
    chart <- ggplot(data=temp.df,aes(x=x,y=y))
    chart <- chart + geom_line(aes(colour=z))
    chart <- chart + scale_colour_continuous(low="blue",high="red")
    chart <- chart + theme(legend.position="bottom")
    chart + guides(colour=guide_colourbar(barwidth=30,label.position="bottom"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 2012-10-26
      • 2020-06-04
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      相关资源
      最近更新 更多