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

标签 r ggplot2

在条形图的图例中,图例中的颜色与条形中的颜色不匹配:它们乱序。例如,在条形图中,每个条形的最左侧部分(第 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/

相关文章:

r - 仅当 ggplot 中 y 轴下限设置为 0 时才会出现图条

r - 合并两个 data.frames 并用 df2 的值替换 df1 某些列的值

r - 模拟从 "Hat"随机抽取

r - 如何在 R 中的特定条件下删除行?

r - 安装旧版本的 R

r - 将 textGrob 转换为 imageGrob/rasterGrob?

R 为 fn 和 gr 优化相同的函数

R Arules 项目频率图旋转轴标签

r - 对数据集排序,以便 geom_path() 以与人眼相同的方式连接点

r - ggplot geom_bar 与 stat = "sum"