Reputation: 55
Desired Behavior: I want to create a grouped barplot of multiple columns of my dataset, plotting the grouped barplot with respect to a categorical variable having the values low, medium and high. For example, for the dataset shown below, I want to create a a three-group bar-plot for each 'B', 'M', 'LA' and so on on the x-axis, while filling the bars of each three-group by the mean of their values according to the category.
I have tried the following code to counter this, but I can't seem to understand what I am doing wrong here, as the code throws the error that the values don't add up dimension wise. Can anyone let me know what is the fault here, as per to my knowledge all the filled aspects of the ggplot seem to emulate my requirement? ...
df <- data.frame(B=c(1,2,4,-5,8,9,8,4,5),GM=c(5,5,4,-7,9,8,7,-4,6),
LA=c(-5,8,5,-8,4,5,6,1,7), NS= c(-5,-8,-5,-8,4,5,-6,1,7),
SL=c(-5,8,5,-8,-4,-5,6,-1,-7), data.SWASH_group=c("low", "medium", "low",
"high", "high", "low", "medium", "low", "high"))
species=c(df$B, df$GM, df$LA, df$NS, df$SL)
condition=c(df$data.SWASH_group=="low", df$data.SWASH_group=="medium",
df$data.SWASH_group=="high")
value=c(mean(df$B), mean(df$GM), mean(df$LA), mean(df$NS), mean(df$SL))
# For Grouping
library(ggplot2)
ggplot(df, aes(fill=condition, y=value, x=species)) +
geom_bar(position="dodge", stat="identity")
This is how the head of my real dataset - df - looks like (The one in the
code is just for example purposes):
B GM LA NS SL data.SWASH_group
561.5806 533.5484 602.4000 675.4706 621.5758 low
780.5758 615.8788 801.8788 589.2903 875.0938 medium
649.9697 658.9091 789.1818 638.4412 646.5455 medium
... I expect the output to look something like this:
This is the output my own code produces:
Upvotes: 2
Views: 2315
Reputation: 441
We would need reproducible data to address your question. But if I have understood your description correctly this should work:
library(ggplot2)
library(dplyr)
library(reshape2)
df <- data.frame(A=c(1,2,4,5,8,9,8,4,5),B=c(5,5,4,7,9,8,7,4,6),
C=c(5,8,5,8,4,5,6,1,7),
group.var=c("low", "medium", "low", "high", "high", "low", "medium", "low", "high"))
df.agg <- df %>% group_by(group.var) %>% summarise_all(funs(mean))
df.agg <- melt(df.agg)
ggplot(df.agg) +
geom_bar(aes(x=variable, y = value, fill = as.factor(group.var)),
position="dodge", stat="identity")
Upvotes: 1