我在下面有一个脚本来说明我的问题:
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
如何加宽位于底部的“连续变量”图例?
答案 0 :(得分:20)
您应该使用函数guides()
而不是函数theme()
并设置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"))
答案 1 :(得分:9)
您可以在代码中使用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"))