r - 调整图例的宽度以进行连续变量

标签 r ggplot2 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")
# 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


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

最佳答案

代替功能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"))

关于r - 调整图例的宽度以进行连续变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14650190/

相关文章:

r - 将参数传递给ggplot

r - 如何在R中找到日期的 "origin"

r - 将换行符分隔的文本文件转换为 csv 文件

r - 按R数据帧中数据 block 的长度生成随机数

r - highcharts 中的分面函数

r - 按更高的分类、丝和属对图例进行分组? ggplot2

r - 如何在 R 中使用具有日期范围的 intersect() 函数

python - 图例在保存时被切断 - Matplotlib

javascript - jquery cookies 和图例状态

python - 创建后如何修改matplotlib图例?