0

Sorry for my bad english skills. I will make many grammar ou spelling errors. I apologize for it.

I have some non numeric data like :

#Used library
library(ggplot2)
library(scales)

#Definition of data
graph<-cbind.data.frame(
    sample(c("FI", "FA","FC"), 100, replace = TRUE),
    sample(c("Rep","Dip"),100, replace=TRUE)
)
names(graph)<- c("RegimeInscription","repondant")

I look after a graph like this :graph barplot position fill with percent value in bar from this question I follow the explanation but it is not valid for discrete data on X axis.

#Some var
theme_barre <- theme_bw()+
  theme(
    panel.grid=element_blank(),
    plot.title=element_text(colour = 'red', size=10, face="bold", hjust = 0.5, vjust = 0.5),
    legend.text=element_text(size=9),
    legend.key.size = unit(2, "mm"),
    panel.background = element_rect(fill = "transparent",colour = NA), # or theme_blank()
    plot.background  = element_rect(fill = "transparent",colour = NA),
    panel.grid.minor = element_blank()#, 
    )
texte <- geom_text(
        #mapping = aes(y = (..count.. / sum(..count..))/ c(2, 1) , label = paste0(round(..count.. / sum(..count..) * 100), "%") ),
        mapping = aes(label = paste0(round(..count.. / sum(..count..) * 100), "%") ),
        stat = "count",
        na.rm=TRUE,
)

#Make the graph
    ggplot(graph, aes(x = factor(repondant),fill = factor(RegimeInscription) )) +#Initiation du graphique
    geom_bar(position = "fill") +#Realisation du graphique
    theme_barre + #fond blanc
    coord_flip()+ #barre horizontale
    scale_fill_discrete( name=" ", labels=c("FI"="Fi","FA"="Fa","FC"="Fc") )+
    labs(x=" ", y=" ")+
    scale_y_continuous(breaks=seq(0,1,0.1), labels=percent)+
    ggtitle("Régime d'inscription")+
    texte

I think the issue come from the texte value the comment with y=... is not a solution because I have many graph with the same value and different length. The graph are made automaticaly on different data so I can not specify some value.

Many thanks

Hedjour
  • 101
  • 1
  • 8
  • On your proposal I modify the texte var with y = cumsum((..count.. / sum(..count..)))-0.5*(..count.. / sum(..count..)) Unfortunately it doesn't work – Hedjour Sep 02 '16 at 11:03
  • 1
    It would be better for you to make the data summarize. `library(dplyr); tbl <- graph %>% table() %>% prop.table(2) %>% data.frame() %>% ; group_by(repondant) %>% mutate(pos = cumsum(Freq) - (0.5 * Freq))`. Then refer to [Showing data values on stacked bar chart in ggplot2](http://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2) . – cuttlefish44 Sep 02 '16 at 12:25
  • Many thanks @cuttlefish44 your comment save me – Hedjour Sep 02 '16 at 15:07

0 Answers0