[英]Remove legend background when using geom_rect
我在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
您可以通過在geom_rect
添加inherit.aes=FALSE
來擺脫圖例中的薰衣草背景。 我還想指出,您可以大大減少圖形所需的代碼量。 而不是單獨的呼叫geom_rect
每對日期的,你只需要有一個載體xmin
日期和一個矢量xmax
在一個單一的呼叫日期geom_rect
。 例如:
Data = data.frame(x=as.Date(c("2005-01-01", "2010-01-01")), y=c(5,6), group=c("A","B"))
rect.data = data.frame(xmin = as.Date(c("2003-08-11","2004-04-29","2016-04-01")),
xmax = as.Date(c("2003-12-14","2004-12-20","2016-12-13")),
ymin = -Inf,
ymax = Inf)
ggplot(Data, aes(x, y, colour=group)) +
geom_rect(data=rect.data, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
fill="lavender", linetype=0, alpha=1, inherit.aes=FALSE) +
geom_point(size=3) +
theme_classic()
同樣,如果您可以制定明確的標准來確定陰影的開始和結束日期,則可以直接從數據中以編程方式提取日期,而不必手動對其進行硬編碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.