Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2条形图:订单填写为输入数据_R_Ggplot2 - Fatal编程技术网

R ggplot2条形图:订单填写为输入数据

R ggplot2条形图:订单填写为输入数据,r,ggplot2,R,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","Muf

给出如下数据帧:

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

标签是根据需要订购的,但填充有一个大问题。 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")

在这种情况下,钢筋按错误的填充物分组。例如,对于“Cat”,图形应绘制1个蓝色大小的10条,然后绘制1个绿色、1个红色、2个绿色、2个红色、1个绿色(如labeltext的类别)。 我希望该图被剥离为原始数据。就像盖伊第一次那样
stat=“identity”
无法按预期工作


我尝试了许多方法重新排序,但都没有多大帮助。我已经搜索了ggplot2标签中的SO条形图填充问题(超过700篇文章),并非常仔细地阅读了文档。尽管如此,到目前为止还没有找到答案。

我发现一个黑客使用了
geom_tile

sam%>%
  group_by(title)%>%
  # set the order you want within each group
  arrange(title, desc(fnum))%>%
  # calculate the (vertical) center of each tile
  mutate(cum_size = cumsum(size) - size / 2)%>%
  ggplot(aes(x=title, y=cum_size, fill = fcat))+
    geom_tile(color="white", size = 1, aes(height = size))+
    geom_text(aes(label=paste(labeltext,size,fnum,fcat,sep=" ")),
              size = 3)
这是另一种方式

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")
结果:

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