I have a dataframe df.all
and I'm plotting it in a bar plot with ggplot2 using the code below. I'd like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "Singular" come before the bars labeled "Plural".
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"))
I've tried doing levels(df.all$number) = c("S", "P")
thinking that maybe ggplot uses the order of the levels to decide plotting order, but that didn't work. I'm not sure what else to try. Any ideas?
The contents of df.all
, in case it's useful:
> 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 ...
Reordering in ggplot is done using theme() function. Within this, we use axis. text. x with the appropriate value to re-order accordingly.
Another way to re-order barplots is to use the base R function reorder(). It kind of works the same way and we reorder the factors based on population size. Reordering bars in barplot using base R function reorder() also results in the same barplot as we ordered by fct_reorder().
Under Chart Tools, on the Design tab, in the Data group, click Select Data. In the Select Data Source dialog box, in the Legend Entries (Series) box, click the data series that you want to change the order of. Click the Move Up or Move Down arrows to move the data series to the position that you want.
Rearranging Results in Basic R If you want the bar graph to go in descending order, put a negative sign on the target vector and rename the object.
In some cases I don't think this is possible:
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"))
correct order in stacked:
g+geom_bar(stat="identity",position="stack")
incorrect order in dodge:
g+geom_bar(stat="identity",position="dodge")
let's try to reverse the order in 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")
no dice
let's try to reorder the data frame
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")
nope
I think df.all$number
needs to be an ordered factor. Try df.all$number <- ordered(df.all$number)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With