I am trying to move the horizontal position of the bars in my ggplot2 stacked bar chart so that the x-axis tick marks are aligned with the left-hand edges of the bars rather than being centered
Example:
data1
Status Age Percent
1 low 8.0 19.04762
2 low 8.5 30.23256
3 low 9.0 38.16794
4 intermediate 8.0 45.23810
5 intermediate 8.5 39.53488
6 intermediate 9.0 25.95420
7 high 8.0 35.71429
8 high 8.5 30.23256
9 high 9.0 35.87786
Figure 1 aligns the bars correctly but changes the bar length (should sum to 100%) and y axis length (maximum should be 100%)
Figure1 <- ggplot() +
geom_bar(
aes(y = Percent, x = Age, fill = Status),
data = data1, stat = "identity", width = 0.3,
position = position_nudge(x = 0.25)
) +
xlim(7.5, 9.5)
Figure 2 plots the data correctly but bars are centred on y axis tick marks
Figure2 <- ggplot() +
geom_bar(
aes(y = Percent, x = Age, fill = Status),
data = data1, stat = "identity", width = 0.3
) +
xlim(7.5, 9.5)
I want a plot in which the data are portrayed correctly and the left-hand edges of the bars are aligned with the y-axis tick marks
Setting just = 0
(default is 0.5) will align the x-axis tick marks with the left edges of the columns.
library(ggplot2)
ggplot(data1, aes(Age, Percent, fill = Status)) +
geom_bar(just = 0, stat = 'identity')