How do I rotate a plot clockwise with ggplot? [duplicate] - r

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

Related

Wierd looking grouped barplots [duplicate]

This question already has an answer here:
Limit ggplot2 axes without removing data (outside limits): zoom
(1 answer)
Closed 1 year ago.
I have an issue with my grouped barplots, I want to visualize gene expression, and since some genes had very high expression I decided to se the ylim to different values to better visualize the lower values. However the plots come out looking like this, where the bar marked with 'y' is cut off misleading in the second picture where the limit is set to 5000. Any ideas to why this is and potenitally how to fix it? Ive used this code
grouped_bars <-ggplot(data, aes(x=gene_name, y=Result, fill=Day))+geom_bar(stat="identity",colour="black", position="dodge")+ scale_fill_manual(values =c("blue","red")) + ylim(0, 5000)
Use coord_cartesian() instead of ylim() to avoid your bars from getting lost.
grouped_bars <- ggplot(data, aes(x=gene_name, y=Result, fill=Day)) +
geom_bar(stat="identity",colour="black", position="dodge") +
scale_fill_manual(values =c("blue","red")) +
coord_cartesian(ylim=c(0,5000))
grouped_bars

Is there an R function to make a plot with not equally spaced axis? [duplicate]

This question already has answers here:
Using ggplot2, can I insert a break in the axis?
(10 answers)
Closed 2 years ago.
I want to make a plot with not equally spaced axis. Here is my data.
group <- c("group1","group2","group3","group4","group5","group6")
value <- c(520,550,13,15,30,20)
df <- cbind.data.frame(group,value)
I want to make a plot like this.
idealplot
I tried to make it using ggplot, like this.
ggplot(df,aes(x=factor(group),y=value))+geom_bar(stat="identity")
This code makes a plot with equally spaced axis which I do not want. This plot is unable to compare group 3,4,5 and 6. If you know how to make the ideal plot, please let me know.
undesiredplot
I don't think I've ever seen a discontinuous axis in ggplot2.
With points instead of bars, you can fake a discontinuous axis by using facets:
ggplot(df,aes(x=factor(group),y=value)) +
facet_grid(value < 100 ~., scales='free_y') +
geom_point(stat="identity") +
theme(strip.text = element_blank())
Other alternatives to visualizing data with large differences in values is to log-transform or sqrt-transform the Y axis, which effectively stretches out small values and compresses large values.
ggplot(df,aes(x=factor(group),y=value)) +
geom_bar(stat="identity") +
scale_y_log10() +
annotation_logticks(base=10, sides='lr')

How can I sort a bar graph from highest to lowest in R? (ggplot2) [duplicate]

This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 3 years ago.
How can I sort a bar graph from highest to lowest in R? (ggplot2)
the code is this, but feel free to do a better code haha
Ps: it is a huge data
ggplot(kiva, aes(repayment_interval, loan_amount, fill = repayment_interval)) +
geom_bar(stat = "identity") +
ggtitle("Total of loan for different types of repayment intervals")
I assume you want highest to lowest y-variable, loan_amount.
Difficult to answer without example data but something like this should work, using reorder. Also geom_col = less typing.
ggplot(kiva,
aes(reorder(repayment_interval, -loan_amount), loan_amount),
fill = repayment_interval)) +
geom_col() +
ggtitle("Total of loan for different types of repayment intervals")

How to add frequency count labels to the bars in a bar graph using ggplot2? [duplicate]

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)

Order bars in ggplot [duplicate]

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.

Resources