[英]geom_rect and legend that shows the width of the rectangles
這是一個非常簡單的腳本
library(ggplot2)
df <- data.frame(xmin = c(1, 2.5, 3), xmax = c(2, 3.5, 5), ymin=c(1,1.5, 3), ymax = c(2,2.5,4), size = c(1,1,2), fill = c("red", "orange", "blue"))
p <- ggplot()
p <- p + geom_rect(data = df, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, size = factor(size), fill = fill))
p <- p + scale_size_manual(values = c(1,2), guide = guide_legend(keywidth = 4, override.aes = list(colour = "black")))
p
該腳本創建以下圖表...
我要實現的目標是添加一個可反映矩形寬度的圖例(指南)! 僅2條簡單的行作為“ legend key”,而鍵的寬度為1或2。
我被卡住了,所以任何提示都值得贊賞
由於您指定的是xmin / xmax和ymin / ymax,因此我不認為size參數在這里有任何作用
my_dat <- data.frame(xmin = c(1, 2.5, 3), xmax = c(2, 3.5, 5),
ymin=c(1,1.5, 3), ymax = c(2,2.5,4),
size = c(1,1,2), fill = c("red", "orange", "blue"))
# size doesn't do anything for geom_rect. compare this...
ggplot(my_dat) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax,
size = factor(size), fill = fill))
# ...and this. same!
ggplot(my_dat) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax,
fill = fill))
# try geom_linerange
ggplot(my_dat) +
geom_linerange(aes(x=(xmin+xmax)/2, ymin=ymin, ymax=ymax, color=fill,
size=factor(size))) +
scale_size_manual(values=c(20, 50)) +
xlim(1, 5)
經過編輯,使結果更接近原始圖,盡管這有點古怪。
並且如果添加+ theme(legend.direction = 'horizontal')
:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.