I want to create a barplot for a size range. I present a dummy dataset (data)
Size1 Size2 A B C
0 5 0.3 0.5 0.2
5 10 0.1 0.2 0.7
10 20 0.5 0.2 0.3
20 50 0.2 0.4 0.4
50 100 0.7 0.1 0.2
I can create a plot if there was just one "Size". For example if there was only "Size2" I'd do something like
library(reshape2)
data1 <- melt(data, id.var="Size2")
library(ggplot2)
ggplot(data1, aes(x = Size2, y = value, fill = variable)) +
geom_bar(stat = "identity")
With this I get https://imgur.com/z0zUiwm
However, I want a plot for A, B, C within each size range. So with Size on the x-axis and percent of A, B, and C on the y-axis, how can I proceed further. I want the lines of the barplot to be connected i.e no gaps between bars in the x-axis.
data$Size = paste(data$Size1, data$Size2, sep = "-"), to create a single Size column indicating the range (drop the other size columns when you melt). To make the bars connect, setwidth = 1ingeom_bar- Gregor Thomasgeom_barworks well with categorical x-axes. If you want to use a continuous x-axis, I'd recommend usinggeom_rect. But in that case you'll need to calculate the percentages yourself, I think. - Gregor Thomas