我怎樣才能給圖上色,這樣 -
A1 - 深藍色,A2 - 淺藍色,B1 - 深紅色,B2 - 淺紅色
tbl <- tibble(x = c(rnorm(n = 100, mean = 0, sd = 1),
rnorm(n = 100, mean = 0, sd = 0.5),
rnorm(n = 100, mean = 4, sd = 1),
rnorm(n = 100, mean = 4, sd = 0.5)),
y = c(rep("A1", 100),
rep("A2", 100),
rep("B1", 100),
rep("B2", 100))
)
ggplot(data = tbl,
aes(x = x,
fill = y))
geom_histogram(color = "black",
alpha = 0.5)
theme_bw()
uj5u.com熱心網友回復:
我隨意選擇了顏色(深藍色~淺紅色)。
您可以使用 中的十六進制代碼手動更改顏色sclae_fill_manual。
tbl <- tibble(x = c(rnorm(n = 100, mean = 0, sd = 1),
rnorm(n = 100, mean = 0, sd = 0.5),
rnorm(n = 100, mean = 4, sd = 1),
rnorm(n = 100, mean = 4, sd = 0.5)),
y = c(rep("A1", 100),
rep("A2", 100),
rep("B1", 100),
rep("B2", 100))
)
ggplot(data = tbl,
aes(x = x,
fill = y))
geom_histogram(color = "black",
alpha = 0.5)
scale_fill_manual(values = c('#2C3FF6','#72B5FC','#F62C2C','#F0C3C3'))
theme_bw()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

由reprex 包于 2022-05-18 創建(v2.0.1)
uj5u.com熱心網友回復:
類似于上面的答案,但使用命名的顏色向量更詳細一些。
# Create a named vector of colors
# There is no R color named "light red" therefore I used red instead.
colours <- c(A1= "darkblue", A2="lightblue", B1= "darkred", B2= "red")
ggplot(data = tbl,
aes(x = x,
fill = y))
geom_histogram(color = "black",
alpha = 0.5)
scale_fill_manual(values = colours)
theme_bw()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/477216.html
上一篇:在ggplot中繪制輪廓
