gpt4 book ai didi

r - 在错误顺序的 ggplot2 条形图图例元素中,一个玩家的条形颜色已关闭

转载 作者:行者123 更新时间:2023-12-02 01:43:46 25 4
gpt4 key购买 nike

在条形图的图例中,图例中的颜色与条形中的颜色不匹配:它们乱序。例如,在条形图中,每个条形的最左侧部分(第 1 季)是黄色,但在图例中,黄色是第 8 季的颜色,因此图例的颜色与条形图的颜色不匹配。

另外:

  1. 图例中的每个方框都有黑色边框。我希望删除那些边框,但保留条形图中条形周围的边框。
  2. 对于第 1 赛季,Koosman 的胜利为零,因此没有显示它的方框,因此栏中的颜色不协调。有没有办法对此进行调整,以便 Koosman 在他的酒吧中的第一个盒子是第 2 季的颜色?

我查看了 stackoverflow 解决方案,但没有找到符合我情况的解决方案。如果存在,请分享其链接,我将不胜感激。

Output

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()

最佳答案

试试这个

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)))

enter image description here

position = position_stack(reverse = TRUE) 在 geom_bar 中反转颜色序列,并且 guides(fill = guide_legend(override.aes = list(color = NA))) 去掉图例中的边框。

关于r - 在错误顺序的 ggplot2 条形图图例元素中,一个玩家的条形颜色已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71210751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com