I have unsuccesfully tried to create a bar plot using ggplot, using the following dataframe:
test <- data.frame(group=c("A", "A", "B", "B"),
gender=c(0,1,0,1),
percent1=c(2,3,4,3),
percent2=c(1,2,0.5,1))
What I want to do, is to create a bar plot. X axis: groups by Letters ('A', 'B'), and split these groups by gender (0 or 1). And Y axis: values of percent1 and percent2 stacked on top of each other
My most 'succesful' solution: I simplified my dataframe, after which the following code gave me the barplot below. However, I did not manage to create subgroups.I have used 'facet_wrap' and 'facet_grid' but consequently failed to create a plot from these.
Question: How do I make this stacked barplot, arranged by groups and subgroups?
# require(ggplot)
# require(reshape)
dfr <- data.frame(percent1 = c(2,3,4,3,1,5),
percent2 = c(1,2,0.5,1,1,1),
row.names = c("A female", "A male", "B female", "B male", "C female", "C male"))
dfr$category <- row.names(dfr)
mdfr<-melt(dfr, id.vars="category")
plot <- ggplot(mdfr, aes(category, value, fill=variable)) +
geom_bar(stat = "identity")
geom_bar(stat = "identity")
is the more verbose equivalent ofgeom_col()
. – teunbrand