我目前有一個關于“填充”引數如何為我的情節中的不同條著色的問題。我希望對顏色進行預先確定的排名,以便我繪制的表格中的每個值都對應于一種顏色,而不是在我的情況下,它會發生變化。它在我的代碼中的作業方式是,我表中的最高值對應于最亮的顏色(在默認的 ggplot 調色板中),而最低值對應于最暗的顏色。我的具體愿望是 100 應該是最亮的顏色,0 應該是最暗的顏色(因為我正在繪制分位數),當然還有介于兩者之間的所有內容。
這是我的代碼。
library(tidyverse)
test <- tibble(factors=c("fact1","fact2","fact3","fact4"),
percent=c(18.3,42.6,81.1,62.8))
ggplot(test)
geom_hline(
aes(yintercept = y),
data.frame(y = c(0:4) * 25),
color = "lightgrey"
)
geom_col(
aes(
x = reorder(str_wrap((factors), 5), percent),
y = percent,
fill = percent
),
position = "dodge2",
show.legend = TRUE,
alpha = .9,
width=0.8
)
geom_segment(
aes(
x = reorder(str_wrap(factors, 5), percent),
y = 0,
xend = reorder(str_wrap(factors, 5), percent),
yend = 100
),
linetype = "dashed",
color = "gray12"
)
scale_y_continuous(
limits = c(-50, 100),
expand = c(0, 0),
breaks = c(0, 25, 50, 75,100)
)
coord_polar()
如果您將“測驗”小標題的“百分比”部分中的值更改為類似percent=c(80.1,80.5,80.1,79.9)您會很快理解我在說什么的值,就像在我的理想情況下一樣,它應該只顯示四種幾乎相同的顏色而不是從“百分比”列的最高到最低確定的顏色范圍,顯示從深到淺的顏色。
提前致謝!
uj5u.com熱心網友回復:
首先你應該創建一個調色板:
my_palette <- colorRampPalette((RColorBrewer::brewer.pal(9, "Blues")))
然后你可以scale_fill_gradientn在你的 ggplot 中添加一個:
test <- tibble(factors=c("fact1","fact2","fact3","fact4"),
# percent=c(18.3,42.6,81.1,62.8))
percent = c(80.1,80.5,80.1,79.9))
ggplot(test)
geom_hline(
aes(yintercept = y),
data.frame(y = c(0:4) * 25),
color = "lightgrey"
)
geom_col(
aes(
x = reorder(str_wrap((factors), 5), percent),
y = percent,
fill = percent
),
position = "dodge2",
show.legend = TRUE,
alpha = .9,
width=0.8
)
geom_segment(
aes(
x = reorder(str_wrap(factors, 5), percent),
y = 0,
xend = reorder(str_wrap(factors, 5), percent),
yend = 100
),
linetype = "dashed",
color = "gray12"
)
scale_y_continuous(
limits = c(-50, 100),
expand = c(0, 0),
breaks = c(0, 25, 50, 75,100)
)
scale_fill_gradientn(colours = my_palette(100),
limits=c(0,100))
coord_polar()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394821.html
