I have the following dataframe
unit <- c("a", "b", "c", "d")
top1990 <- c(100, 80, 70, 90)
base1990 <- c(40, 60, 20, 30)
top2000 <- c(120, 85, 65, 80)
base2000 <- c(40, 65, 25, 15)
df <- data.frame(unit, top1990, base1990, top2000, base2000)
unit top1990 base1990 top2000 base2000
a 100 40 120 40
b 80 60 85 65
c 70 20 65 25
d 90 30 80 15
I need to plot on the same graph, for each unit, two bars, one for the year 1990 and the other one for the year 2000. Each bar should start from the value "base***" and should end at the value "top****". I also need to order units by "top1990" (decreasing order).
I used the function geom_segment of the ggplot2 library, as follows
df$unit <- factor(df$unit, levels = df$unit[order(df$top1990, decreasing = T)])
ggplot(data = df) + geom_segment(aes(x=df$unit, xend=df$unit, y=df$top1990, yend=df$base1990), size = 7, color = "blue") +
geom_segment(aes(x=df$unit, xend=df$unit, y=df$top2000, yend=df$base2000), size = 7, color = "red")
However, I obtained this, in which bars are overlapping, wheareas I would need them side-by-side.
What I am missing?