ggplot2: adding labels to geom_bar() for a count [duplicate] - r

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)

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)

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

How can I reorder each grouped bar in R using ggplot? [duplicate]

This question already has answers here:
Order discrete x scale by frequency/value
(7 answers)
Closed 4 years ago.
I am trying to reorder the bars (which each represent a categorical variable) in my R ggplot2 Note: I am not trying to reorder the percentages of variables represented within the bars.
This is what my bar chart looks like:
Most importantly, I want Enrollment on the top. Ideally, I would like this in reverse order (then Expulsion, then ISS, then OSS >1 day, then OSS 1 day)
Here is my code:
ggplot(discbyracelong,
aes(x=discipline,
y=pct, fill=Race))+
geom_bar(stat="identity")+
geom_text(aes(label=paste(round(pct,digits= 1),sep="")),
position=position_stack(vjust=0.5),
size= 3)+
coord_flip()+
guides(fill = guide_legend(reverse=TRUE))
Here is a link to my data: https://drive.google.com/file/d/14AwBBZfevTeZyrgwMpz7XwTj8J8pCUgO/view?usp=sharing
Thank you for any help!
Just turn discipline into a factor. Instead of using unique, you could type out the values in a different order as well.
discbyracelong$discipline <- factor(discbyracelong$discipline, levels = (unique(discbyracelong$discipline)))
ggplot(discbyracelong,
aes(x=discipline,
y=pct, fill=Race))+
geom_bar(stat="identity")+
geom_text(aes(label=paste(round(pct,digits= 1),sep="")),
position=position_stack(vjust=0.5),
size= 3)+
coord_flip()+
guides(fill = guide_legend(reverse=TRUE))
scale_x_discrete(limits=c("Enrollment","Expulsion","In-School-Suspension", etc...))
see this link:http://www.sthda.com/english/wiki/ggplot2-axis-ticks-a-guide-to-customize-tick-marks-and-labels#change-the-order-of-items

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