This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 9 years ago.
I am plotting a barplot in ggplot:
ggplot(fastqc.dat,aes(y=fastqc.dat$ReadCount,x=fastqc.dat$Sample)) + geom_bar(stat="identity",position="identity",fill="darkblue") + xlab("Samples") + ylab("Read Counts") + opts(axis.text.x=theme_text(angle=-90))
My file 'fastqc.dat' looks like this:
Sample ReadCount
201304950-01_ATTCAGAA_R1 27584682
201304951-01_GAATTCGT_R1 25792086
201304952-01_CTGAAGCT_R1 36000000
201304953-01_GAGATTCC_R1 35634177
201304954-01_ATTACTCG_R1 88906701
It produces the following plot:
But I want to reorder the bars based on the read counts i.e. the Y axis. I tried a lot of things but it just won't happen. I even tried sorting fastqc.dat based on ReadCount column. Any suggestions?
... so bringing the helpful suggestions together, one solution would be:
fastqc.dat$Sample <- factor(fastqc.dat$Sample,
levels=fastqc.dat$Sample[order(fastqc.dat$ReadCount)])
and than use your code...
HTH
I got it to work. I had to add aes(x=fastqc.dat$Sample) to geom_bar() as below:
fastqc.dat$Sample <-factor(fastqc.dat$Sample, levels=fastqc.dat[order(fastqc.dat$ReadCount), "Sample"])
ggplot(fastqc.dat,aes(x=fastqc.dat$Sample,y=fastqc.dat$ReadCount)) + geom_bar(aes(x=fastqc.dat$Sample),stat="identity",position="identity",fill="darkblue") + xlab("Samples") + ylab("Read Counts") + opts(axis.text.x=theme_text(angle=-90))
This arranges the bars on X axis.
Related
This question already has answers here:
Show frequencies along with barplot in ggplot2
(5 answers)
Closed 8 years ago.
I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that.
What I can't figure out is how to add frequency count labels to the bars in the bargraph. The syntax that I've tried is as follows:
ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))
I think I thoroughly searched in stackoverflow and "R Graphics Cookbook" by W.Chang but I couldn't find any specific answer to what parameter should I match to "y" in the aesthetics of geom_text() above. I tried some variants like: (y = ..count..) but it didn't work.
I would appreciate any help. Thanks...
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)
This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 3 years ago.
I'm trying to visualize the different backgrunds of our students, the bars represent different high school programs. I write:
ggplot(test, aes(x=fct_infreq(gymnasiegrov))) + geom_bar()
And I get:
As you can see the names get very cluttered, so I write:
ggplot(test, aes(x=fct_infreq(gymnasiegrov))) + geom_bar()+coord_flip()
And get:
This "does" look much better, but for optimal effect I would like to show the frequencies in descending order from the top, like my first plot but rotated clockwise.
Is there any way I can make this happen?
I think you can use scale_x_discrete(limits = rev(levels(test$fct_infreq(gymnasiegrov)))).
Where test is your data frame and fct_infreq(gymnasiegrov) is your discrete variable.
Please let me know if it worked :)
Edit: To be more clear add it to ggplot(test, aes(x=fct_infreq(gymnasiegrov))) + geom_bar()+coord_flip() so that you get ggplot(test, aes(x=fct_infreq(gymnasiegrov))) + geom_bar()+coord_flip() + scale_x_discrete(limits = rev(levels(test$fct_infreq(gymnasiegrov))))
Use ggplot(test, aes(x=reoder(fct_infreq(gymnasiegrov)),Count)) + geom_bar()+coord_flip()
This question already has answers here:
Show frequencies along with barplot in ggplot2
(5 answers)
Closed 8 years ago.
I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that.
What I can't figure out is how to add frequency count labels to the bars in the bargraph. The syntax that I've tried is as follows:
ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))
I think I thoroughly searched in stackoverflow and "R Graphics Cookbook" by W.Chang but I couldn't find any specific answer to what parameter should I match to "y" in the aesthetics of geom_text() above. I tried some variants like: (y = ..count..) but it didn't work.
I would appreciate any help. Thanks...
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)
This question already has answers here:
Show frequencies along with barplot in ggplot2
(5 answers)
Closed 8 years ago.
I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that.
What I can't figure out is how to add frequency count labels to the bars in the bargraph. The syntax that I've tried is as follows:
ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))
I think I thoroughly searched in stackoverflow and "R Graphics Cookbook" by W.Chang but I couldn't find any specific answer to what parameter should I match to "y" in the aesthetics of geom_text() above. I tried some variants like: (y = ..count..) but it didn't work.
I would appreciate any help. Thanks...
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)
This question already has answers here:
Show frequencies along with barplot in ggplot2
(5 answers)
Closed 8 years ago.
I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that.
What I can't figure out is how to add frequency count labels to the bars in the bargraph. The syntax that I've tried is as follows:
ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))
I think I thoroughly searched in stackoverflow and "R Graphics Cookbook" by W.Chang but I couldn't find any specific answer to what parameter should I match to "y" in the aesthetics of geom_text() above. I tried some variants like: (y = ..count..) but it didn't work.
I would appreciate any help. Thanks...
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)