将图例添加到R ggplot2中的geom_rect [英] Adding legend to geom_rect in R ggplot2
本文介绍了将图例添加到R ggplot2中的geom_rect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
按照此
如何为阴影添加图例?
解决方案
我们可以将填充内容插入 aes
s内,并为其指定 scale
:
库(ggplot2)ggplot(data,aes(x = Time,y = Average,colour = Test))+geom_rect(aes(aes(xmin = 20,xmax = 30,ymin = -Inf,ymax = Inf,fill ="What"),colour = NA,alpha = 0.05)+geom_errorbar(aes(ymin = Average-se,ymax = Average + se),width = 0.2)+geom_line()+geom_point()+scale_fill_manual('突出显示此,值=粉红色",指南= guide_legend(override.aes =列表(alpha = 1)))
Consider a dataset as in this question. A shading can be plotted using geom_rect
in ggplot2
as follows.
data <- structure(list(Time = c(20L, 40L, 60L, 80L, 100L, 120L, 20L,
40L, 60L, 80L, 100L), Average = c(5.8, 6.1, 6.4, 6.7, 7, 7.7,
8.47, 9.317, 10.2487, 11.27357, 12.40093), Test = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Control",
"Exp"), class = "factor"), n = c(9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L), se = c(0.12, 0.145, 0.188, 0.99, 0.44, 0.32,
0.5, 0.88, 0.9, 0.33, 0.456)), .Names = c("Time", "Average",
"Test", "n", "se"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11")
ggplot(data, aes(x=Time, y=Average, colour=Test)) +
geom_rect(aes(xmin=20,xmax=30,ymin=-Inf,ymax=Inf),fill="pink",colour=NA,alpha=0.05) +
geom_errorbar(aes(ymin=Average-se, ymax=Average+se), width=0.2) +
geom_line() +
geom_point()
How to add a legend for the shading ?
解决方案
We can insert the fill inside the aes
s and give it a scale
:
library(ggplot2)
ggplot(data, aes(x=Time, y=Average, colour=Test)) +
geom_rect(aes(xmin=20,xmax=30,ymin=-Inf,ymax=Inf,fill="What"),colour=NA,alpha=0.05) +
geom_errorbar(aes(ymin=Average-se, ymax=Average+se), width=0.2) +
geom_line() +
geom_point() +
scale_fill_manual('Highlight this',
values = 'pink',
guide = guide_legend(override.aes = list(alpha = 1)))
这篇关于将图例添加到R ggplot2中的geom_rect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文