1

I've a problem plotting a simple bar plot in ggplot2. I'd like to display labels of values above each bar, but am using postion=position_dodge to plot bars along side each other as I have multiple factor variables along my x-axis for a number of data points which refer to individual countries.

Currently using the following code I am getting all values centred over the name of the COUNTRY along my x-axis, whereas I'd like each value centred over each bar along the x-axis:

ggplot(maln_complete_recent_melt, aes(x=COUNTRY, y=percent, fill=location)) + geom_bar(position="dodge", colour="black") +labs(title="Percentage of malnoursihed children according to height for age",  fill="DHS Survey") + geom_text(aes(label=round(percent))) + scale_x_discrete(labels=c(paste(maln_complete_recent_melt$COUNTRY, maln_complete_recent_melt$Year, sep="")))

And this is how my plot currently looks:

enter image description here

I'm sure this is something really simple, but having trawled the forums and consulted a number of books on R, I can't find what I'm looking for.


Here's my data for replication:

structure(list(COUNTRY = structure(c(4L, 10L, 5L, 8L, 14L, 13L, 
9L, 2L, 11L, 1L, 6L, 7L, 12L, 3L, 4L, 10L, 5L, 8L, 14L, 13L, 
9L, 2L, 11L, 1L, 6L, 7L, 12L, 3L, 4L, 10L, 5L, 8L, 14L, 13L, 
9L, 2L, 11L, 1L, 6L, 7L, 12L, 3L, 4L, 10L, 5L, 8L, 14L, 13L, 
9L, 2L, 11L, 1L, 6L, 7L, 12L, 3L, 4L, 10L, 5L, 8L, 14L, 13L, 
9L, 2L, 11L, 1L, 6L, 7L, 12L, 3L, 4L, 10L, 5L, 8L, 14L, 13L, 
9L, 2L, 11L, 1L, 6L, 7L, 12L, 3L), class = "factor", .Label = c("Swaziland", 
"Namibia", "Zimbabwe", "Comoros", "Kenya", "Tanzania", "Uganda", 
"Lesotho", "Mozambique", "Ethiopia", "Rwanda", "Zambia", "Malawi", 
"Madagascar")), Year = structure(c(5L, 23L, 20L, 21L, 20L, 14L, 
12L, 18L, 15L, 18L, 14L, 17L, 19L, 22L, 5L, 23L, 20L, 21L, 20L, 
14L, 12L, 18L, 15L, 18L, 14L, 17L, 19L, 22L, 5L, 23L, 20L, 21L, 
20L, 14L, 12L, 18L, 15L, 18L, 14L, 17L, 19L, 22L, 5L, 23L, 20L, 
21L, 20L, 14L, 12L, 18L, 15L, 18L, 14L, 17L, 19L, 22L, 5L, 23L, 
20L, 21L, 20L, 14L, 12L, 18L, 15L, 18L, 14L, 17L, 19L, 22L, 5L, 
23L, 20L, 21L, 20L, 14L, 12L, 18L, 15L, 18L, 14L, 17L, 19L, 22L
), class = "factor", .Label = c("1992", "1993", "1994", "1995", 
"1996", "1997", "1998", "1999", "2000", "2000/1", "2001/2", "2003", 
"2003/4", "2004", "2005", "2005/6", "2006", "2006/7", "2007", 
"2008/9", "2009", "2010", "2011")), location = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L), .Label = c("Urban", "Rural", "Total", "Capital.City", 
"Urban.Non.slum", "Urban.Slum"), class = "factor"), percent = c(29.9107142857143, 
31.5, 26.4, 30.2, 43.4, 37.8, 29.2, 23.8, 33.1, 23.1, 25.8, 25.5, 
39, 27.5, 35.0071736011478, 46.2, 37.1, 41, 50.9, 49.2, 45.7, 
31.4, 47.2501049401341, 30, 40.5, 39.5, 47.9, 33.4, 33.8, 44.4, 
35.3, 39.2, 50.1, 47.8, 41, 29, 45.3, 28.9, 37.7, 38.1, 45.4, 
32, NA, 22, 28.5, 31.7, 46.8, NA, 20.6, NA, 29.2, NA, 16.9, NA, 
37.2, 29, 21.6666666666667, 10.7799374501425, 16.0100871430598, 
21.2511075992408, 23.7772452013661, 23.8620542603877, 8.15543422154615, 
12.5321762341041, 13.5923403336176, 13.0494984481826, 16.4644101423357, 
21.5426476162423, 27.4144189089535, 16.2492480072194, 32.9268292682927, 
27.8790407698325, 25.0985529949154, 26.2950824206824, 37.9693371617388, 
40.5197947988247, 29.3593820697417, 28.5099311066166, 36.0242620139593, 
20.7853672439948, 28.5877166151249, 22.8001055045257, 35.5434627654004, 
28.0633523545878)), .Names = c("COUNTRY", "Year", "location", 
"percent"), row.names = c(NA, -84L), class = "data.frame")
B314005
  • 53
  • 9
marty_c
  • 5,779
  • 5
  • 24
  • 27

2 Answers2

0

The answer was in the following post: Position geom_text on dodged barplot

Just needed to add the position=position_dodge call into geom_text() as follows:

ggplot(maln_complete_recent_melt, aes(x=COUNTRY, y=percent, fill=location)) + geom_bar(position="dodge", colour="black") +labs(title="Percentage of malnoursihed children according to height for age",  fill="DHS Survey") + geom_text(aes(label=round(percent)), position = position_dodge(width=0.9), vjust=-0.5) + scale_x_discrete(labels=c(paste(maln_complete_recent_melt$COUNTRY, maln_complete_recent_melt$Year, sep="")))

Which produced this:

enter image description here

Community
  • 1
  • 1
marty_c
  • 5,779
  • 5
  • 24
  • 27
0

@marty_c answered your question. If you're not required to use this format, though, you might consider facets (using df = maln_complete_recent_melt):

ggplot(df, aes(x=COUNTRY, y=percent, fill=location)) + 
  geom_histogram(stat="identity") +
  labs(title="Percentage of malnoursihed children according to height for age",  fill="DHS Survey") + 
  geom_text(aes(label=round(percent),y=0),vjust=-0.5,size=4)+
  scale_x_discrete(labels=c(paste(df$COUNTRY, df$Year, sep="")))+
  theme(axis.text.x=element_text(angle=-90, color="black",hjust=0,vjust=0.2))+
  facet_grid(location~.)

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Hey thanks for this, I hadn't thought to look at the data in this way, but can see how it's a good alternative to my approach. Actually a lot easier to compare between locations! cheers – marty_c Jan 13 '14 at 16:13