反向填充为了在GGPLOT2柱状图(Reverse fill order for histogram

2019-09-20 01:30发布

我已经注意到了,在使用情节生成了柱状图填充棒默认为反向字母,而在按字母顺序排列的传奇。 我有什么办法让既字母顺序排序? 问题是在以下示例性绘图显而易见。 奖金的问题:我如何从左到右的顺序吧,从改变到字母降低总计数? 谢谢

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

我试图从高计数责令低,填充颜色,使他们的传奇字母排序相匹配。 我一直在试图调整其与从最初的职位,但没有sucess提示了几个小时。 任何帮助将是非常感激!

Answer 1:

如果你只是想颜色顺序比赛,你可以扭转传说:色订货会匹配,但传说将在相反的字母顺序:

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

为了让字母顺序背,集团因子的重新排序先于上面的代码:

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

对于奖金(订货通过减少总数的酒吧),重新排序站点变量的因素顺序:

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



文章来源: Reverse fill order for histogram bars in ggplot2
标签: r ggplot2