R geom_bar and facet_grid labels on top of bars - r

I am trying to make this graph look better, and I'm stuck with the labels (the numbers in this case). How can I make them to show on the top of their correspondent bar? Notice it is a facet_grid.
I have the following code and the output:
ggplot(articles_europaoccidental_sex_count_unique_group, aes(Country, percentage)) + geom_bar(stat = "identity", position = "dodge", aes(fill=Gender)) +
facet_grid(~Propuesta) + geom_text(aes(label = round(percentage, 2)), position = position_dodge(width = 0.9), vjust = -1)
Thanks!

You are almost there, just you need to move aes(fill=Gender) to inside ggplot
library(tidyverse)
#Reproducible data set
test_mtcars <- mtcars %>% group_by(cyl,am, gear) %>% summarise(mean = mean(mpg))
ggplot(test_mtcars, aes(as.factor(cyl), mean, fill=as.factor(am))) + geom_bar(stat = "identity", position = "dodge") +
facet_grid(~gear) + geom_text(aes(label = round(mean, 2)), position = position_dodge(width = 0.9), vjust = -1)

Related

How to avoid overlaying annotations?

I'm trying to plot some annotation in a geom_histogram() ggplot. See image below. These annotations are the count of the histogram for each bin, each group. However, I don't know how to distance the different annotations when the counts are similar. I only know to fix the annotation with vjust or hjust but I wonder if there's a relative way. I don't think an example is necessary. Probably just looking at my code will be easy for someone more experienced.
This is the code I have used:
bind_rows(
RN_df %>% mutate(type='RN'),
RVNM_df %>% mutate(type='RVM')
) %>% group_by(hash) %>%
summarise(n_eps = n(), genre, type) %>%
ggplot(aes(x = n_eps, fill = genre)) +
geom_histogram(binwidth = 1) +
stat_count(aes(y=..count..,label=..count.., colour = genre),geom="text",vjust= -1, hjust = 0.5, size = 3) +
facet_wrap(~type)
This is my output image:
You can use geom_text_repel from ggrepel:
library(ggrepel)
ggplot(df, aes(x = n_eps, fill = genre)) +
geom_histogram(binwidth = 1) +
geom_text_repel(aes(label = ..count..), stat = 'count',
position = position_stack(vjust = 0.5), direction = 'y') +
facet_wrap(~type)

r/ggplot: compute bar plot share within group

I am using ggplot2 to make a bar plot that is grouped by one variable and reported in shares.
I would like the percentages to instead be a percentage of the grouping variable rather than a percentage of the whole data set.
For example,
library(ggplot2)
library(tidyverse)
ggplot(mtcars, aes(x = as.factor(cyl),
y = (..count..) / sum(..count..),
fill = as.factor(gear))) +
geom_bar(position = position_dodge(preserve = "single")) +
geom_text(aes(label = scales::percent((..count..)/sum(..count..)),
y= ((..count..)/sum(..count..))), stat="count") +
theme(legend.position = "none")
Produces this output:
I'd like the percentages (and bar heights) to reflect the "within cyl" proportion rather than share across the entire sample. Is this possible? Would this involve a stat argument?
As an aside, if its possible to similarly position the geom_text call over the relevant bars that would be ideal. Any guidance would be appreciated.
Here is one way :
library(dplyr)
library(ggplot2)
mtcars %>%
count(cyl, gear) %>%
group_by(cyl) %>%
mutate(prop = prop.table(n) * 100) %>%
ggplot() + aes(cyl, prop, fill = factor(gear),
label = paste0(round(prop, 2), '%')) +
geom_col(position = "dodge") +
geom_text(position = position_dodge(width = 2), vjust = -0.5, hjust = 0.5)

How to show value label in stacked and grouped bar chart using ggplot

