Adjusting the width of legend for continuous variable

前端 未结 2 1367
栀梦
栀梦 2020-12-10 12:33

I have a script below to illustrate my question:

temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE)
chart <- ggplot(data=temp.df,aes         


        
相关标签:
2条回答
  • 2020-12-10 12:57

    You can use guide_colourbar instead of guide_legend in your code :

    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"))
    

    enter image description here

    0 讨论(0)
  • 2020-12-10 13:06

    Instead of function guides() you should use the function theme() and set the 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"))
    
    0 讨论(0)
提交回复
热议问题