![](/img/trans.png)
[英]ggplot: how to get a semi-transparent fill in a geom_rect with a color different from other geoms? fill=alpha() does not seem to work
[英]Specify different alpha for fill and color in geom_rect inside aes( )?
我有一個圖表,我想在其中為元素的每個組件設置單獨的 alpha 級別。 我有一個 geom_rect 元素,旨在根據變量“違規”更改顏色和填充。 我想為顏色指定 alpha 並分別填充。 但是,在保持基於“違規”的分組顏色的同時,我無法做到這一點。
我有一個 colors 的命名向量,例如,其中的名稱與“違規”變量的值匹配:
viol.colors <- c("darkorange", "darkblue")
names(viol.colors) = c("violation.mean", "violation.sd")
示例數據(真實數據有更多列,這就是為什么 plot 代碼中包含“選擇”的原因):
groups <- data.frame(
violation = rep(c("violation.mean", "violation.sd"), each = 5),
xmin = rep(c(1,1.5), each = 5),
xmax = rep(c(2,3), each = 5),
ymin = rep(c(1,2), each = 5),
ymax = rep(c(1.5, 3), each = 5),
rect.color = rep(c("darkorange", "darkblue"), each = 5)
)
如果我將顏色和填充設置為按“違規”分組,然后使用 scale_fill_manual 設置 colors,則 colors 將正確應用於 geom_rect 的這兩個組件。 但是,如果我在 aes() 中設置顏色和填充,我無法指定 alpha 級別。
示例 1 - 這基於“違規”變量應用了正確的 colors,但我無法從 aes() 中指定 alpha。
geom_rect(data = groups %>%
group_by(violation) %>%
select(violation, xmin, xmax, ymin, ymax, rect.color) %>%
unique(),
aes(xmin = xmin - 0.75,
xmax = xmax + 0.75,
ymin = ymin - (sd *0.15),
ymax = ymax + (sd *0.15),
colour = violation,
fill = violation
),
size= 0.25
scale_fill_manual(values = viol.colors, aesthetics = c("color", "fill"))
示例 2 - 如果我在 aes() 之外為 geom_rect 設置顏色和填充,我可以指定 alpha,但不會基於“違規”應用正確的 colors。
geom_rect(data = groups %>%
group_by(violation) %>%
select(violation, xmin, xmax, ymin, ymax, rect.color) %>%
unique(),
aes(xmin = xmin - 0.75,
xmax = xmax + 0.75,
ymin = ymin - (sd *0.15),
ymax = ymax + (sd *0.15),
group = violation
),
size= 0.25,
fill = alpha("darkorange", .05),
colour = alpha("darkorange", .5) +
scale_fill_manual(values = viol.colors, aesthetics = c("color", "fill"))
在我看來,最簡單的方法應該是結合這兩種方法並將以下代碼放在 aes() 中。 但是,這會引發錯誤。
fill = alpha(violation, .05),
colour = alpha(violation, .5)
任何有關如何實現這一點的見解將不勝感激,我已經嘗試了許多不同的版本,但均未成功。
解決此問題的一種方法是將 alpha 編碼為您想要的顏色,方法是用十六進制指定顏色,最后兩位數字表示 alpha 值。 您可以在許多在線計算器中查找這些值。
例如,如果您想要 5% 不透明度的深藍色(“#00008b”,我在其中查找),那么您將 append “0D”,代表 13(約 255 的 5%)到最后。 然后scale_color_identity
使用您添加到數據中的顏色名稱到 output 該特定顏色。 如果您以十六進制指定顏色,您還可以獲得為它指定 alpha 的附加選項。
https://ggplot2.tidyverse.org/reference/aes_colour_fill_alpha.html#alpha
ggplot(data = data.frame(violation = 1:2,
colors = c("#00008b0D", #0D = 13 in hex = 5% of 255
"#ff8c0080")), #80 = 128 in hex = 50% of 255
aes(violation, 1, fill = colors)) +
geom_tile() +
scale_fill_identity() +
theme_minimal()
您可以使用這些功能來控制審美評價。
library(tidyverse)
viol.colors <- c("darkorange", "darkblue")
names(viol.colors) = c("violation.mean", "violation.sd")
groups <- data.frame(
violation = rep(c("violation.mean", "violation.sd"), each = 5),
xmin = rep(c(1,1.5), each = 5),
xmax = rep(c(2,3), each = 5),
ymin = rep(c(1,2), each = 5),
ymax = rep(c(1.5, 3), each = 5)
)
ggplot(groups) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax ,
colour = stage(violation, after_scale = alpha(color, .5)),
fill = stage(violation, after_scale = alpha(fill, .05))
), size= 0.25) +
scale_fill_manual(values = viol.colors, aesthetics = c("color", "fill"))
我確實想出了一個解決方案。 這似乎有點hacky,我暫時不會接受這個作為答案,但我想分享它。 在我之前的代碼中,我過濾了 ggplot 代碼中的“組”數據。 當數據最初在代碼的其他地方生成時,我將其更改為發生。 這允許我根據“組”數據集的列值設置“填充”和“顏色”的值。
數據:
groups <- data.frame(
violation = rep(c("violation.mean", "violation.sd"), each = 5),
xmin = rep(c(1,1.5), each = 5),
xmax = rep(c(2,3), each = 5),
ymin = rep(c(1,2), each = 5),
ymax = rep(c(1.5, 3), each = 5),
rect.color = rep(c("darkorange", "darkblue"), each = 5)
)
然后,僅在此處過濾到唯一值,而不是在 ggplot 代碼中
groups <- groups %>%
group_by(violation) %>%
unique()
現在,我可以在 aes() 之外的“color”和“fill”中使用“rect.color”的列值,因為 groups$rect.color 現在的長度與生成的 geom_rect 元素的數量相同。
geom_rect_pattern(data = groups,
aes(xmin = xmin - 0.75,
xmax = xmax + 0.75,
ymin = ymin - (sd *0.15),
ymax = ymax + (sd *0.15),
group = violation,
),
size= 0.25,
color = alpha(groups$rect.color, .5),
fill = alpha(groups$rect.color, .05)
我知道這可能不是實現這一目標的最佳方式,但它確實有效。 如果有人有更好的方法或可以改進這一點,我會接受它作為答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.