我正在使用ggplot制作图形,希望我的图例没有背景颜色。下面的代码可以通过最后一行legend.key=element_blank()来移除背景颜色:
#For Testing
ggplot(data= Data, aes(x = Date, y = Elev, group = Well, colour = Well)) +
geom_line(size = 0.75) +
xlab("") + ylab("Elevation (ft.)") +
scale_color_brewer(palette = "Spectral") +
scale_x_date(breaks = date_breaks("1 year"),
date_labels = ("%b %Y")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line( size=.1, color="gray"),
panel.grid.minor = element_blank(),
axis.line.x = element_line(color = "black"),
axis.line.y = element_line(color = "black"),
panel.background = element_rect(fill = "white"),
legend.key=element_blank())
然而,我想使用geom_rect给我的图形染色。问题是,当我在图形的某些区域添加薰衣草色的阴影时,图例的背景也变成了薰衣草色。实际上,似乎对于我用geom_rect制作的每个薰衣草色框,图例背景都会变成更深的薰衣草色......以下代码是上面的代码加上我的geom_rect命令:
ggplot(data= Data, aes(x = Date, y = Elev, group = Well, colour = Well)) +
geom_rect(data = Data,
aes(xmin = as.Date("2003-08-11", "%Y-%m-%d"),
xmax = as.Date("2003-08-14", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2004-04-29", "%Y-%m-%d"),
xmax = as.Date("2004-12-20", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2005-04-07", "%Y-%m-%d"),
xmax = as.Date("2005-12-12", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2006-04-21", "%Y-%m-%d"),
xmax = as.Date("2006-12-08", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2007-03-27", "%Y-%m-%d"),
xmax = as.Date("2007-12-03", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2008-04-09", "%Y-%m-%d"),
xmax = as.Date("2008-11-28", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2009-05-11", "%Y-%m-%d"),
xmax = as.Date("2009-11-26", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2010-04-10", "%Y-%m-%d"),
xmax = as.Date("2010-10-10", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2011-04-19", "%Y-%m-%d"),
xmax = as.Date("2011-11-18", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2012-04-12", "%Y-%m-%d"),
xmax = as.Date("2012-11-14", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2013-05-09", "%Y-%m-%d"),
xmax = as.Date("2013-11-12", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2014-05-23", "%Y-%m-%d"),
xmax = as.Date("2014-12-01", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2015-04-10", "%Y-%m-%d"),
xmax = as.Date("2015-11-28", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_rect(data = Data,
aes(xmin = as.Date("2016-04-01", "%Y-%m-%d"),
xmax = as.Date("2016-12-31", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "lavender",
linetype = 0,
alpha = 0.05) +
geom_line(size = 0.75) +
xlab("") + ylab("Elevation (ft.)") +
scale_color_brewer(palette = "Spectral") +
scale_x_date(breaks = date_breaks("1 year"),
date_labels = ("%b %Y")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line( size=.1, color="gray"),
panel.grid.minor = element_blank(),
axis.line.x = element_line(color = "black"),
axis.line.y = element_line(color = "black"),
panel.background = element_rect(fill = "white"),
legend.key=element_blank())
有人能帮我将图例中的薰衣草背景移除吗?以下是代码头:
Date Well Elev
1 2002-05-23 MW-3 929.04
2 2002-05-29 MW-3 929.39
3 2002-05-31 MW-3 929.37
4 2002-06-05 MW-3 929.36
5 2002-06-12 MW-3 NA
6 2002-06-13 MW-3 929.47
7 2002-06-19 MW-3 929.42
8 2002-06-26 MW-3 930.02
9 2002-07-05 MW-3 930.00
inherit.aes = FALSE
解决了问题。关于您提到的大幅减少代码的第二点 - 感谢您的提示。我知道一定有更简单的方法!然而,当我设置矩形日期的数据框并运行您上面所做的ggplot代码时,我得到的图表没有任何矩形。您知道这可能是为什么吗? - snalvendput(data_sample)
命令。 - eipi10dput(data_sample)
的输出粘贴到你的问题中(粘贴到问题正文中,而不是评论中)。 - eipi10