在條形圖的圖例中,圖例中的顏色與條形圖中的顏色不匹配:它們是亂序的。例如,在條形圖中,第 1 季的每個條形的最左邊是黃色的,但在圖例中,黃色是第 8 季的顏色,因此圖例的顏色與條形圖的顏色不匹配。
此外:
- 圖例中的每個框都有黑色邊框。我希望洗掉那些邊框,但保留條形圖中條形周圍的邊框。
- 對于第 1 季,Koosman 的勝利為零,因此沒有顯示方框,因此欄中的顏色不協調。有沒有辦法對此進行調整,所以庫斯曼在他的酒吧里的第一個盒子是第 2 季的顏色?
我檢查了 stackoverflow 解決方案,但沒有看到與我的情況相匹配的解決方案。如果存在,如果您可以分享其鏈接,我將不勝感激。

win_dfL 的輸入
structure(list(Pitcher = c("Jacob deGrom", "Jacob deGrom", "Jacob deGrom",
"Jacob deGrom", "Jacob deGrom", "Jacob deGrom", "Jacob deGrom",
"Jacob deGrom", "Tom Seaver", "Tom Seaver", "Tom Seaver", "Tom Seaver",
"Tom Seaver", "Tom Seaver", "Tom Seaver", "Tom Seaver", "Dwight Gooden",
"Dwight Gooden", "Dwight Gooden", "Dwight Gooden", "Dwight Gooden",
"Dwight Gooden", "Dwight Gooden", "Dwight Gooden", "Jerry Koosman",
"Jerry Koosman", "Jerry Koosman", "Jerry Koosman", "Jerry Koosman",
"Jerry Koosman", "Jerry Koosman", "Jerry Koosman", "Sid Fernandez",
"Sid Fernandez", "Sid Fernandez", "Sid Fernandez", "Sid Fernandez",
"Sid Fernandez", "Sid Fernandez", "Sid Fernandez"), Season = c("S1",
"S2", "S3", "S4", "S5", "S6", "S7", "S8", "S1", "S2", "S3", "S4",
"S5", "S6", "S7", "S8", "S1", "S2", "S3", "S4", "S5", "S6", "S7",
"S8", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S1", "S2",
"S3", "S4", "S5", "S6", "S7", "S8"), Wins = c(9, 14, 7, 15, 10,
11, 4, 7, 16, 16, 25, 18, 20, 21, 19, 11, 17, 24, 17, 15, 18,
9, 19, 13, 0, 19, 17, 12, 6, 11, 14, 15, 6, 9, 16, 12, 12, 14,
9, 1)), row.names = c(NA, -40L), class = c("tbl_df", "tbl", "data.frame"
))
ggplot2 代碼
win_dfL %>%
ggplot(aes(x=Pitcher, y=Wins, fill=Season))
geom_bar(stat="identity", color="black", width = .85)
scale_fill_manual(values=c("#A0522D",
"#FAD5A5",
"red",
"white",
"gray",
"#C04000",
"#FF7518",
"yellow"))
# Horizontal bar plot
coord_flip()
uj5u.com熱心網友回復:
使用mutate和fct_rev 
uj5u.com熱心網友回復:
嘗試這個
win_dfL %>%
ggplot(aes(x=Pitcher, y=Wins, fill=Season))
geom_bar(stat="identity", color="black", width = .85, position = position_stack(reverse = TRUE))
scale_fill_manual(values=c("#A0522D",
"#FAD5A5",
"red",
"white",
"gray",
"#C04000",
"#FF7518",
"yellow"))
# Horizontal bar plot
coord_flip()
guides(fill = guide_legend(override.aes = list(color = NA)))

position = position_stack(reverse = TRUE)在 geom_bar 內反轉顏色序列,并guides(fill = guide_legend(override.aes = list(color = NA)))擺脫圖例中的邊框。
uj5u.com熱心網友回復:
我們可以fct_rev從forcats包中使用:
win_dfL %>%
ggplot(aes(x=Pitcher, y=Wins, fill=forcats::fct_rev(Season)))
geom_col()
scale_fill_manual(values=c("#A0522D",
"#FAD5A5",
"red",
"white",
"gray",
"#C04000",
"#FF7518",
"yellow"))
coord_flip()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/430470.html
上一篇:R:如何將資料框列合并為一行并將這些原始列的行值合并為一列?[復制]
下一篇:R中的3引數非線性方程擬合
