[英]R - Multiple plot with ggplot
I have this small dataset 我有这个小数据集
map red_team blue_team
1 7 8
2 21 32
3 11 22
4 10 8
And I am trying to create a multiplot where each individual plot one represents one of the maps (1,2,3 and 4), and the content is two bars, one for red_team and another for blue_team on the X axis and the score on the Y axis. 我正在尝试创建一个多图,其中每个单独的图代表一个地图(1、2、3和4),内容是两个小节,一个在X轴上代表red_team,另一个在blue_team上,分数在Y轴。
This what I currently have. 这是我目前拥有的。
ggplot(winners_and_score, aes(red_team)) + geom_bar() + facet_wrap(~ map)
I'm having issue trying to display the score for both teams. 我在尝试显示两支球队的得分时遇到问题。
Thanks. 谢谢。
require(reshape2)
require(ggplot2)
# toy data
df = data.frame(map = 1:4, red_team = sample(7:21, 4, replace=T),
blue_team = sample(8:32, 4, replace=T))
df.melted <- melt(df, id='map')
> df.melted
map variable value
1 1 red_team 8
2 2 red_team 15
3 3 red_team 17
4 4 red_team 19
5 1 blue_team 22
6 2 blue_team 32
7 3 blue_team 31
8 4 blue_team 18
# making the plot
ggplot(data=df.melted, aes(x=variable, y=value, fill=variable)) +
geom_bar(stat='identity') +
facet_wrap(~map) +
theme_bw()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.