# Reorder stacked barplot x based on fill values with ggplot2

A survey with 5 questions is administered. The questions share the same set of possible answers. Here are the data, reshaped for plotting with ggplot2.

``````library(tidyr)
library(magrittr)

data <- data.frame(ID = c(1:500),
q1  = factor(sample(c(1:4), 500, replace = T),
labels = c("A", "B", "C", "D")),
q2  = factor(sample(c(1:4), 500, replace = T),
labels = c("A", "B", "C", "D")),
q3  = factor(sample(c(1:4), 500, replace = T),
labels = c("A", "B", "C", "D")),
q4  = factor(sample(c(1:4), 500, replace = T),
labels = c("A", "B", "C", "D")),
q5  = factor(sample(c(1:4), 500, replace = T),
labels = c("A", "B", "C", "D"))) %>%
gather(question, value, q1:q5)
``````

I want to sort the ordering of the questions based on number of a given response. So instead of this...

``````library(ggplot2)

ggplot(data, aes(x = question , fill = value)) +
geom_bar() +

theme(panel.background = element_rect(fill = "white")) +
scale_fill_manual("Value", values = c("#2171B5", "#6BAED6", "#BDD7E7",
"#EFF3FF"))
`````` ...I want the order of the questions along the x axis to be based on the count of answer = D, for example.

• I would recommend 1) precomputing the quantity in advance; 2) reordering via setting `scale_x_manual`. – tonytonov Jan 30 '15 at 12:11

Got it. Ordered by the number of responses = A, in the example below.

``````  data\$question <- reorder(dataf\$question, data\$value, function(x) max(table(x)))

ggplot(heatDf, aes(x = question, fill = value)) +
geom_bar() +
theme(panel.background = element_rect(fill = "white")) +
scale_fill_manual("", values = c("#2171B5", "#6BAED6", "#BDD7E7",
"#EFF3FF", "grey30"))
`````` • you can also do `ggplot(data, aes(x = factor(question, levels=as.character(count(data, question, value) %>% ungroup %>% filter(value=="D") %>% arrange(n) %>% .\$question), ordered=TRUE) , fill = value))` but it's usually way better to do this outside ggplot like you did here. – hrbrmstr Jan 30 '15 at 12:21
{"ft_lang_label":"__label__en","ft_lang_prob":0.5864188,"math_prob":0.9987491,"size":1127,"snap":"2019-43-2019-47","text_gpt3_token_len":370,"char_repetition_ratio":0.12644702,"word_repetition_ratio":0.22413793,"special_character_ratio":0.40905058,"punctuation_ratio":0.26070037,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99971837,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-20T16:53:04Z\",\"WARC-Record-ID\":\"<urn:uuid:73f06ba0-7d2b-4e71-afc3-8ea4516e0f6b>\",\"Content-Length\":\"141665\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5e9a1b70-f6a6-4b55-ba70-a26aa2401113>\",\"WARC-Concurrent-To\":\"<urn:uuid:3f4545b4-14ed-4f71-85f2-5176f95e4c27>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/28234856/reorder-stacked-barplot-x-based-on-fill-values-with-ggplot2/28235710\",\"WARC-Payload-Digest\":\"sha1:7B6PGE7TMZRSSVF5S44FTPFCCVZAATXF\",\"WARC-Block-Digest\":\"sha1:JMODZYTSFYJB2BTPXST7C7PXE4CQ2ZPV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496670597.74_warc_CC-MAIN-20191120162215-20191120190215-00333.warc.gz\"}"}