だから私は次のようなdfを持っています:
colour_clicks <- structure(list(Color = c("beige", "black", "blue", "brown", "burgundy",
"gray", "green", "navy blue", "of many colors", "olive", "pink",
"red", "violet", "white"), avg = c(1.6799741044454, 2.48696524064171,
2.67327546825034, 1.94043703007519, 1.26768060836502, 1.94480302693078,
1.47427101200686, 1.09180327868852, 1.90684892897407, 1.11425902864259,
1.35, 1.48054996646546, 1.49029356060606, 2.02322924600152)), row.names = c(NA,
14L), class = c("tbl_df", "tbl", "data.frame"))
そして、これらの平均を説明するために棒グラフを作成したいのですが、2つの問題があります。
ggplot(colour_clicks, aes(x = reorder(Color, -avg), y=avg, fill=Color)) +
geom_bar(stat="identity", color = "black")+
scale_fill_manual(values=c(
"blue",
"black",
"white",
"gray",
"brown",
"orange",
"beige",
"violet",
"red",
"green",
"pink",
"orangered4",
"olivedrab",
"navyblue"
)) +
theme(legend.position = "none")
(2)の場合、指定した塗りつぶし変数はですColor
が、x軸変数はreorder(Color)
であるため、同じ変数に変更する必要があります。ggplot
この種の問題を回避するために、電話の前に並べ替えることをお勧めします。
colour_clicks %>%
mutate(Color = reorder(Color, desc(avg))) %>% # reorder here, not within ggplot call
ggplot(aes(x = Color, y=avg, fill=Color)) +
geom_bar(stat="identity", color = "black")+
scale_fill_manual(values=c(
"blue",
"black",
"white",
"gray",
"brown",
"orange",
"beige",
"violet",
"red",
"green",
"pink",
"orangered4",
"olivedrab",
"navyblue"
)) +
theme(legend.position = "none")
(1)の場合、独自のグラデーションバーを作成する以外にこれを行うための優れた方法はありません。基本的な方法は、ゼロからの値までのシーケンスを作成しavg
、それを使用してcolorRamp()
。でグラデーションを生成することです。残念ながら、これはR、G、Bを別々に出力するため、16進数に変換する必要があります。しかし、それほど悪くはありません。ここにあります。
# Make the gradient
max_y <- colour_clicks$avg[colour_clicks$Color == "of many colors"]
pal <- c('red', 'orange', 'yellow', 'green', 'blue', 'violet')
color_generator <- colorRamp(pal)
df <- data.frame(
y = seq(0, max_y, length.out = 100)
) %>%
mutate(
R = color_generator(y/max_y)[,1],
G = color_generator(y/max_y)[,2],
B = color_generator(y/max_y)[,3],
hexcol = sprintf("#%02x%02x%02x", (round(R, 0)),
(round(G, 0)),
(round(B, 0)))
)
colour_clicks %>%
mutate(Color = reorder(Color, desc(avg))) %>%
ggplot(aes(x = Color, y=avg, fill=Color)) +
geom_bar(stat="identity", color = "black")+
scale_fill_manual(values=c(
"blue", "black", "white", "gray", "brown", "orange", "beige", "violet",
"red", "green", "pink", "orangered4", "olivedrab", "navyblue"
)) +
# Overlay the rainbow column on the old column
geom_col(data = df, aes(x = 6, y = max_y/100, group = 1:100), fill = df$hexcol) +
theme(legend.position = "none")
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加