簡體   English   中英

ggplot2 使圖例鍵填充透明

[英]ggplot2 make legend key fill transparent

我正在嘗試使 ggplot 的圖例鍵填充透明。 我按照 Hadley 的 ggplot2 指南之一的說明更改圖例鍵填充,但是由於某種原因,當我將填充設置為透明時,它會填充灰色。 即使我將圖例鍵填充設置為白色,它在最終圖中仍然顯示為灰色。

下面是一個例子:

library(ggplot2)

data1 = c(0,10, 11, 23, 33, 40, 41, 50, 59, 68, 76, 88, 90, 99)
data2 = c(2, 8, 10, 22, 39, 47, 49, 55, 62, 70, 76, 86, 88, 95)

df = data.frame(data1, data2)

(plot = ggplot() +
  geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
  geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
  scale_x_continuous(expand=c(0,0), limits=c(0,100)) + 
  scale_y_continuous(expand=c(0,0), limits=c(0,100))+
  theme_classic()+
  labs(y="data2", x="data1", 
       title="sample 1 data1 vs data2") +
  theme(plot.title = element_text(size=18, face="bold"),
        legend.key = element_rect(colour = "transparent", fill = "white"),
        legend.justification = c(1,0), legend.position = c(1,0))+
  scale_color_discrete(name="Sample") )

Example_plot

如果我設置theme(legend.key = element_rect(colour = "transparent", fill = "red"))我得到以下圖: red_fill

所以看起來我可以更改圖例鍵填充,但不能更改為白色或透明。

有誰知道我做錯了什么,或者是否沒有辦法讓圖例鍵填充透明/白色?

編輯:設置theme(legend.key = element_rect(fill = alpha("white", 0.0)))不能解決問題。

看這里:

library(ggplot2)
library(scales)

data1 = c(0,10, 11, 23, 33, 40, 41, 50, 59, 68, 76, 88, 90, 99)
data2 = c(2, 8, 10, 22, 39, 47, 49, 55, 62, 70, 76, 86, 88, 95)

df = data.frame(data1, data2)

(plot = ggplot() +
  geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
  theme_classic()+
  labs(y="data2", x="data1", 
       title="sample 1 data1 vs data2") +
  theme(plot.title = element_text(size=18, face="bold"),
        legend.key = element_rect(colour = "transparent", fill = alpha("red", 0)),
        legend.justification = c(1,0), legend.position = c(1,0))+
  scale_color_discrete(name="Sample") )

EDIT2:如果我使用geom_line()而不是geom_smooth我可以將圖例鍵填充設置為 NA,所以這一定是因為geom_smooth的線有一個灰色區域來表示它周圍的置信區間,因此圖例鍵看起來像.

(plot = ggplot() +
  geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
  geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
  scale_x_continuous(expand=c(0,0), limits=c(0,100)) + 
  scale_y_continuous(expand=c(0,0), limits=c(0,100))+
  theme_classic()+
  labs(y="data2", x="data1", 
       title="sample 1 data1 vs data2") +
  theme(plot.title = element_text(size=18, face="bold"),
        legend.key = element_rect(colour = NA, fill = NA),
        legend.justification = c(1,0), legend.position = c(1,0))+
  scale_color_discrete(name="Sample") )

geom_line

我也對這種行為感到瘋狂,所以這里有一個適用於您的示例的可行解決方案:

plot + guides(color=guide_legend(override.aes=list(fill=NA)))

檢查線程以獲取更多信息。

如果你願意,你可以欺騙它。 添加第二個geom_smooth() 第一個帶有信心帶並且您不顯示傳奇。 使用第二個,您刪除樂隊但顯示圖例。

df$Color <- "Red"
df1 <- df
(plot = ggplot() +
  geom_smooth(data=df, aes(data1, data2,colour=Color), se = TRUE, show.legend = FALSE) + 
  geom_smooth(data=df1, aes(data1, data2,colour=Color), se=FALSE) +
  geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
  scale_x_continuous(expand=c(0,0), limits=c(0,100)) + 
  scale_y_continuous(expand=c(0,0), limits=c(0,100))+
  theme_classic()+
  labs(y="data2", x="data1", 
       title="sample 1 data1 vs data2") +
  theme(plot.title = element_text(size=18, face="bold"),
        legend.key = element_rect(colour = "transparent", fill = "white"),
        legend.justification = c(1,0), legend.position = c(1,0))+
  scale_color_discrete(name="Sample"))

在此處輸入圖片說明

這個答案似乎是最簡單的解決方案,在theme()定義中設置legend.key = element_blank()

除了legend.key = element_blank()您還可以將legend.background=element_blank()放在theme() ,以使文本也透明

這也將使使用gg_themes時默認的白色背景透明

要利用透明度級別,可以使用:

對於整個傳說:

theme(legend.background=element_rect(fill = alpha("white", 0.5)))

alpha("white", 0)element_blank()一樣完全透明,而alpha("white", 1)沒有透明度。

現在,對於關鍵 - 如果關鍵和背景具有不同的透明度:

theme(legend.background=element_rect(fill = alpha("white", 0)),
      legend.key=element_rect(fill = alpha("white", .5)))

注意:背景透明度高於關鍵的透明度,即背景 alpha 必須小於關鍵 alpha。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM