【发布时间】:2014-02-01 00:28:44
【问题描述】:
我在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="")))
这就是我的情节目前的样子:
我确信这很简单,但是在浏览了论坛并查阅了许多关于 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")
【问题讨论】:
-
@Beasterfield,是的,谢谢!
position =position_dodge(width=0.9)成功了...