0

This question was flagged as already answered some time ago and someone redirected me to this page. However, I have no knowledge of Tidyverse (and the data itself looks different) so I've dedided to give it another shot by reformulating this post.

In short, I have successfully made a stacked barplot in R where the percentages add up to 100% for several different categories. The dataframe looks like this:

sujeito teste epentese vozeamento palavra tipo  ortografia
   <chr>   <chr> <chr>    <chr>      <chr>   <chr> <chr>     
 1 a       n     1        0          cats    ts    cs        
 2 b       l     1        1          ducks   ks    cs        
 3 c       l     1        1          cups    ps    cs        
 4 d       l     0        0          grapes  ps    ces       
 5 e       l     1        0          lakes   ks    ces       
 6 f       n     1        0          gates   ts    ces       
 7 g       n     0        0          books   ks    cs        
 8 h       n     1        0          cakes   ks    ces       
 9 a       n     1        1          kites   ts    ces       
10 b       n     1        0          boats   ts    cs     

I used ggplot2 and deplyr to make a stacked barplot displaying frequencies. Since I need to display two independent variables, I used facet_wrap to organize visualization. I used the following code:

dados%>%
  group_by(ortografia, tipo, epentese)%>%
  summarise(quantidade = n())%>%
  mutate(frequencia = quantidade/sum(quantidade))%>%
  ggplot(., aes(x = tipo, y = frequencia, fill = epentese))+
  geom_col(position = position_fill(reverse=TRUE))+
  geom_text(aes(label = if_else(epentese == 0, scales::percent(frequencia, accuracy = 1), "")), vjust = 0, nudge_y = .01) +
  scale_y_continuous(labels=scales::percent)+
  facet_wrap(~ortografia)+
  labs(title = "Epenthesis rates by ortographic pattern and cluster type", subtitle = "Orthographic Pattern")+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(plot.subtitle = element_text(hjust = 0.5))+
  xlab("Cluster Type")+ylab("Frequency")

However, my plot ends up like the left side of the following image, not like the right side (which is my goal). I'm afraid that using different facets complicates things a little bit.

enter image description here

Could someone help this humble user organize these facets in descending order ?

EDIT: Here's the output of dput, as requested:

> dput(dados)
structure(list(sujeito = c("a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "d", "d", "d", "d", "d", "d", "d", 
"d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", 
"d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "d", 
"d", "d", "d", "d", "d", "d", "d", "d", "d", "d", "e", "e", "e", 
"e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", 
"e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", 
"e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", 
"e", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", 
"f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", 
"f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", 
"f", "f", "f", "f", "f", "g", "g", "g", "g", "g", "g", "g", "g", 
"g", "g", "g", "g", "g", "g", "g", "g", "g", "g", "g", "g", "g", 
"g", "g", "g", "g", "g", "g", "g", "g", "g", "g", "g", "g", "g", 
"g", "g", "g", "g", "g", "g", "g", "g", "g", "g", "h", "h", "h", 
"h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", 
"h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", 
"h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", 
"h"), teste = c("n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", 
"l", "l", "l", "l", "l", "l", "l", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", 
"l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "l", "l", "l", "l", "l", 
"l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", 
"l", "l", "l", "l", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", 
"l", "l", "l", "l", "l", "l", "l", "l", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "l", "l", "l", "l", "l", "l", "l", "l", "l", 
"l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "l", "l", "l", "l", "l", "l", 
"l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", 
"l", "l", "l", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "l", 
"l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", 
"l", "l", "l", "l", "l", "l", "l", "l", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "l", "l", "l", "l", "l", "l", "l", "l", "l", 
"l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l"), 
    epentese = c("1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "0", "1", "1", "1", "0", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "0", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "0", "0", "1", "1", "0", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", 
    "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "0", "1", "1", "1", "0", "0", "1", "1", "1", "0", "0", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", 
    "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", "0", 
    "0"), vozeamento = c("0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "1", "0", "1", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "0", 
    "1", "1", "0", "1", "1", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "1", "1", "1", "1", "0", "1", "1", "0", "1", 
    "1", "0", "1", "0", "1", "0", "1", "1", "1", "1", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "1", "0", 
    "0", "0", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "1", "0", "0", "1", "1", "1", "0", "0", "1", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "1", "0", "0", "0", "0", "1", "1", "0", "0", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "0", 
    "0", "1", "0", "0", "0", "1", "1", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", 
    "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0"), palavra = c("cats", "ducks", "cups", "grapes", 
    "lakes", "gates", "books", "cakes", "kites", "boats", "ropes", 
    "maps", "eggs", "globes", "jobs", "beds", "kids", "pubs", 
    "pigs", "tubes", "sides", "codes", "pubs", "beds", "kids", 
    "eggs", "pigs", "tubes", "globes", "sides", "codes", "maps", 
    "cats", "boats", "ducks", "books", "grapes", "ropes", "gates", 
    "kites", "cakes", "lakes", "cats", "ducks", "cups", "grapes", 
    "lakes", "gates", "books", "cakes", "kites", "boats", "ropes", 
    "maps", "eggs", "globes", "jobs", "beds", "kids", "pubs", 
    "pigs", "tubes", "sides", "codes", "jobs", "pubs", "beds", 
    "kids", "eggs", "pigs", "tubes", "globes", "sides", "codes", 
    "cups", "maps", "cats", "boats", "ducks", "books", "grapes", 
    "ropes", "gates", "kites", "cakes", "lakes", "cats", "ducks", 
    "cups", "grapes", "lakes", "gates", "books", "cakes", "kites", 
    "boats", "ropes", "maps", "eggs", "globes", "jobs", "beds", 
    "kids", "pubs", "pigs", "tubes", "sides", "codes", "jobs", 
    "pubs", "beds", "kids", "eggs", "pigs", "tubes", "globes", 
    "sides", "codes", "cups", "maps", "cats", "boats", "ducks", 
    "books", "grapes", "ropes", "gates", "kites", "cakes", "lakes", 
    "cats", "ducks", "cups", "grapes", "lakes", "gates", "books", 
    "cakes", "kites", "boats", "ropes", "maps", "eggs", "globes", 
    "jobs", "beds", "kids", "pubs", "pigs", "tubes", "sides", 
    "codes", "jobs", "pubs", "beds", "kids", "eggs", "pigs", 
    "tubes", "globes", "sides", "codes", "cups", "maps", "cats", 
    "boats", "ducks", "books", "grapes", "ropes", "gates", "cakes", 
    "lakes", "cats", "ducks", "cups", "grapes", "lakes", "gates", 
    "books", "cakes", "kites", "boats", "ropes", "maps", "eggs", 
    "globes", "jobs", "beds", "kids", "pubs", "pigs", "tubes", 
    "sides", "codes", "jobs", "pubs", "beds", "kids", "eggs", 
    "pigs", "globes", "sides", "codes", "cups", "maps", "cats", 
    "boats", "ducks", "books", "grapes", "ropes", "gates", "kites", 
    "cakes", "lakes", "cats", "ducks", "cups", "grapes", "lakes", 
    "gates", "books", "cakes", "boats", "ropes", "maps", "eggs", 
    "globes", "jobs", "beds", "kids", "pubs", "pigs", "tubes", 
    "sides", "codes", "jobs", "pubs", "beds", "kids", "eggs", 
    "pigs", "tubes", "globes", "sides", "codes", "cups", "maps", 
    "cats", "boats", "ducks", "books", "grapes", "ropes", "gates", 
    "kites", "cakes", "lakes", "cats", "ducks", "cups", "grapes", 
    "lakes", "gates", "books", "cakes", "kites", "boats", "ropes", 
    "maps", "eggs", "globes", "jobs", "beds", "kids", "pubs", 
    "pigs", "tubes", "sides", "codes", "jobs", "pubs", "beds", 
    "kids", "eggs", "pigs", "tubes", "globes", "sides", "codes", 
    "cups", "maps", "cats", "boats", "ducks", "books", "grapes", 
    "ropes", "gates", "kites", "cakes", "lakes", "cats", "ducks", 
    "cups", "grapes", "lakes", "gates", "books", "cakes", "kites", 
    "boats", "ropes", "maps", "eggs", "globes", "jobs", "beds", 
    "kids", "pubs", "pigs", "tubes", "sides", "codes", "jobs", 
    "pubs", "beds", "eggs", "pigs", "tubes", "globes", "sides", 
    "codes", "cups", "maps", "cats", "boats", "ducks", "books", 
    "grapes", "ropes", "gates", "kites", "cakes", "lakes"), tipo = c("ts", 
    "ks", "ps", "ps", "ks", "ts", "ks", "ks", "ts", "ts", "ps", 
    "ps", "gz", "bz", "bz", "dz", "dz", "bz", "gz", "bz", "dz", 
    "dz", "bz", "dz", "dz", "gz", "gz", "bz", "bz", "dz", "dz", 
    "ps", "ts", "ts", "ks", "ks", "ps", "ps", "ts", "ts", "ks", 
    "ks", "ts", "ks", "ps", "ps", "ks", "ts", "ks", "ks", "ts", 
    "ts", "ps", "ps", "gz", "bz", "bz", "dz", "dz", "bz", "gz", 
    "bz", "dz", "dz", "bz", "bz", "dz", "dz", "gz", "gz", "bz", 
    "bz", "dz", "dz", "ps", "ps", "ts", "ts", "ks", "ks", "ps", 
    "ps", "ts", "ts", "ks", "ks", "ts", "ks", "ps", "ps", "ks", 
    "ts", "ks", "ks", "ts", "ts", "ps", "ps", "gz", "bz", "bz", 
    "dz", "dz", "bz", "gz", "bz", "dz", "dz", "bz", "bz", "dz", 
    "dz", "gz", "gz", "bz", "bz", "dz", "dz", "ps", "ps", "ts", 
    "ts", "ks", "ks", "ps", "ps", "ts", "ts", "ks", "ks", "ts", 
    "ks", "ps", "ps", "ks", "ts", "ks", "ks", "ts", "ts", "ps", 
    "ps", "gz", "bz", "bz", "dz", "dz", "bz", "gz", "bz", "dz", 
    "dz", "bz", "bz", "dz", "dz", "gz", "gz", "bz", "bz", "dz", 
    "dz", "ps", "ps", "ts", "ts", "ks", "ks", "ps", "ps", "ts", 
    "ks", "ks", "ts", "ks", "ps", "ps", "ks", "ts", "ks", "ks", 
    "ts", "ts", "ps", "ps", "gz", "bz", "bz", "dz", "dz", "bz", 
    "gz", "bz", "dz", "dz", "bz", "bz", "dz", "dz", "gz", "gz", 
    "bz", "dz", "dz", "ps", "ps", "ts", "ts", "ks", "ks", "ps", 
    "ps", "ts", "ts", "ks", "ks", "ts", "ks", "ps", "ps", "ks", 
    "ts", "ks", "ks", "ts", "ps", "ps", "gz", "bz", "bz", "dz", 
    "dz", "bz", "gz", "bz", "dz", "dz", "bz", "bz", "dz", "dz", 
    "gz", "gz", "bz", "bz", "dz", "dz", "ps", "ps", "ts", "ts", 
    "ks", "ks", "ps", "ps", "ts", "ts", "ks", "ks", "ts", "ks", 
    "ps", "ps", "ks", "ts", "ks", "ks", "ts", "ts", "ps", "ps", 
    "gz", "bz", "bz", "dz", "dz", "bz", "gz", "bz", "dz", "dz", 
    "bz", "bz", "dz", "dz", "gz", "gz", "bz", "bz", "dz", "dz", 
    "ps", "ps", "ts", "ts", "ks", "ks", "ps", "ps", "ts", "ts", 
    "ks", "ks", "ts", "ks", "ps", "ps", "ks", "ts", "ks", "ks", 
    "ts", "ts", "ps", "ps", "gz", "bz", "bz", "dz", "dz", "bz", 
    "gz", "bz", "dz", "dz", "bz", "bz", "dz", "gz", "gz", "bz", 
    "bz", "dz", "dz", "ps", "ps", "ts", "ts", "ks", "ks", "ps", 
    "ps", "ts", "ts", "ks", "ks"), ortografia = c("cs", "cs", 
    "cs", "ces", "ces", "ces", "cs", "ces", "ces", "cs", "ces", 
    "cs", "cs", "ces", "cs", "cs", "cs", "cs", "cs", "ces", "ces", 
    "ces", "cs", "cs", "cs", "cs", "cs", "ces", "ces", "ces", 
    "ces", "cs", "cs", "cs", "cs", "cs", "ces", "ces", "ces", 
    "ces", "ces", "ces", "cs", "cs", "cs", "ces", "ces", "ces", 
    "cs", "ces", "ces", "cs", "ces", "cs", "cs", "ces", "cs", 
    "cs", "cs", "cs", "cs", "ces", "ces", "ces", "cs", "cs", 
    "cs", "cs", "cs", "cs", "ces", "ces", "ces", "ces", "cs", 
    "cs", "cs", "cs", "cs", "cs", "ces", "ces", "ces", "ces", 
    "ces", "ces", "cs", "cs", "cs", "ces", "ces", "ces", "cs", 
    "ces", "ces", "cs", "ces", "cs", "cs", "ces", "cs", "cs", 
    "cs", "cs", "cs", "ces", "ces", "ces", "cs", "cs", "cs", 
    "cs", "cs", "cs", "ces", "ces", "ces", "ces", "cs", "cs", 
    "cs", "cs", "cs", "cs", "ces", "ces", "ces", "ces", "ces", 
    "ces", "cs", "cs", "cs", "ces", "ces", "ces", "cs", "ces", 
    "ces", "cs", "ces", "cs", "cs", "ces", "cs", "cs", "cs", 
    "cs", "cs", "ces", "ces", "ces", "cs", "cs", "cs", "cs", 
    "cs", "cs", "ces", "ces", "ces", "ces", "cs", "cs", "cs", 
    "cs", "cs", "cs", "ces", "ces", "ces", "ces", "ces", "cs", 
    "cs", "cs", "ces", "ces", "ces", "cs", "ces", "ces", "cs", 
    "ces", "cs", "cs", "ces", "cs", "cs", "cs", "cs", "cs", "ces", 
    "ces", "ces", "cs", "cs", "cs", "cs", "cs", "cs", "ces", 
    "ces", "ces", "cs", "cs", "cs", "cs", "cs", "cs", "ces", 
    "ces", "ces", "ces", "ces", "ces", "cs", "cs", "cs", "ces", 
    "ces", "ces", "cs", "ces", "cs", "ces", "cs", "cs", "ces", 
    "cs", "cs", "cs", "cs", "cs", "ces", "ces", "ces", "cs", 
    "cs", "cs", "cs", "cs", "cs", "ces", "ces", "ces", "ces", 
    "cs", "cs", "cs", "cs", "cs", "cs", "ces", "ces", "ces", 
    "ces", "ces", "ces", "cs", "cs", "cs", "ces", "ces", "ces", 
    "cs", "ces", "ces", "cs", "ces", "cs", "cs", "ces", "cs", 
    "cs", "cs", "cs", "cs", "ces", "ces", "ces", "cs", "cs", 
    "cs", "cs", "cs", "cs", "ces", "ces", "ces", "ces", "cs", 
    "cs", "cs", "cs", "cs", "cs", "ces", "ces", "ces", "ces", 
    "ces", "ces", "cs", "cs", "cs", "ces", "ces", "ces", "cs", 
    "ces", "ces", "cs", "ces", "cs", "cs", "ces", "cs", "cs", 
    "cs", "cs", "cs", "ces", "ces", "ces", "cs", "cs", "cs", 
    "cs", "cs", "ces", "ces", "ces", "ces", "cs", "cs", "cs", 
    "cs", "cs", "cs", "ces", "ces", "ces", "ces", "ces", "ces"
    )), row.names = c(NA, -346L), class = c("tbl_df", "tbl", 
"data.frame"))
Matt
  • 73
  • 1
  • 8
  • 1
    Please share more data using `dput(dados)`, edit the question and paste the output. The data you added can not allow reproducing your issue! – Duck Nov 17 '20 at 19:18
  • Did the comment recommendations in your [previous question](https://stackoverflow.com/q/64867950/3358272) not work? – r2evans Nov 17 '20 at 19:22
  • yes. Your problem is that your variable 'tipo' is a character so ggplot presents it in alphabetical order. If you change it to an ordered factor then ggplot will respect this. So, e.g. dados$tipo <- factor(dadas$tipo, levels = c('ks', 'ps', 'bz', ... etc), ordered = TRUE) – Ollie Perkins Nov 17 '20 at 19:41
  • 1
    also if you use facet_wrap(~ortografia, scales = 'free'), then it will delete the NA column form the LHS – Ollie Perkins Nov 17 '20 at 19:41
  • 3
    @OlliePerkins, reordering by factors is insufficient here: as an example, `"ks"` is before `"ps"` in the left facet of the "desired" output, but their order is reversed in the right facet. Since those x-axis labels are derived from a single column in the data, a simple one-column `factor` is insufficient. It will take breaking that column into multiple columns (one for each facet) to get the order to be facet-specific. – r2evans Nov 17 '20 at 19:51
  • Ahh yes, fair point. – Ollie Perkins Nov 17 '20 at 19:54

1 Answers1

2

This is a bit harder than it first appears, since the x axis order is different in the two facets. If we want the ordering to be done according to frequency in both facets, we will need to sort by the frequency of the interaction of the x axis and faceting variable, then spoof the x axis by getting it to show only the x axis part of the interaction-variable name:

dados %>%
  group_by(ortografia, tipo) %>%
  summarize(frequencia = length(which(epentese == "0")) /
                         length(which(epentese == "1"))) %>%
  ungroup() %>%
  mutate(tipo2 = interaction(tipo, ortografia)) %>%
  mutate(tipo2 = forcats::fct_reorder(tipo2, -frequencia)) %>%
  group_by(across(everything())) %>%
  summarize(epentese = c("0", "1")) %>%
  mutate(frequencia = ifelse(epentese == "0", frequencia, 1 - frequencia)) %>%
  ungroup() %>%
  ggplot(aes(x = tipo2, y = frequencia, fill = epentese))+
  geom_col(position = position_fill(reverse = TRUE))+
  geom_text(aes(label = if_else(epentese == "0" & frequencia != 0, 
                                scales::percent(frequencia, accuracy = 1), "")), 
            vjust = 0, nudge_y = .01) +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels = function(x) substr(x, 1, 2)) +
  facet_grid(~ortografia, space = "free_x", scales = "free_x") +
  labs(title = "Epenthesis rates by ortographic pattern and cluster type", 
       subtitle = "Orthographic Pattern") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(plot.subtitle = element_text(hjust = 0.5)) +
  xlab("Cluster Type") + 
  ylab("Frequency")

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87