2014-01-13 2 views
1

간단한 막대 그림을 ggplot2에 플로팅하는 데 문제가 있습니다. 각 막대 위에 값의 레이블을 표시하고 싶지만 각 국가를 참조하는 여러 개의 데이터 요소에 대해 내 x 축을 따라 여러 요소 변수가있는 경우 서로 나란히 따라 플롯 막대에 postion=position_dodge을 사용하고 있습니다.피할 수있는 막대 그래프 값

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

현재 내가 x 축을 따라 각 막대 위에 중심 각 값을 원하는 반면, 내 X 축을 따라 COUNTRY의 이름 위에 중심으로 모든 값을 얻고 다음 코드를 사용하여

는 그리고 이것은 내 음모 현재 모습입니다 :

enter image description here

나는 이것이 정말 간단한 일이지만, 포럼을 저인망 및 R 책의 번호를 상담 한거야, 내가 무엇을 찾을 수없는 I 찾고있어.


여기에 복제를 위해 내 데이터의 :

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

이 http://stackoverflow.com/questions/6017460/ggplot에서보세요 -geom-text-with-dodged-barplot – Beasterfield

+0

@ Beasterfield, 네, 고마워요! 'position = position_dodge (width = 0.9)'가 트릭을 만들었습니다. –

답변

0

대답은 다음 게시물에 있었다 : Position geom_text on dodged barplot

그냥 다음과 같이() geom_text에 위치 = position_dodge 호출을 추가하는 데 필요한 :

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

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~.) 

+0

안녕하세요.이 방법으로 데이터를 볼 생각은 없었지만 내 접근 방식에 대한 좋은 대안이 무엇인지 확인할 수 있습니다. 사실 여러 위치를 비교하는 것이 훨씬 쉽습니다! 건배 –