Wierd looking grouped barplots [duplicate] - r

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

Related

How do I add values to my bargraph in Rstudio [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)

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

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

ggplot2: adding labels to geom_bar() for a count [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)

How to add data labels [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)

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)

Resources