ggplot2条形图:按数据填写订单 [英] ggplot2 bar plot: order fill as in data

查看:53
本文介绍了ggplot2条形图:按数据填写订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这样提供数据框:

  sam< -data.table(title = c(rep("Cat",8),rep("Dog",4)),fcat = c("A","B","C","B","B","C","C","B","C","B","B","C),fnum = c(seq(8,1),seq(4,1)),labeltext = c("Pancakes","Muffins","Baursaq","Muffins","Muffins",rep("Baursaq",3),"Muffins","Baursaq","Baursaq","Muffins"),大小= c(10,rep(1,11)))山姆标题fcat fnum标签文本大小1:猫A 8煎饼102:Cat B 7松饼13:Cat C 6 Baursaq 14:B类猫5个松饼15:B类猫4个松饼16:C类猫3 Baursaq 17:C类猫2 Baursaq 18:B类猫1 Baursaq 19:狗C 4松饼110:狗B 3 Baursaq 111:狗B 2 Baursaq 112:狗C 1松饼1 

我想创建一个带有x = title,y = size,由fcat填充,由labeltext标记并由fnum降序排列的barplot.第一种方法,无需重新排序原始数据.在这里,我将标签的文本串联起来,以检查顺序,大小和类别是否正确使用.

  ggplot(sam)+geom_bar(aes(x = factor(title),y = size,fill = fcat),stat ="identity",color ="white",size = 1)+geom_text(aes(x = title,y = size,label =(paste(labeltext,size,fnum,fcat,sep ="))),大小= 3,位置= position_stack(vjust = 0.5))+ggtitle(不订购") 

标签是根据需要排序的,但是填充存在很大的问题.通过fnum重新排序填充:

  ggplot(sam)+geom_bar(aes(x = factor(title),y = size,fill = reorder(fcat,fnum)),stat ="identity",color ="white",size = 1)+geom_text(aes(x = title,y = size,label = reorder(paste(labeltext,size,fnum,fcat,sep ="),fnum)),大小= 3,位置= position_stack(vjust = 0.5))+ggtitle(由fnum排序") 

在这种情况下,条被错误的填充分组.例如,对于猫",图形应绘制1个蓝色大小10 bar的图形,然后绘制1个绿色,1个红色,2个绿色,2个红色,1个绿色(与labeltext的类别相同).我希望像原始数据一样删除该图.像

Giving data frame like this:

sam<-data.table(title=c(rep("Cat",8),rep("Dog",4)),
               fcat=c("A","B","C","B","B","C","C","B","C","B","B","C"),
               fnum=c(seq(8,1),seq(4,1)),
               labeltext=c("Pancakes","Muffins","Baursaq","Muffins","Muffins",
                 rep("Baursaq",3),"Muffins","Baursaq","Baursaq","Muffins"), 
               size=c(10,rep(1,11)))

sam
    title fcat fnum labeltext size
 1:   Cat    A    8  Pancakes   10
 2:   Cat    B    7   Muffins    1
 3:   Cat    C    6   Baursaq    1
 4:   Cat    B    5   Muffins    1
 5:   Cat    B    4   Muffins    1
 6:   Cat    C    3   Baursaq    1
 7:   Cat    C    2   Baursaq    1
 8:   Cat    B    1   Baursaq    1
 9:   Dog    C    4   Muffins    1
10:   Dog    B    3   Baursaq    1
11:   Dog    B    2   Baursaq    1
12:   Dog    C    1   Muffins    1

I want to create a barplot with x=title, y=size, filled by fcat, labelled by labeltext and ordered in descending order by fnum. First approach without reordering the original data. Here I concatenated text of labels in order to check if the order, size and category is used correctly.

ggplot(sam)+
    geom_bar(aes(x=factor(title), y=size,fill=fcat),
             stat="identity", color="white", size=1)+
    geom_text(aes(x=title, y=size,
             label=(paste(labeltext,size,fnum,fcat, sep=" "))),
             size = 3, position = position_stack(vjust = 0.5))+
    ggtitle("Without ordering")

Labels are ordered as needed, but there's a big problem with filling. Reorder filling by fnum:

ggplot(sam)+
    geom_bar(aes(x=factor(title), 
              y=size,fill=reorder(fcat,fnum)),
              stat="identity", color="white", size=1)+
    geom_text(aes(x=title,y=size,
             label=reorder(paste(labeltext,size,fnum,fcat, 
                       sep=" "),fnum)),
             size = 3, position = position_stack(vjust = 0.5))+
    ggtitle("Ordered by fnum")

In this case bars are grouped by wrong fillings. For instance, for "Cat" the graph should plot 1 blue sized 10 bar, then 1 green, 1 red, 2 greens, 2 red, 1 green (as in labeltext's categories). I want the plot to be stripped as in the original data. Like this guy had in first place. stat="identity" doesn't work as expected.

I tried numerous approach reordering, but nothing helps much. I've searched through the SO barplot fill questions in ggplot2 tag (more than 700 posts o_O) and read the documentation very carefully. Nonetheless, no answer was found so far.

解决方案

this is another way

ggplot(sam)+
  geom_bar(aes(x=title, y=size,group=fnum,fill=fcat), #group by fnum and fill with fcat
           stat="identity", color="white", size=1)+
  geom_text(aes(x=title, y=size,
                label=(paste(labeltext,size,fnum,fcat, sep=" "))),
            size = 3, position = position_stack(vjust = 0.5))+
  ggtitle("group by fnum and fill by fcat")

Result:

这篇关于ggplot2条形图:按数据填写订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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