题
我有一个数据帧df.all
和我使用下面的代码绘制它与GGPLOT2柱状图。我想使它这样的回避酒吧顺序翻转。即,使得棒标记为“奇异”来棒标记为“复数”之前。
ggplot(df.all, aes(gram, V1, fill=number)) +
geom_bar(stat="identity", position="dodge") +
scale_x_discrete(labels=c("Grammatical","Ungrammatical")) +
scale_y_continuous(formatter="percent", limits=c(0,1)) +
facet_grid(. ~ experiment) +
scale_fill_hue("Attractor", breaks=c("S","P"), labels=c("Singular","Plural"))
我试着做levels(df.all$number) = c("S", "P")
想,也许ggplot使用级别的顺序来决定策划秩序,但没有奏效。我不知道什么尝试。任何想法?
df.all
的内容,如果它是有用的:
> df.all
number gram experiment V1
1 S G BERIMBAU_AGR_A 0.8133333
2 S G BERIMBAU_AGR_B 0.8658537
3 S U BERIMBAU_AGR_A 0.5436242
4 S U BERIMBAU_AGR_B 0.4597701
5 P G BERIMBAU_AGR_A 0.8580645
6 P G BERIMBAU_AGR_B 0.8536585
7 P U BERIMBAU_AGR_A 0.3087248
8 P U BERIMBAU_AGR_B 0.3975904
> str(df.all)
'data.frame': 8 obs. of 4 variables:
$ number : Factor w/ 2 levels "S","P": 2 2 2 2 1 1 1 1
..- attr(*, "scores")= num [1:2(1d)] 0 -1
.. ..- attr(*, "dimnames")=List of 1
.. .. ..$ : chr "P" "S"
$ gram : Factor w/ 2 levels "G","U": 1 1 2 2 1 1 2 2
$ experiment: Factor w/ 4 levels "BERIMBAU_AGR_A",..: 1 4 1 4 1 4 1 4
$ V1 : num 0.813 0.866 0.544 0.46 0.858 ...
解决方案 3
哈德利提供了一个解决方案。这里的问题和解决方案的一个复制。
我们的目标是让酒吧标有“S”来酒吧标有“P”之前。此默认不发生,因为r命令水平按字母顺序。
df <- read.csv("http://pealco.net/code/ggplot_dodge/df.txt")
ggplot(df, aes(gram, V1, fill=number))
+ geom_bar(stat="identity", position="dodge")
随着哈德利的另一种回答说:“你需要基于X变量,而不是变量y重新排序”。虽然我不知道为什么这个工程。
要翻转在本实施例的要素的顺序,可以将因子转换为数值,并乘以-1。
df <- with(df, df[order(gram, -as.numeric(number)), ])
我还是想为什么df <- with(df, df[order(gram, -as.numeric(number)), ])
作品一些更多的解释。
其他提示
我认为df.all$number
需要是有序的因素。尝试df.all$number <- ordered(df.all$number)
在某些情况下,我不认为这是可能的:
layerCake<-data.frame(group=c(rep("normal",4),rep("tumor",4)),
class=factor(rep(c("exon","intron","intergenic","unmapped"),2),levels=rev(c("exon","intron","intergenic","unmapped")),ordered=TRUE),
fraction=c(.02,.25,.50,.23,.015,.20,.555,.23)
)
layerCake[layerCake$group=='normal',"reads"]<-130948403*layerCake[layerCake$group=='normal',"fraction"]
layerCake[layerCake$group=='tumor',"reads"]<-200948403*layerCake[layerCake$group=='tumor',"fraction"]
g<-ggplot(layerCake, aes(x=factor(group),y=reads, fill=factor(class),order = as.numeric(class)))+xlab("Group")+scale_fill_discrete(name="Anno Class",breaks=c("exon","intron","intergenic","unmapped"))
正确的顺序堆叠:结果 克+ geom_bar(STAT = “同一性”,位置= “堆叠”)
不正确的顺序在躲闪:
g+geom_bar(stat="identity",position="dodge")
让我们尝试颠倒顺序在ggplot:
g<-ggplot(lc, aes(x=factor(group),y=reads, fill=factor(class),order = -as.numeric(class)))+xlab("Group")+scale_fill_discrete(name="Anno Class",breaks=c("exon","intron","intergenic","unmapped"))
g+geom_bar(stat="identity",position="dodge")
没有骰子
让我们尝试以重新排序所述数据帧
lc <- with(lc, lc[order(-as.numeric(class)), ])
g<-ggplot(lc, aes(x=factor(group),y=reads, fill=factor(class),order = -as.numeric(class)))+xlab("Group")+scale_fill_discrete(name="Anno Class",breaks=c("exon","intron","intergenic","unmapped"))
g+geom_bar(stat="identity",position="dodge")
没了
更改因子水平确实改变躲过条的顺序!常见的错误:颜色仍停留在某个位置,所以采取了快速浏览使它看起来像的顺序没有改变。但是,如果你看一下值,你会看到,要真正改变了。
编辑:我以前回答以下仅改变给酒吧的配色方案的顺序。这仍然是有用的,因为我们可能经常要扭转同时颜色方案作为改变杆的顺序:
我是用scale_fill_manual因为我想手工填写我的酒吧的颜色。
ggplot(data, aes_string(x = "countries", y = "population", fill = "agegroups")) +
scale_fill_manual(values = CustomColorFunction(), limits = (levels(data$agegroups)))
用了5个小时不断变化的因子水平和排列数据帧希望这可以帮助别人!修修补补