1

I want to add the total numbers per year on top of each bar. I tried with geom_text, but am getting strange output. Unfortunately, I do not understand how geom_text works, so I don't know where to start. I have seen a lot of other posts with similar questions, but they did not help me for the specific plots I have. If I missed anything from them, I want to apologize for potential duplicative efforts. Does anyone know how to specify it correctly?

code that works so far without total on top:

data1 <- data.frame(matrix(
c('Africa', 2011,   54, 1593,
'Africa',   2012,   26, 1604,
'Africa',   2013,   18, 1267,
'Africa',   2014,   20, 846,
'Central America',  2011,   30, 1593,
'Central America',  2012,   32, 1604,
'Central America',  2013,   13, 1267,
'Central America',  2014,   9, 846,
'East Asia',    2011,   1509, 1593,
'East Asia',    2012,   1546, 1604,
'East Asia',    2013,   1236, 1267,
'East Asia',    2014,   817, 846
), nrow=12, ncol=4, byrow=TRUE
))
colnames(data1)<-c('region', 'year', 'number', 'total')
data1$number <- as.numeric(levels(data1$number))
data1$total <- as.numeric(levels(data1$total))

p <- ggplot(data1,
        aes(x = year, y = number, fill = region)) + 
  geom_bar(position = "fill",stat = "identity") +
  scale_y_continuous(labels = percent_format()) +
  labs(x="Year", y="Percentage") +
  theme(panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), 
    panel.background = element_blank(),
    legend.position="top",
    legend.title = element_text(size=10),
    legend.text = element_text(size=10))   
print(p)

code that gives me strange output:

p + geom_text(aes(x = year, y = number, label = total))
Florian Seliger
  • 421
  • 4
  • 16

0 Answers0