2015-01-22 2 views
2

위의 막대 그래프 막대 위에 각 변수에 대한 관측 수를 추가하고 싶습니다. 여기 내 데이터 프레임입니다.ggplot을 사용하여 막대 그래프의 막대 그래프 관측 번호

means <- structure(list(Driver = c("Crop agriculture", "Crop agriculture", 
"Infrastructure", "Infrastructure", "Mining", "Mining", "Mixed Agriculture", 
"Mixed Agriculture", "Other land use", "Other land use", "Pasture", 
"Pasture", "Tree crops", "Tree crops", "Water", "Water"), Period = c("1990-2000", 
"2000-2005", "1990-2000", "2000-2005", "1990-2000", "2000-2005", 
"1990-2000", "2000-2005", "1990-2000", "2000-2005", "1990-2000", 
"2000-2005", "1990-2000", "2000-2005", "1990-2000", "2000-2005" 
), mean = c(36.2697273704497, 61.3311804191792, 28.7523209391483, 
30.955220240622, 49.4900570536558, 41.5037095947389, 13.1454310847706, 
10.3642833385884, 28.5871996967629, 23.3988064930454, 53.9768942854543, 
61.3460606144189, 17.326, 16.1278503954073, 36.6165841628849, 
32.6896740558384), n = c("n = 1669", "n = 783", "n = 298", "n = 151", 
"n = 20", "n = 7", "n = 1355", "n = 925", "n = 1623", "n = 851", 
"n = 10986", "n = 6039", "n = 316", "n = 211", "n = 466", "n = 244" 
)), .Names = c("Driver", "Period", "mean", "n"), class = "data.frame", row.names = c(NA, 
-16L)) 

이 데이터 프레임을 기반으로하고 싶은 점은 무엇입니까?

means.barplot <- ggplot(means, aes(x = Driver, y = mean, fill = Period, width = .85)) + 
geom_bar(position = "dodge", stat = "identity") + 
labs(x = "", y = "EF (T/ha)") + 
theme(axis.text = element_text(size = 16), 
     axis.title = element_text(size = 20), 
     legend.title = element_text(size = 20, face = "bold"), 
     legend.text = element_text(size = 20), 
     axis.line = element_line(colour = "black")) + 
scale_fill_grey("Period") + 
scale_y_continuous(limits = c(0, 70)) + 
theme_classic(base_size = 20, base_family = "") + 
theme(panel.grid.minor = element_line(colour =" grey", size = 0.5)) 

그러나이 플롯에는 각 변수 (이 경우 각 기간의 드라이버)에 대한 관측자 수를 더하고 싶습니다. 아직 그 일을 처리하지 못했습니다. 누군가 나를 도와 줄 수 있습니까? 감사.

답변

2

한 가지 방법은 ggplot_build()annotate()을 사용하는 것입니다. 한 번 ggplot 객체를 만들어 x 및 y 위치 값을 가져 오려고합니다. 여기에서 객체는 g입니다. ggplot_build(objectname)$data[[1]]을 사용하면 필요한 값을 얻을 수 있습니다. annotate()에서 foo의 x와 y 값과 원하는 라벨 (예 : mydf $ n)을 사용합니다. 추가로 x 축 레이블을 수정하려면 theme(axis.text.x = element_text(angle = 45, hjust = 1))을 추가했습니다.

g <- ggplot(mydf, aes(x = Driver, y = mean, fill = Period, width = .85)) + 
    geom_bar(position = "dodge", stat = "identity") + 
    labs(x = "", y = "EF (T/ha)") + 
    theme(axis.text = element_text(size = 16), 
      axis.title = element_text(size = 20), 
      legend.title = element_text(size = 20, face = "bold"), 
      legend.text= element_text(size=20), 
      axis.line = element_line(colour = "black")) + 
      scale_fill_grey("Period") + 
      scale_y_continuous(limits=c(0,70)) + 
    theme_classic(base_size = 20, base_family = "") + 
    theme(panel.grid.minor = element_line(colour = "grey", size = 0.5)) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) 


### Create a data frame for annotate() 
foo <- ggplot_build(g)$data[[1]] 

g + annotate("text", x = foo$x, y = foo$y + 1, label = mydf$n, size = 3) 

enter image description here

관련 문제