Reputation: 169
I have a data frame:
df <- data.frame(human = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5),
stage = c("A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4"),
class = c(0,1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0)
)
And want bar chart for each stage on the x-axis:
ggplot(df, aes(x = stage, y = class, fill = as.factor(human))) + geom_bar(stat = "identity") + scale_y_continuous(limits = c(0,15))
Looks good, but I would like the human elements side by side, so I add position = "dodge"
:
ggplot(df, aes(x = stage, y = class, fill = as.factor(human))) + geom_bar(stat = "identity", position= "dodge") + scale_y_continuous(limits = c(0,15))
While colums are side by side now, for some reason all class = 1:
Upvotes: 0
Views: 325
Reputation: 2901
A solution that avoids the dplyr
preprocessing to use stat_summary
:
ggplot(df, aes(x = stage,
y = class,
fill = as.factor(human))) +
stat_summary(geom = "bar",
position = "dodge",
fun.y = "sum")
Upvotes: 1
Reputation: 135
It's because your "identies" are 0 or 1. One way to deal with this is to summarize
your data before you plot it. For example:
library(tidyverse)
df %>%
group_by(human, stage) %>%
summarise(class = sum(class)) %>%
ggplot(aes(x = stage, y = class, fill = as.factor(human))) +
geom_bar(stat = "identity", position= "dodge")
Upvotes: 4
Reputation: 5923
Because you use stat = "identity"
. So then you'll have to count beforehand.
library(tidyverse)
df %>%
count(stage, class, human) %>%
ggplot(aes(x = stage, y = n, fill = as.factor(human))) +
geom_bar(stat = "identity", position = "dodge")
Upvotes: 0