2016-05-02 7 views
0

があります。gogplot2を使ってRのgeom_bar rにラベル/周波数を配置する方法

library(ggplot2); library(scales); library(reshape2); 
da1 <- read.table(text = "NÃO SIM 
5 1 
24 44 
",sep = "",header = TRUE) 
da1m <- melt(cbind(da1, ind = rownames(da1)), id.vars = c('ind')) 
da1m$Resposta <- c("NÃO", "SIM", "NÃO", "SIM") 
names(da1m) <- c("Resposta", "variable", "value") 

ggplot(da1m,aes(x = variable, y = value,fill = Resposta)) + 
geom_bar(position = "fill",stat = "identity", colour = "black") + 
scale_y_continuous(labels = percent_format())+ 
labs(title = "Gráfico 1", x="Gosta de utilizar o R", 
    y="Há interessse em aprimorar os conhecimentos em R")+ 
scale_fill_manual(values=c("#E69F00", "#56B4E9"))+ 
geom_text(aes(label=value), position=position_dodge(width=0.9), 
     vjust=-0.5) 

そして、これは私が得るものです::だから、これは私のコードです

geom_bar with label error

私が間違って何をしているのですか?周波数を追加しようとすると、図表はgeom_textの機能にも変わってきています。説明のため

答えて

0

、ここで位置決めテキストラベルには2つの異なるオプションがありますが、必要に応じて、あなたはこれらを変更することができます。

library(dplyr) 

# Add two position locations for text labels (centered and top) 
da1m = da1m %>% group_by(variable) %>% 
    mutate(y.pos.ctr = cumsum(value) - 0.5*value, 
     y.pos.top = cumsum(value)) 

ggplot(da1m, aes(x=variable, y=value, fill=Resposta)) + 
    geom_bar(stat = "identity", colour = "black") + # get rid of position="fill" 
    #scale_y_continuous(labels = percent_format()) + # y values don't seem to be percentages 
    labs(title = "Gráfico 1", x="Gosta de utilizar o R", 
     y="Há interessse em aprimorar os conhecimentos em R")+ 
    scale_fill_manual(values=c("#E69F00", "#56B4E9")) + 
    geom_text(aes(label=value, y=y.pos.ctr), colour="white") + # Centered value labels 
    geom_text(aes(label=value, y=y.pos.top), colour="red") # Value labels at top of each bar section 

enter image description here

+0

こんにちは!どうもありがとうございました!私はggplotについてまだよく分かりませんが、これはとても役に立ちました:) –

関連する問題