我正在嘗試在 ggplot 中創建一個條形圖,考慮填充每個條形的“型別”變數。
但是,條形圖的最大值過高(高于 100,而實際上它們應該接近 40)。我的目標是放置覆寫填充。
我很感激任何幫助。
df <- structure(list(Count = c("Beu", "Beu", "Beu", "Abe", "Abe", "Abe",
"Pre", "Pre", "Pre", "Bra", "Bra", "Bra"), Type = c(1, 2, 3,
1, 2, 3, 1, 2, 3, 1, 2, 3), Hours = c(40.17775, 42.1492178098676,
42.1910353866317, 38.3701812919564, 39.9185282522996, 38.8002722361139,
41.6389448017412, 41.7041742286751, 41.9545826200271, 41.1375910844406,
41.0602923264312, 40.6300999927013)), row.names = c(NA, 12L), class = "data.frame")
這是我要運行的代碼:
df %>%
mutate(Type = as.factor(Type)) %>%
ggplot(mapping = aes(x = Count, y = Hours, fill = Type))
geom_bar(stat = 'identity')
coord_flip()
theme_classic()
uj5u.com熱心網友回復:
要覆寫條形,您可以添加position = 'identity'較低geom_bar的透明度alpha,如下所示:
library(ggplot2)
library(dplyr)
df %>%
mutate(Type = as.factor(Type)) %>%
ggplot(mapping = aes(x = Count, y = Hours, fill = Type))
geom_bar(stat = 'identity', position = 'identity', alpha =0.2)
coord_flip()
theme_classic()

另一種選擇是通過躲避你的酒吧使用position_identity讓它們覆寫并確保最高值在你的后面,你可以arrange像這樣每組的值:
df %>%
mutate(Type = as.factor(Type)) %>%
group_by(Count) %>%
arrange(desc(Hours)) %>%
ggplot(mapping = aes(x = Count, y = Hours, fill = Type))
geom_bar (stat="identity", position =position_identity())
coord_flip()
theme_classic()


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/527526.html
標籤:rggplot2
上一篇:Y軸刻度數以百萬計
