躲避的条形图值

时间:2014-01-13 09:55:53

标签: r graph ggplot2 bar-chart

我在ggplot2中绘制一个简单的条形图时遇到了问题。我想在每个条形图上面显示值的标签,但我使用postion=position_dodge绘制彼此相邻的条形图,因为我的x轴上有多个因子变量,用于指向各个国家/地区的多个数据点

目前使用以下代码我将所有值都放在{x}轴上COUNTRY的名称中心,而我希望每个值都沿着x轴居中在每个条上:

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="")))

这就是我的情节目前的样子:

enter image description here

我确信这是非常简单的事情,但是在浏览论坛并咨询了R上的一些书籍后,我无法找到我正在寻找的东西。


这是我的复制数据:

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")

2 个答案:

答案 0 :(得分:0)

答案如下:Position geom_text on dodged barplot

只需将position = position_dodge调用添加到geom_text()中,如下所示:

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="")))

产生了这个:

enter image description here

答案 1 :(得分:0)

@marty_c回答了你的问题。但是,如果您不需要使用此格式,则可以考虑构面(使用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~.)