我想比较州的两个不同候选人的选举结果(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")
在这里,你可以并排看到直方图中的两个结果。
州的结果:
"如何合并"这两个文件是为了并排获得蓝色和红色条纹?
答案 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, %")