使用ggplot

时间:2017-04-28 10:24:52

标签: r ggplot2 graphing

我试图生成频率为多组的条形图。我尝试使用geom_bar(),但我一直遇到"错误:stat_count()不得与y审美一起使用。"每个参与者都有一行,年龄(2类),条件(2类)和表现(0或1)。从我在手册上阅读的内容到几乎在网上,如果我使用

bar<-ggplot(data, aes(age, performance, fill = condition)) + geom_bar(position = "dodge")

我应该得到我想要的东西(which is this),但我得到了错误,我无法弄清楚我错过了什么。是不是geom_bar()应该默认给出计数?当我使用stat =&#34; identity&#34;我得到像这样的完整酒吧:how it actually looks。 请帮忙!任何建议将不胜感激。

EDITED: 这是我的实际数据:

structure(list(ageyears = c(4L, 4L, 5L, 5L, 5L, 4L, 5L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 5L, 4L, 
5L, 5L, 4L, 4L, 4L, 5L, 4L, 4L, 5L, 4L, 5L, 4L, 4L, 5L, 5L, 4L, 
4L, 5L, 4L, 5L, 4L, 5L, 4L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 4L, 4L, 5L, 5L, 4L, 5L, 5L, 4L, 4L, 
5L, 5L, 5L, 4L, 5L, 5L, 4L, 5L, 5L, 4L, 4L, 5L, 4L, 5L, 5L, 4L, 
5L, 4L, 4L, 5L, 5L, 4L, 5L, 5L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 
5L, 5L, 5L, 4L, 5L, 5L, 4L, 5L, 5L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 
4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 5L, 5L, 
5L, 5L, 4L, 4L, 4L, 5L, 4L), MatrixLabels = structure(c(2L, 2L, 
1L, 1L, 2L, 2L, 1L,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L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
1L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L)), .Names = c("ageyears", 
"MatrixLabels", "Mat_sort_pass_fail"), row.names = c(1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 38L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 48L, 
49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 60L, 61L, 62L, 
63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 74L, 75L, 76L, 
77L, 78L, 79L, 80L, 82L, 83L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 
92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 
104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 
115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 
126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 
137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 
148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 
159L, 160L, 197L, 198L, 200L, 201L, 202L, 203L, 204L, 205L, 206L, 
207L), class = "data.frame")

3 个答案:

答案 0 :(得分:1)

来自geom_bar的文档:

  

默认情况下,geom_bar使用stat =“count”来表示高度   酒吧比例与每组病例数(或重量   提供了aethetic,权重的总和)。如果你想要高度   用于表示数据中值的条形图,使用stat =“identity”和   将变量映射到y审美。

在你的情况下,你应该使用高度作为你的表现总和,因为你有一个汇总数据,所以ggplot应该使用stat = identity

编辑 OP粘贴dput:

您需要先汇总您的数据,我假设df是您的数据框,您可以使用任何内容进行汇总,我使用data.table和baseR聚合,您可以选择其中任何一个他们这样做如下:

###1. base R aggregate

 df <- aggregate(Mat_sort_pass_fail ~ ageyears + MatrixLabels, data=df1 ,sum)
df$perc <- df$Mat_sort_pass_fail/sum(df$Mat_sort_pass_fail)
names(df) <- c("age","condition","performance","percentage")



###2. sumarization using data.table
library(data.table)

dt <- setDT(df)
dt1 <- dt[,list(Performance = sum(Mat_sort_pass_fail)),by=c("ageyears","MatrixLabels")]
dt1[,perc:=Performance/sum(Performance)] ##percentage within column

df <- data.frame(dt1)
names(df) <- c("age","condition","performance","percentage")


library(ggplot2)
library(RColorBrewer)

ggplot(df, aes(x = condition ,y=performance)) +
  geom_bar(aes(fill = factor(age)),stat="identity",position = "dodge") +
  ggtitle("Matrix Sort Performance") + 
  scale_fill_brewer(palette = "Dark2")

###In case you need the percentage run the below code:
ggplot(df, aes(x = condition ,y=percentage)) +
  geom_bar(aes(fill = factor(age)),stat="identity",position = "dodge") +
  ggtitle("Matrix Sort Performance") + 
  scale_fill_brewer(palette = "Dark2")

enter image description here

答案 1 :(得分:0)

通常它会根据您的数据计算频率。如果您的数据已经分组,请尝试以下操作:

"dom": '<"top"if>rt<"bottom"lp><"clear">',

答案 2 :(得分:0)

您可以使用geom_col()作为geom_bar(stat = "identity")的别名。

你也有我认为错误的地图。

我根据您发布的图表模仿了一些数据:

df <- data.frame(age = factor(rep(4:5, each = 2), labels = c('4-Years-Olds', '5-Years-Olds')),
                 performance = c(48,37,65,65),
                 condition = factor(c(1,2,1,2), labels = c('No Label', 'Label')))

library(ggplot2)

ggplot(df) +
    geom_col(aes(condition, performance, fill = age), position = 'dodge') +
    scale_fill_manual(values = c('skyblue', 'darkolivegreen1'))