【发布时间】:2011-06-09 18:44:53
【问题描述】:
我正在尝试用 R 中的一些 ggplots 对最近的 MLB 选秀进行一些分析
selection <- draft[c("Team","Division","Position")]
head(selection)
Team Division Position
1 pit NL Central P
2 sea AL West P
3 ari NL West P
4 bal AL East P
5 kc AL Central O
6 was NL East I
其中 P = 投手,O=外场等
我想按每个分区的位置显示球队选择的球员人数
p <- ggplot(data=selection, aes(x=Team, fill= Position)) + geom_bar(position="stack")
p <- p + coord_flip()
p <- p+ ylab("Players Selected")
p <- p + facet_wrap(~Division)
p
这让我了解了其中的一部分,但很不吸引人
a) 分组有效,但所有团队都显示在每个分区网格中 - 尽管每个分区中只有 5 或 6 个团队实际上 - 并且正确 - 显示数据
b) 通过坐标翻转,团队将按字母倒序排列在页面下方。我可以求助吗?左对齐也很好
c) 我如何将图例设置为 Pitching、Outfield 而不是 P 和 O - 这是我需要以某种方式设置和包含的向量
d) 看看每支球队选择对每种类型球员的比例也会很有趣。这是通过设置 position="fill" 来完成的。我可以将轴设置为 % 而不是 0 到 1。我还尝试设置 geom_vline(aes(xintercept=0.5) - 和 yintercept 以防考虑到翻转 - 但这条线没有出现在 x 轴的中间标记处
帮助非常感谢
【问题讨论】:
-
如果您的目标只是翻转因子,您可以在 ggplot2 aes 调用
x=时使用reorder(Team, -as.numeric(Team))。
标签: r ggplot2 aesthetics