针对两个分类变量创建清晰的ggplot图

时间:2017-02-26 17:06:05

标签: r ggplot2

我首先要道歉,因为我没有花足够的精力去问我的问题。我注意到他们不清楚,并遵守社区要求帮助你和我自己的规则。

我的代码遇到了问题,而且我不知道如何制作清晰的图表。

问题如下:制作一个条形图,显示每支球队联赛胜利的数量。图表必须尽可能清晰可读。

这是我的代码:

   library(Lahman)
   library(ggplot2)
   library(dplyr)
   library(reshape2)
   Teams %>% group_by(teamID) %>% 
     filter(!is.na(LgWin == "Y")) %>% 
     ggplot(Teams, mapping = aes(teamID, ..count..)) + 
     geom_bar(mapping = aes(fill = LgWin))

图片没有任何意义。请帮忙。

The picture does not make any sense. Please help.

1 个答案:

答案 0 :(得分:1)

最后你要绘制每个teamId拥有的行数LgWin ==" Y"对?如果是这样,你可以这样做:

Teams %>% 
  filter(LgWin == "Y") %>% 
  group_by(teamID) %>% 
  summarise(number_of_wins = n()) %>% 
  transform(., teamID = reorder(teamID, number_of_wins)) %>% 
  ggplot(., mapping = aes(x = teamID, number_of_wins)) + 
  geom_bar(stat = "identity", mapping = aes(fill = number_of_wins)) +
  coord_flip()

有了这个结果:

enter image description here

希望这会有所帮助...