使用ggplot2根据填充值重新排序堆叠的barplot x [英] Reorder stacked barplot x based on fill values with ggplot2

查看:1667
本文介绍了使用ggplot2根据填充值重新排序堆叠的barplot x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一项包含5个问题的调查被执行。这些问题共享相同的一组可能的答案。这里是用ggplot2进行绘图的数据。

  library(tidyr)
library(magrittr)

data < - data。 (ID = c(1:500),
q1 =因子(样本(c(1:4),500,替换= T),
labels = c(A,B (c(1:4),500,replace = T),
labels = c(A,B,C,D)),
q2 = (c(1:4),500,replace = T),
labels = c(A,B,C,D)),
q3 = (C(1:4),500,替换= T),
标签= c(A,B,C,D)),
q4 = ,C,D)),
q5 =因子(样本(c(1:4),500,替换= T),
labels = c(A, ,C,D)))%>%
gather(question,value,q1:q5)

我想根据给定响应的数量对问题进行排序。因此,而不是... ...

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



...例如,我希望沿x轴的问题顺序基于answer = D的计数。

解决方案

明白了。在下面的示例中,由响应数= A指定。

  data $ question<  -  reorder(dataf $ question,data $ value,function(x)max(table(x) [b] b 
$ b $ 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))


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.

解决方案

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)[1]))  

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

这篇关于使用ggplot2根据填充值重新排序堆叠的barplot x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