My question is about how to show data (or value) labels in a stacked and grouped bar chart using ggplot. The chart is in the form of what has been resolved here stacked bars within grouped bar chart .
The code for producing the chart can be found in the first answer of the question in the above link. An example data set is also given in the question in the link. To show the value labels, I tried to extend that code with
+ geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-0.25)
but this does not work for me. I greatly appreciate any help on this.
You need to move data and aesthetics from geom_bar() up to ggplot() so that geom_text() can use it.
ggplot(data=test, aes(y = value, x = cat, fill = cond)) +
geom_bar(stat = "identity", position = "stack") +
theme_bw() +
facet_grid( ~ year) +
geom_text(aes(label = value), position = "stack")
Then you can play around with labels, e.g. omitting the zeros:
ggplot(data=test, aes(y = value, x = cat, fill = cond)) +
geom_bar(stat = "identity", position = "stack") +
theme_bw() +
facet_grid( ~ year) +
geom_text(aes(label = ifelse(value != 0, value, "")), position = "stack")
... and adjusting the position by vjust:
ggplot(data=test, aes(y = value, x = cat, fill = cond)) +
geom_bar(stat = "identity", position = "stack") +
theme_bw() +
facet_grid( ~ year) +
geom_text(aes(label = ifelse(value != 0, value, "")), position = "stack", vjust = -0.3)
Try this. Probably the trick is to use position_stack in geom_text.
library(tidyverse)
test <- expand.grid('cat' = LETTERS[1:5], 'cond'= c(F,T), 'year' = 2001:2005)
test$value <- floor((rnorm(nrow(test)))*100)
test$value[test$value < 0] <- 0
ggplot(test, aes(y = value, x = cat, fill = cond)) +
geom_bar(stat="identity", position='stack') +
geom_text(aes(label = ifelse(value > 0, value, "")), position = position_stack(), vjust=-0.25) +
theme_bw() +
facet_grid( ~ year)
Created on 2020-06-05 by the reprex package (v0.3.0)

How to create a distance between the geom text and the bar

I have a following problem. I want increase a padding bettween the text and the bar. But at same time, the value of text must be in the box of ggplot2 device.
Reproducible examples:
diamonds %>%
group_by(color) %>%
count() %>%
ggplot(aes(color, y = n)) +
geom_bar(stat = "identity") +
geom_text(
aes(label = n),
vjust = 0.5,
hjust = "inward") +
coord_flip()
Because you flipped coordinates it looks like your hjust and vjust calls were applied incorrectly. With this in mind, I only adjusted the text horizontally and I expanded the limits to fit the label for G, which would otherwise be cut off by the limits of the graph.
diamonds %>%
group_by(color) %>%
count() %>%
ggplot(aes(color, y = n)) +
geom_bar(stat = "identity") +
geom_text(aes(label = n),
hjust = -.5) +
coord_flip() +
expand_limits(y = 12000)
Or, if you want the text labels to be within the bars you can use the following.
diamonds %>%
group_by(color) %>%
count() %>%
ggplot(aes(color, y = n)) +
geom_bar(stat = "identity") +
geom_text(aes(label = n),
hjust = 1.5) +
coord_flip()

r ggplot2 facet_grid how to add space between the top of the chart and the border

Is there a way to add space between the labels on the top of the chart and the margin of a plot using ggplot's facet_grid. Below is a reproducible example.
library(dplyr)
library(ggplot2)
Titanic %>% as.data.frame() %>%
filter(Survived == "Yes") %>%
mutate(FreqSurvived = ifelse(Freq > 100, Freq*1e+04,Freq)) %>%
ggplot( aes(x = Age, y = FreqSurvived, fill = Sex)) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(Class ~ ., scales = "free") +
theme_bw() +
geom_text(aes(label = prettyNum(FreqSurvived,big.mark = ",")), vjust = 0, position = position_dodge(0.9), size = 2)
The resulting chart has the label of numbers right next to the border of the plot.
I wanted to add to #dww 's answer, but don't have enough reputation.
The expand option actually will allow you to add space only to the top of your graph. From the ?expand_scale help file:
# No space below the bars but 10% above them
ggplot(mtcars) +
geom_bar(aes(x = factor(cyl))) +
scale_y_continuous(expand = expand_scale(mult = c(0, .1)))
One simple way is to use the expand argument of scale_y_continuous:
dt = Titanic %>% as.data.frame() %>%
filter(Survived == "Yes") %>%
mutate(FreqSurvived = ifelse(Freq > 100, Freq*1e+04,Freq))
ggplot(dt, aes(x = Age, y = FreqSurvived, fill = Sex)) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(Class ~ ., scales = "free") +
theme_bw() +
geom_text(aes(label = prettyNum(FreqSurvived,big.mark = ",")),
vjust = 0, position = position_dodge(0.9), size = 2) +
scale_y_continuous(expand = c(0.1,0))
The downside of using expand is that it will add space both above and below the bars. An alternative is to plot some invisible data on the graph at a height above the bars, which will force ggplt to expand the axis ranges to accomodate this dummy data. Here I add some invisible bars whose height is 1.2* the actual bars:
Titanic %>% as.data.frame() %>%
filter(Survived == "Yes") %>%
mutate(FreqSurvived = ifelse(Freq > 100, Freq*1e+04,Freq)) %>%
ggplot( aes(x = Age, y = FreqSurvived, fill = Sex)) +
geom_bar(aes(y = FreqSurvived*1.2), stat = "identity",
position = "dodge", fill=NA) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(Class ~ ., scales = "free") +
theme_bw() +
geom_text(aes(label = prettyNum(FreqSurvived,big.mark = ",")),
vjust = 0,
position = position_dodge(0.9), size = 2)

Resources