合并两个创建的直方图以便比较它们

时间:2017-11-14 16:26:16

标签: r

我想比较州的两个不同候选人的选举结果(Percentatge.of.Votes),并将它们绘制成一个图形。

为此,我编译了以下内容:

swing.obama = filter(swing.states, Candidate == "Barack Obama")

swing.romney = filter(swing.states, Candidate != "Barack Obama")

par(mfrow=c(2,1))

ro.swing = barplot(swing.romney$Percentatge.of.Votes, names.arg = swing.romney$State.or.district,
                    col = "red2", main = "Results by State - Mitt Romney")

ob.swing = barplot(swing.obama$Percentatge.of.Votes, names.arg = swing.obama$State.or.district,
                   col = "darkblue", main = "Results by State - Barack Obama")

在这里,你可以并排看到直方图中的两个结果。

州的结果:

"如何合并"这两个文件是为了并排获得蓝色和红色条纹?

1 个答案:

答案 0 :(得分:1)

请测试此ggplot2解决方案:

library(ggplot2)
ggplot(swing.states, 
       aes(State.or.district, Percentatge.of.Votes, fill = Candidate)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Results by state",
     x = "State/district",
     y = "Votes, %")