尝试画画

时间:2017-08-20 06:57:28

标签: r

您好我正在尝试创建一个旁边的条形图,但我的条形图不在正确的空格中似乎重叠。

barplot(counts, 
    main ="2011 vs 2016 Men Volunteering",
    ylim=c(0,320000),
    xlab="Age (years)",
    ylab = "Frequancy",
    names.arg = c("15-19", "20-24", "25-34", "35-44", "45-54", "55-64", "65-74", "75-84", "84 and over"),
    col=c("red","green"),
    beside=TRUE)

legend("topright", 
       legend = c("2011", "2016"), 
       fill = c("red", "green"))

输出:

实际上我希望它看起来像这样:

我尝试过使用ncol = 2,但我无法使用它,我该如何实现呢?

像这样,

barplot(counts, 
    main ="2011 vs 2016 Men Volunteering",
    ylim=c(0,320000),
    xlab="Age (years)",
    ylab = "Frequancy",
    names.arg = c("15-19", "20-24", "25-34", "35-44", "45-54", "55-64", "65-74", "75-84", "84 and over"),
    ncol = 2,
    col=c("red","green"),
    beside=TRUE)

legend("topright", 
       legend = c("2011", "2016"), 
       fill = c("red", "green"))

还是这个?

counts <- as.matrix(ELEVENMenVolunteerAge, SIXTEENMenVolunteerAge, ncol = 2)

所有帮助表示非常感谢。

1 个答案:

答案 0 :(得分:0)

由于counts没有可复制的数据,因此我提供了一些关于如何创建躲闪图的示例。

ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
    geom_bar(position = "dodge")

enter image description here

# Grouped Bar Plot
counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, col=c("darkblue","red"),
        legend = rownames(counts), beside=TRUE)

enter image description here