Col plot: fill 100 - value - r

I have a df, which gives the plot
library(ggplot2)
df <- data.frame(group = c("Thriller", "Horror", "Action"), number = c(60, 50, 90))
ggplot(df, aes(group, number)) +
geom_col()
But I want this
I tried tings with fill, but it does not give me the result. Does someone have any suggestions? Thanks in advance!

Are you looking for something like this?
ggplot(data.frame(group = rep(df$group, 2),
number = c(df$number, 100 - df$number),
class = rep(c("B", "A"), each = nrow(df))),
aes(group, number, fill = class)) +
geom_col() +
geom_text(aes(label = paste(number, "%")),
position = position_stack(vjust = 0.5)) +
scale_fill_manual(values = c("dodgerblue", "gray30"), guide = "none")

Related

Is there a possibility to add label "subject ID number" R-geom_histogram (ID on each bar for all subjects) of variable on x-axis and count on y-axis

I created a histogram with a number (count) of subjects on the y-axis and I'm trying to find a way to add the ID label of each ID on each bar of the histogram. I tried geom_text and geom_text_repel but I still can't get the number to be organized exactly on each bar for each subject.
For my dataset I'm reading a CSV file with 72 subjects, the columns are ID and Drugratio
ggplot code:
plot_5 <- ggplot(All_data, aes(x=as.numeric(logMRP), fill = as.factor(PATIENTID)))+geom_histogram(aes( bins = 30, label=as.factor(PATIENTID)))+ geom_text( stat='count', aes(label=ID), color="Black", size=3, check_overlap = TRUE, hjust=1, position=position_stack(vjust=0.5 ))+theme(legend.position = "none")
show(plot_5)
Any suggestions!
Thank you
Without your data or code, we can only guess, but it seems your data is something like this:
set.seed(4)
df <- data.frame(ID = factor(1:72), value = 2 - rgamma(72, 3, 2))
And your plotting code is like this:
library(ggplot2)
ggplot(df, aes(value, fill = ID)) +
geom_histogram(bins = 30) +
geom_text(stat = "count", aes(label = ID, y = ..count..),
check_overlap = TRUE) +
guides(fill = guide_none()) +
labs(x = NULL)
This looks very similar to your own plot. To fix it, let's use stat_bin with position = position_stack() for the text layer.
ggplot(df, aes(x = value, fill = ID)) +
geom_histogram(bins = 30) +
stat_bin(geom = "text", bins = 30, na.rm = TRUE,
aes(label = ifelse(after_stat(count) == 0, NA, after_stat(group)),
group = ID, y = after_stat(count)),
position = position_stack(vjust = 0.5)) +
guides(fill = guide_none()) +
labs(x = NULL)
Created on 2022-09-01 with reprex v2.0.2
Following the great suggestions I got from Allan Cameron, I was able to add the ID values for each subject similar to the graphs above
plot_5_new <- ggplot(All_data, aes(x=as.numeric(logMRP), fill = as.factor(ID)))+geom_histogram(bins=30)+ stat_bin(geom = "text", bins = 30, na.rm = TRUE, aes(label = ifelse(after_stat(count) == 0, NA, after_stat(group)), group = as.factor(ID), y = after_stat(count)),position = position_stack(vjust = 0.5)) + guides(fill = guide_none())+theme(legend.position = "none") show(plot_5_new)

Labeling pie charts using ggplot2

I have this code:
as_tibble(earlyCiliated[[]]) %>%
ggplot(aes(x="", y=Phase, fill=Phase)) + geom_col() +
coord_polar("y", start=0) +
geom_text(aes(label = paste0(Phase, "%")))
and my output looks like this:
What am I doing wrong that's causing the labels to all be on top of each other?
I can't completely recreate your plot because I do not have your data. That being said, you can try this:
install.packages("ggrepel")
library(ggrepel)
as_tibble(earlyCiliated[[]]) %>%
ggplot(aes(x="", y=Phase, fill=Phase)) + geom_col() +
coord_polar("y", start=0) +
geom_label_repel(data = earlyCiliated[[]],
aes(y = Phase, label = paste0(Phase, "%")),
size = 4.5, nudge_x = 1, show.legend = FALSE)
This is what it will look like (using other data because none was provided)
library(ggplot2)
library(ggrepel)
library(tidyverse)
df <- data.frame(value = c(15, 25, 32, 28),
group = paste0("G", 1:4))
# Get the positions
df2 <- df %>%
mutate(csum = rev(cumsum(rev(value))),
pos = value/2 + lead(csum, 1),
pos = if_else(is.na(pos), value/2, pos))
ggplot(df, aes(x = "" , y = value, fill = fct_inorder(group))) +
geom_col(width = 1, color = 1) +
coord_polar(theta = "y") +
scale_fill_brewer(palette = "Pastel1") +
geom_label_repel(data = df2,
aes(y = pos, label = paste0(value, "%")),
size = 4.5, nudge_x = 1, show.legend = FALSE) +
guides(fill = guide_legend(title = "Group")) +
theme_void()

Barplot in ggplot - dodge position + counting

