使用melt和ggplot创建每个条带一个变量的堆叠条形图 [英] Creating Stacked Bar Chart With one Variable for each Bar, using melt, and ggplot

查看:85
本文介绍了使用melt和ggplot创建每个条带一个变量的堆叠条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我昨天发布的问题提出了不同的观点,并提供了更好的描述,因此希望您能谅解.我有以下数据:

 数据<-data.frame(LMX = c(1.92,2.33,3.52,5.34,6.07,4.23,3.45,5.64),兴旺= c(4.33,6.54,6.13,4.85,4.26,6.32,5.63、4.55),幸福感= c(1.92、2.33、3.52、2.34、4.07、3.23、3.45、4.64))行名(数据)<-1:8 

现在,我的目标是生成一个翻转的条形图,该条形图为每个变量显示一个条形,所有条形之和总计为100%,并根据值进行划分-黄色代表0到1.99之间的所有值,橙色代表从2到3.99的所有值,从4到5.99的所有值都是红色,从6到7的所有值都是绿色.更确切地说,我正在寻找这样的东西.:

现在,我尝试了以下代码:

  Data_A< -melt(cbind(Data,ind = rownames(Data)),id.vars = c('ind'))ggplot(Data_A,aes(x =变量,y =值,填充=因子(值)))+geom_bar(position ="fill",stat ="identity")+scale_y_continuous(labels = percent_format())+coord_flip() 

不幸的是,我不知道如何将我上面提到的那些类别中的值分组.而且,使用此代码,值甚至没有以正确的顺序从低到高排列.

能否请您给我一些建议,以获取如上所示的图片?

另外,还有一个问题:这8个人中的每一个都属于两个组之一,我想根据这两个组来区分值.但是,在我的代码中包含此附加变量只会使其与其他变量融为一体.因此,我也看不到任何方法来说明这些组,例如使用 facet_grid()添加组标识符.您在这里也有建议吗?我应该使用完全不同的方法/代码吗?

解决方案

关于第一部分,这是您要查找的内容吗?(我建议您更改颜色以防止癫痫病发作.)

 数据%>%mutate_all(cut,c(0,2,4,6,7),right = F,)%>%收集(键=变量",值=值")%&%;%ggplot(aes(x =变量,fill =值))+geom_bar(位置= position_fill(反向= TRUE))+coord_flip()+scale_fill_manual(values = c(黄色",橙色",红色",绿色")) 

对于第二部分,一个可重现的示例将很有用,但是您可以添加一个组"变量(在 gather ggplot 之间)并使用 facet_grid facet_wrap .

---在有关组的信息之后进行了以下编辑---

DataG [Data_IlA $ G1_ID == 2] 中缺少列选择,并且变量名称与 DataG 中的变量名称不同,因此无法创建DataG_1./p>

