2016-07-05 7 views
1

にfacet_gridで位置をラベル:ここでがデータを調整すると、私はRで、この例を持ってggplot2

パッケージ

install.packages("plyr") 
library(plyr) 

install.packages("ggplot2") 
library(ggplot2) 

install.packages("ggthemes") 
library(ggthemes) 

私のコードです:

uf <- c("AC","AC","AC","AC","AC","AC","AC","AC","AM","AM","AM", 
"AM","AM","AM","AM","AM") 

    da <- c("Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada") 

    tr <- c(97,99,90.5,78.6,3,1,9.5,21.4,97.2,99.1,96.8,98.7,2.8,0.9,3.2,1.3) 

    resposta <- c("resposta","resposta","resposta","resposta","não resposta","não resposta","não resposta","não resposta","resposta","resposta","resposta","resposta", 
"não resposta","não resposta","não resposta","não resposta") 

    taxa <- data.frame(uf, da, tr,resposta) 

######_Adjusting data labels position_############### 
    taxa <- ddply(taxa, .(da), transform, pos = cumsum(tr) - (0.5 * tr)) # Ajusta a posição 

    g1 <- ggplot()+ geom_bar(aes(y = tr, x = da, fill = resposta), data = taxa, 
          stat="identity") + 
       facet_wrap(~uf) + 
       geom_text(data=taxa, aes(x = da, y = pos, label = tr), size=4) + 
       theme(legend.position="bottom", legend.direction="horizontal", 
       legend.title = element_blank()) 

    g1 

この数字を生産: Question for dependencia with facet for uf

私はすべての値でこの数字を作りたいと思いますバーの中で。 y軸を100

助けていただければ幸いです。

答えて

0

私はあなたのコードを少し整えて、ifelseを使ってラベルの正しい位置を決定しました。

Library(ggplot2) 

uf <- c("AC","AC","AC","AC","AC","AC","AC","AC","AM","AM","AM", 
     "AM","AM","AM","AM","AM") 

da <- c("Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada") 

tr <- c(97,99,90.5,78.6,3,1,9.5,21.4,97.2,99.1,96.8,98.7,2.8,0.9,3.2,1.3) 

resposta <- c("resposta","resposta","resposta","resposta","não resposta","não resposta","não resposta","não resposta","resposta","resposta","resposta","resposta", 
       "não resposta","não resposta","não resposta","não resposta") 

taxa <- data.frame(uf, da, tr,resposta) 

######_Adjusting data labels position_############### 

taxa$pos <- ifelse(taxa$tr > 50, taxa$tr/2, 100 - taxa$tr/2) 

ggplot(taxa, aes(y = tr, x = da, fill = resposta, label = tr)) + 
    geom_bar(stat="identity") + 
    geom_text(aes(y = pos), size=4) + 
    facet_wrap(~uf) + 
    theme(legend.position="bottom", legend.direction="horizontal", 
     legend.title = element_blank()) 

enter image description here

+0

こんにちは、アレックス、ありがとうございます。今、私は仕事を終えることができます – Tadeu

関連する問題