ggplot2中直方图条的反向填充顺序 [英] Reverse fill order for histogram bars in ggplot2

查看:258
本文介绍了ggplot2中直方图条的反向填充顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,在使用绘图创建的直方图中填充条形的默认设置是按字母顺序颠倒的,而图例按字母顺序排列。我有没有办法让两个按字母顺序?在下面的示例图中,问题很明显。奖金问题:我如何将从左至右的字母顺序从字母顺序改为递减总计?谢谢

  df < -  data.frame(
Site = c(A05,R17, R01,A05,R17,R01),
Group = c(Fungia,Fungia,Acro,Acro,Porites,Porites), b $ b Count = c(6,8,6,7,2,9),
Total = c(13,10,15,13,​​10,15)


网站组别数量总计
1 A05 Fungia 6 13
2 R17 Fungia 8 10
3 R01 Acro 6 15
4 A05 Acro 7 13
5 R17 Porites 2 10
6 R01 Porites 9 15

qplot(df $ Site,data = df,weight = df $ Count,geom =直方图,fill = df $ Group,ylim = c (0,16))+
xlab(Sites)+
ylab(Counts)+
scale_fill_hue(h = c(0,360),l = 70,c = 70, name =Emergent Groups)


我正在尝试将计数从高到低排列,并使填充颜色匹配图例的字母顺序。我一直试图用最初的帖子提示来调整它几个小时,但没有成功。任何对此的帮助将非常感谢!

如果您只想要颜色顺序匹配,则可以颠倒图例:颜色顺序将匹配,但图例将按字母顺序颠倒:

  qplot(df $ Site,data = df,weight = df $ Count,geom = (Counts)+ 
scale_fill_hue(h = c()),fill = df (0,360),l = 70,c = 70,name =Emergent Groups)+
guides(fill = guide_legend(reverse = TRUE))



为了回到字母顺序,在上面的代码前面加上组因子重新排序:

 #重新排列组
df $组< - 因子(df $ Group,
levels = levels(df $ Group)[order(levels(df $ Group),decrease = TRUE)])

qplot(df $ Site,data = df,weight = df $ Count,geom =直方图,fill = df $ Group,ylim = c (0,16))+
xlab(Sites)+
ylab(Counts)+
scale_fill_hue(h = c(0,360),l = 70,c = 70, (填充= guide_legend(reverse = TRUE))



(通过减少总数来排序),重新排列Site变量的因子顺序:

 #重新排序网站
df $ Site < - factor(df $ Site,
levels = levels(df $ Site)[order(aggregate(Count〜Site,data = df,sum)$ Count,
decrease = TRUE )])
#重新排序组
df $组< - 因子(df $ Group,
levels = levels(df $ Group)[order(levels(df $ Group)), = TRUE)])

qplot(df $ Site,data = df,weight = df $ Count,geom =直方图,fill = df $ Group,ylim = c(0,16)) +
xlab(网站)+
ylab(计数)+
scale_fill_hue(h = c(0,360),l = 70,c = 70,name =Emergent Groups)+
guides(fill = guide_legend(reverse = TRUE))


I have noticed that the default for filling the bars in a histogram created using plot is the reverse alphabetical, while the legend in ordered alphabetically. I there any way to get both to order alphabetically? Problem is apparent in example plot below. Bonus question: how I change the left to right bar order from alphabetical to decreasing count total? Thanks

df <- data.frame(
  Site=c("A05","R17","R01","A05","R17","R01"),
  Group=c("Fungia","Fungia","Acro","Acro","Porites","Porites"),
  Count=c(6,8,6,7,2,9),
  Total=c(13,10,15,13,10,15)
)

  Site   Group Count Total
1  A05  Fungia     6    13
2  R17  Fungia     8    10
3  R01    Acro     6    15
4  A05    Acro     7    13
5  R17 Porites     2    10
6  R01 Porites     9    15

qplot(df$Site,data=df,weight=df$Count,geom="histogram", fill=df$Group, ylim = c(0,16)) + 
  xlab("Sites") + 
  ylab("Counts") + 
  scale_fill_hue(h=c(0,360), l=70, c=70,name = "Emergent Groups")

I am trying to order the counts from high to low and the fill colours so that they match the alphabetic ordering of the legend. I have been trying to adjust it for a few hours with the tips from the initial posts but without sucess. Any help on this would be most appreciated!!!

解决方案

If you just want the color order match, you can just reverse the legend: the color orders will match, but the legend will be in reverse alphabetical order:

qplot(df$Site,data=df,weight=df$Count,geom="histogram", fill=df$Group, ylim = c(0,16)) + 
  xlab("Sites") + 
  ylab("Counts") + 
  scale_fill_hue(h=c(0,360), l=70, c=70,name = "Emergent Groups") +
  guides(fill = guide_legend(reverse = TRUE))

To get the alphabetical order back, precede the above code by a reordering of the Group factor:

# reorder the groups
df$Group <- factor(df$Group , 
                   levels=levels(df$Group)[order(levels(df$Group), decreasing = TRUE)])

qplot(df$Site,data=df,weight=df$Count,geom="histogram", fill=df$Group, ylim = c(0,16)) + 
  xlab("Sites") + 
  ylab("Counts") + 
  scale_fill_hue(h=c(0,360), l=70, c=70,name = "Emergent Groups") +
  guides(fill = guide_legend(reverse = TRUE))

For the bonus (ordering the bars by decreasing total count), reorder the factor order of the Site variable:

# reorder the sites
df$Site <- factor(df$Site, 
                  levels = levels(df$Site)[order(aggregate(Count ~ Site, data = df, sum)$Count, 
                                                 decreasing = TRUE)])
# reorder the groups
df$Group <- factor(df$Group , 
                   levels=levels(df$Group)[order(levels(df$Group), decreasing = TRUE)])

qplot(df$Site,data=df,weight=df$Count,geom="histogram", fill=df$Group, ylim = c(0,16)) + 
  xlab("Sites") + 
  ylab("Counts") + 
  scale_fill_hue(h=c(0,360), l=70, c=70,name = "Emergent Groups") +
  guides(fill = guide_legend(reverse = TRUE))

这篇关于ggplot2中直方图条的反向填充顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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