0

I analysed multiple related questions but couldn't solve the problem.

Here are my data, called fam:

structure(list(familiarity = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("familiar", "not familiar", 
"not sure"), class = "factor"), idiom = structure(c(7L, 8L, 1L, 
5L, 4L, 9L, 3L, 6L, 2L, 6L, 2L, 3L, 4L, 9L, 7L, 8L, 1L, 5L, 9L, 
2L, 3L, 4L, 6L, 7L, 8L, 1L, 5L), .Label = c("be in the same boat", 
"beat one’s chest", "chew the fat", "just around the corner", 
"kick the bucket", "left hanging in the air", "sweep sth under the carpet", 
"take the plunge", "tighten one’s belt"), class = "factor"), 
frequency = c(11L, 11L, 11L, 11L, 10L, 8L, 8L, 7L, 6L, 4L, 
3L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 2L, 2L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L)), .Names = c("familiarity", "idiom", "frequency"), class = "data.frame", 
row.names = c(NA, -27L))

I figured out how to do the stacked bar plot. Here's what I got:

Plot1

Here's my code, so far:

ggplot(data = fam, aes(x = idioms, y = frequency, fill = familiarity, width = 0.5)) + 
    geom_bar(stat = 'identity') + 
    coord_flip() + 
    scale_fill_manual(values = c('limegreen', 'purple', 'red1')) + 
    theme(axis.title.y = element_blank(), 
          axis.title.x = element_text(size = 10), 
          axis.text.x = element_text(size = 10), 
          axis.text.y = element_text(size = 10), 
          legend.text = element_text(size_10), 
          text = element_text(size = 10),
          plot.margin = unit(c(0.3, 0.5, 0.3, 0.8), 'cm')) + 
    scale_y_continuous(breaks = pretty_breaks()) + 
    labs(y = "number of responses") + 
    labs = (fill = "")

I'd like to get an ordered stacked bar with respect to 'familiarity'. In crude terms, I'd like to get the highest familiarity scores (the green bars) first, then 'familiar' and 'not sure', and then 'familiar', 'not sure', 'not familiar'. Basically, descending quality.

I'd be grateful for any suggestions.

  • 1
    Please, use `dput()` for your data. – Marcelo Apr 13 '17 at 01:57
  • 1
    Since you'd already ordered your data.frame in the order your wanted, take that custom order with `custom <- unique(fam$idiom)` and reorder the idiom column based on the custom order with `fam$idiom <- factor(fam$idiom, level=custom)`. – Adam Quek Apr 13 '17 at 03:17
  • Thank you so much @Adam Quek, saved my day. The solution worked perfectly. BTW, I wasn't able to solve my problem looking at related questions. – Maja Gwóźdź Apr 13 '17 at 03:34

0 Answers0