简体   繁体   中英

How can I organize facets in descending order with ggplot2?

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.

在此处输入图片说明

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"))

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")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM