I have a data.table in R with three columns: group, low, and llarge. I’ve converted this into a long format using melt, and now I want to create a ggplot barplot with the group labels centered between two mirrored barplots – one for low and another for large.
example:
library(data.table)
library(reshape2)
df <- data.table(group = c("A", "B", "C"),
low = c(1, 2, 3),
llarge = c(4, 5, 6))
df.melt <- melt(DT, id.vars = "group", variable.name = "size", value.name = "quantity")
I have tried:
df.melt$size <- ifelse(df.melt$size == "low", -df.melt$quantity, df.melt$quantity)
p <- ggplot(df.melt, aes(y = reorder(group, size), x = size, fill = size > 0)) + geom_bar(stat = "identity") ++ labs(x = "Quantity", y = "Group", fill = "Size",
title = "Barplot of low and large") +
theme_minimal()
Basically this meets my requirements, however, further I would like to place the group label of the Y-axis at x=0
like:
(a picture from Google)