以下建议之一是否使您想要的图形?

  DataG%>%collect(key ="variable",value ="value",-Group_ID)%&%mutate(value = cut(value,c(0,1.99,3.99,5.99,7)))%>%ggplot(aes(x =变量,fill =值))+geom_bar(位置= position_fill(反向= TRUE))+scale_y_continuous(labels = scales :: percent)+coord_flip()+scale_fill_manual(values = c(#19557E",#6E3B60",#EA916A",#EFC76C"))+主题(panel.background = element_blank())+xlab(")+ ylab(")+facet_grid(Group_ID〜.) 

  DataG%>%collect(key ="variable",value ="value",-Group_ID)%&%mutate(value = cut(value,c(0,1.99,3.99,5.99,7)))%>%ggplot(aes(x = Group_ID,fill = value))+geom_bar(位置= position_fill(反向= TRUE))+scale_x_discrete(limits = c("Group 1","Group 2"))+scale_y_continuous(labels = scales :: percent)+coord_flip()+scale_fill_manual(values = c(#19557E",#6E3B60",#EA916A",#EFC76C"))+主题(panel.background = element_blank())+xlab(")+ ylab(")+facet_grid(变量〜.) 

---在对组进行评论后在下面进行编辑---

如果您需要更改任何变量的类别,最简单的方法可能是在调用 ggplot 之前进行:

  DataG%>%mutate(Group_ID = case_when(Group_ID == 1〜第一组的名称",Group_ID == 2〜第二组名称"))%&%;%collect(key ="variable",value ="value",-Group_ID)%&%mutate(value = cut(value,c(0,1.99,3.99,5.99,7)))%>%ggplot(aes(x =变量,fill =值))+geom_bar(位置= position_fill(反向= TRUE))+scale_y_continuous(labels = scales :: percent)+coord_flip()+scale_fill_manual(values = c(#19557E",#6E3B60",#EA916A",#EFC76C"))+主题(panel.background = element_blank())+xlab(")+ ylab(")+facet_grid(Group_ID〜.) 

This question is raising different points as the one I posted yesterday, with a better description, so I hope for your understanding. I have the following Data:

Data <- data.frame(LMX = c(1.92, 2.33, 3.52, 5.34, 6.07, 4.23, 3.45, 5.64), Thriving = c(4.33, 6.54, 6.13, 4.85, 4.26, 6.32, 5.63, 4.55), Wellbeing = c(1.92, 2.33, 3.52, 2.34, 4.07, 3.23, 3.45, 4.64))
rownames(Data) <- 1:8

Now, my aim is to generate a flipped over bar chart that is showing one bar for each variable with all bars summing up to 100% and being divided according to the values - yellow for all values from 0 to 1.99, orange for all values from 2 to 3.99, red for all values from 4 to 5.99 and green for all values from 6 to 7. More precisely, I am looking for something like this.:

Now, I tried the following code:

Data_A <- melt(cbind(Data, ind = rownames(Data)), id.vars = c('ind'))

ggplot(Data_A, aes(x = variable, y = value, fill = factor(value))) + 
geom_bar(position = "fill", stat = "identity") + 
scale_y_continuous(labels = percent_format())  + 
coord_flip()

Unfortunately, I have no idea how to group the values in those categories I mentioned above. What is more, using this code the values are not even arranged in the right order, from low to high.

Could you please give me some recommendations how to get a picture as shown above?

Also, there is one further problem: each of those 8 individuals belongs to one of two groups and I would like to distinguish the values in the light of those two groups. However, including this additional variable to my code would just melt it together with the other variables. So I don't see any way to account for the groups here as well, using for instance facet_grid() to add the group-identifier. Do you have a suggestion here as well? Should I maybe use an entirely different approach/code?

解决方案

Is this what you're looking for regarding the first part? (I advise you change colors to prevent epileptic seizures.)

Data %>%
  mutate_all(cut, c(0, 2, 4, 6, 7), right = F, ) %>% 
  gather(key = "variable", value= "value") %>% 
  ggplot(aes(x = variable, fill = value)) + 
  geom_bar(position = position_fill(reverse = TRUE)) +
  coord_flip() +
  scale_fill_manual(values=c("yellow", "orange", "red", "green"))

For the second part, a reproducible example would be useful but you can probably add a "group" variable (between gather and ggplot) and use facet_grid or facet_wrap.

--- Edited below after information about groups ---

Column selection is missing in DataG[Data_IlA$G1_ID == 2] and variable names are not the same as the one in DataG so DataG_1 cannot be created.

Does one of the suggestions below make the figure you want?

DataG %>%
  gather(key = "variable", value = "value", -Group_ID) %>%
  mutate(value = cut(value, c(0, 1.99, 3.99, 5.99, 7))) %>%
  ggplot(aes(x = variable, fill = value)) +
  geom_bar(position = position_fill(reverse = TRUE)) +
  scale_y_continuous(labels = scales::percent) +
  coord_flip() +
  scale_fill_manual(values=c("#19557E","#6E3B60", "#EA916A", "#EFC76C")) +
  theme(panel.background = element_blank()) +
  xlab("") + ylab("") +
  facet_grid(Group_ID ~ .)

DataG %>%
  gather(key = "variable", value = "value", -Group_ID) %>%
  mutate(value = cut(value, c(0, 1.99, 3.99, 5.99, 7))) %>%
  ggplot(aes(x = Group_ID, fill = value)) +
  geom_bar(position = position_fill(reverse = TRUE)) +
  scale_x_discrete(limits = c("Group 1","Group 2")) +
  scale_y_continuous(labels = scales::percent) +
  coord_flip() +
  scale_fill_manual(values=c("#19557E","#6E3B60", "#EA916A", "#EFC76C")) +
  theme(panel.background = element_blank()) +
  xlab("") + ylab("") +
  facet_grid(variable ~ .)

--- Edited below after comment on groups ---

If you need to change categories for any variable, the easiest way may be to do so before calling ggplot:

DataG %>%
  mutate(Group_ID = case_when(
    Group_ID == 1 ~ "1st group's name",
    Group_ID == 2 ~ "2nd group's name"
  )) %>% 
  gather(key = "variable", value = "value", -Group_ID) %>%
  mutate(value = cut(value, c(0, 1.99, 3.99, 5.99, 7))) %>%
  ggplot(aes(x = variable, fill = value)) +
  geom_bar(position = position_fill(reverse = TRUE)) +
  scale_y_continuous(labels = scales::percent) +
  coord_flip() +
  scale_fill_manual(values=c("#19557E","#6E3B60", "#EA916A", "#EFC76C")) +
  theme(panel.background = element_blank()) +
  xlab("") + ylab("") +
  facet_grid(Group_ID ~ .)

这篇关于使用melt和ggplot创建每个条带一个变量的堆叠条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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