如果您只想颜色顺序匹配,则可以反转图例:颜色顺序将匹配,但图例将按相反的字母顺序排列:
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))
要恢复字母顺序,请在上述代码之前对 Group 因子进行重新排序:
# 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))
为了获得奖励(通过减少总计数对条形进行排序),请重新排序 Site 变量的因子顺序:
# 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))