Hey I have the following code:
df = data.frame(Type = c("A", "B", "A", "A", "B"), FLAG = c(1, 1, 0, 1, 0))
df
ggplot(df, aes(x = Type)) + geom_bar(stat = "count", aes(fill = factor(FLAG)), position = "dodge") + coord_flip() + stat_count(geom = "text", colour = "white", size = 3.5,
aes(label = ..count..),position=position_stack(vjust=0.5)) + theme_bw()
but it doesnt work as I want. The graph is OK but instead displaying the total number of observations of each type I want to display the number of each flag (so instead 2 for "B" type I want to display 1 and 1 because for "B" we have 1 observation with FLAG 1 and 1 observations with FLAG 0). What should I change?
With the interaction between Type and FLAG the bars display the counts per groups of both.
ggplot(df, aes(x = interaction(Type, FLAG))) +
geom_bar(stat = "count",
aes(fill = factor(FLAG)), position = "dodge") +
coord_flip() +
stat_count(geom = "text",
aes(label = ..count..),
position=position_stack(vjust=0.5),
colour = "white", size = 3.5) +
theme_bw()
You could replace the stat_count() and geom_bar() with a little pre-processing with count() and geom_col(). Here is an example:
df %>%
janitor::clean_names() %>%
count(type, flag) %>%
ggplot(aes(type, n, fill = as.factor(flag))) +
geom_col(position = "dodge") +
geom_text(aes(label = n, y = n - 0.05), color = "white",
position = position_dodge(width = 1)) +
scale_y_continuous(breaks = 0:3, limits = c(0,3)) +
labs(fill = "flag") +
coord_flip() +
theme_bw()
The only thing janitor::clean_names() does is transform variable names, from uppercase and spaces to lowercase and underscores, respectively.

R: show values in geom_bar(position = 'fill')

I have the following fake data:
n <- 100
set.seed(1)
df <- data.frame(grp = sample(c("A", "B", "C"), size = n, replace = TRUE),
values = sample(1:10, n, replace = TRUE) )
df
My goal is to have a "filled" barplot as follow, but I don't know how to use geom_text() in order to add the values of the percentages for each segment of the bars.
ggplot(df, aes(x = values, fill = grp)) +
geom_bar(position = 'fill') +
geom_text(??)
Can anyone help me please?
Are you looking for something like this?
df2 <- as.data.frame(apply(table(df), 2, function(x) x/sum(x)))
df2$grp <- rownames(df2)
df2 <- reshape2::melt(df2)
ggplot(df2, aes(x = variable, y = value, fill = grp)) +
geom_col(position = "fill") +
geom_text(aes(label = ifelse(value == 0, "", scales::percent(value))),
position = position_fill(vjust = 0.5)) +
scale_y_continuous(labels = scales::percent, name = "Percent") +
labs(x = "Value")

X axis and right box in stacked bars

In a plot like this
library(ggplot2)
df <- data.frame(class = c("a","b","a","b"), date = c(2009,2009,2010,2010), volume=c(1,1,2,0))
df <- df %>% group_by(date) %>% mutate(volumep = 100 * volume/sum(volume))
ggplot(df, aes(x = date, y = volumep, fill = class, label = volumep)) +
geom_bar(stat = "identity") +
geom_text(size = 3, position = position_stack(vjust = 0.5)) + coord_flip()
How is it possible to increase the text in the boxes in the right (class) and how to make the x axis have 0, 25, 50 and 100 values?
To answer the question, just adjust the involved aesthetics, y and size.
ggplot(df, aes(x = date, y = 100*volume, fill = class, label = volume)) +
geom_bar(stat = "identity") +
geom_text(size = c(3, 3, 5, 5), position = position_stack(vjust = 0.5)) +
coord_flip() +
ylab("volume")
Another option is to mutate the values of volume first. In this case, there would be no need to manually set the y axis label.
After the question's edit, the code is now as follows.
library(ggplot2)
library(dplyr)
df %>%
group_by(date) %>%
mutate(volume = 100*volume/sum(volume)) %>%
ggplot(aes(x = date, y = volume, fill = class, label = volume)) +
geom_bar(stat = "identity") +
geom_text(size = c(3, 3, 5, 5), position = position_stack(vjust = 0.5)) +
coord_flip()
ggplot(df, aes(x = date, y = volumnep, fill = class, label = volumnep)) +
geom_bar(stat = "identity") +
geom_text(size = 3, position = position_stack(vjust = 0.5)) +
coord_flip() +
theme(legend.title=element_text(size=22),
legend.text=element_text(size=22)) +
scale_y_continuous(breaks=c(0,25, 50, 100))
Edit:
I'd suggest recasting date as a factor:
ggplot(df, aes(x = factor(date), y = volumnep, fill = class, label = volumnep)) +
geom_bar(stat = "identity") +
geom_text(size = 3, position = position_stack(vjust = 0.5)) +
coord_flip() +
theme(legend.title=element_text(size=22),
legend.text=element_text(size=22)) +
scale_y_continuous(breaks=c(0,25, 50, 100)) +
labs(y="date")

Resources