【问题标题】:ggplot2 bar plot: order fill as in dataggplot2条形图:按数据顺序填写
【发布时间】:2017-05-16 09:38:45
【问题描述】:

像这样给出数据框:

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

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

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

标签是按需要排序的,但是填充有很大的问题。 按 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 的类别中)。 我希望情节像原始数据一样被剥离。就像this 的人一样。 stat="identity" 没有按预期工作。

我尝试了许多方法重新排序,但没有什么帮助。我已经搜索了 ggplot2 标记中的 SO barplot 填充问题(超过 700 个帖子 o_O)并非常仔细地阅读了文档。尽管如此,到目前为止还没有找到答案。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这是另一种方式

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

    结果:

    【讨论】:

      【解决方案2】:

      我发现了一个使用 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多