Maintain space between bars regardless of sample numbers - r

I want to make several plots same space interval between bars regardless of sample numbers
As described below.
Not like this
My original script is
myData <- read.csv('L1L2_100percent.csv', header = T, sep =',')
ggplot(data = myData, aes(x = region, y = vaf, fill = type)) +
geom_bar(stat = "identity", width = 0.4) +
coord_flip()
I think it has to do with saving pictures with a specific height, but I think the height
has to be determined with sample numbers but I don't know the exact value.

You just need to adjust the height of the plot on saving proportional to the number of bars.
Here, plt1 has 5 instead of 10 bars thus it should be saved with a height that is only half than those of plt2:
library(tidyverse)
# create example data
myData <-
iris %>% transmute(
region = row_number() %% 20,
vaf = Sepal.Length,
type = Species
)
plt1 <-
myData %>%
filter(region < 5) %>%
ggplot(aes(x = region, y = vaf, fill = type)) +
geom_bar(stat = "identity") +
coord_flip()
plt2 <-
myData %>%
filter(region < 10) %>%
ggplot(aes(x = region, y = vaf, fill = type)) +
geom_bar(stat = "identity") +
coord_flip()
ggsave("plt1.png", plt1, height = plt1$data$region %>% unique() %>% length())
ggsave("plt2.png", plt2, height = plt2$data$region %>% unique() %>% length())
Plt1:
Plt2:

Related

Placing data labels for stacked bar chart at top of bar

I have been attempting to add a label on top of each bar to represent the proportion that each ethnic group makes up in referrals.
For some reason I cannot get the labels to be placed at the top of each bar. How do I fix this?
My code below
freq <- df %>%
group_by(ethnicity) %>%
summarise(n = n()) %>%
mutate(f = round((n/sum(n)*100, 1))
df %>%
group_by(pathway) %>%
count(ethnicity) %>%
ggplot(aes(x = ethnicity, y = n , fill = pathway)) +
geom_bar(stat = "identity", position = "stack") +
geom_text(data = freq,
aes(x= ethnicity, y = f, label = f),
inherit.aes = FALSE) +
theme(legend.position = "bottom") +
scale_fill_manual(name = "",
values = c("light blue", "deepskyblue4"),
labels = "a", "b") +
xlab("") +
ylab("Number of Referrals") +
scale_y_continuous(breaks = seq(0, 2250, 250), expand = c(0,0)
Here is what it currently looks like
Since you are using the count as your y-axis position in geom_bar, you need to use the same thing in your geom_text to get the labels in the right place. Below is an example using mtcars dataset. Using vjust = -1 I put a little bit of space between the label and the bars to make it more legible and aesthetically pleasing.
library(tidyverse)
mtcars %>%
group_by(carb) %>%
summarise(n = n()) %>%
mutate(f = round(proportions(n) * 100, 1)) -> frq
mtcars %>%
group_by(gear) %>%
count(carb) -> df
df %>%
ggplot(aes(x = carb, y = n, fill = gear)) +
geom_bar(stat = "identity", position = "stack") +
geom_text(data = frq,
vjust = -1,
aes(x= carb, y = n, label = f),
inherit.aes = FALSE)
Created on 2022-10-31 by the reprex package (v2.0.1)

Label grouped bar plot in R

I'm tryng to add label to a grouped bar plot in r.
However I'm using percentege in the y axis, and I want the label to be count.
I've tried to use the geom_text() function, but I don't how exacly the parameters i need to use.
newdf3 %>%
dplyr::count(key, value) %>%
dplyr::group_by(key) %>%
dplyr::mutate(p = n / sum(n)) %>%
ggplot() +
geom_bar(
mapping = aes(x = key, y = p, fill = value),
stat = "identity",
position = position_dodge()
) +
scale_y_continuous(labels = scales::percent_format(),limits=c(0,1))+
labs(x = "", y = "%",title="")+
scale_fill_manual(values = c('Before' = "deepskyblue", 'During' = "indianred1", 'After' = "green2", '?'= "mediumorchid3"),
drop = FALSE, name="")
Here is an exemple of how I need it:
here's a sample of data I'm using:
key value
A Before
A After
A During
B Before
B Before
C After
D During
...
I also wanted to keep the bars with no value (label = 0).
Can someone help me with this?
Here is MWE of how to add count labels to a simple bar chart. See below for the case when these are grouped.
library(datasets)
library(tidyverse)
data <- chickwts %>%
group_by(feed) %>%
count %>%
ungroup %>%
mutate(p = n / sum(n))
ggplot(data, aes(x = feed, y = p, fill = feed)) +
geom_bar(stat = "identity") +
geom_text(stat = "identity",
aes(label = n), vjust = -1)
You should be able to do the same thing on your data.
EDIT: StupidWolf points out in the comments that the original example has grouped data. Adding position = position_dodge(0.9) in geom_text deals with this.
Again, no access to the original data, but here's a different MWE using mtcars showing this:
library(datasets)
library(tidyverse)
data <- mtcars %>%
as_tibble %>%
transmute(gear = as_factor(gear),
carb = as_factor(carb),
cyl = cyl) %>%
group_by(gear, carb) %>%
count
ggplot(data, aes(x = gear, y = n, fill = carb)) +
geom_bar(stat = "identity",
position = "dodge") +
geom_text(aes(label = n),
stat = "identity",
vjust = -1,
position = position_dodge(0.9))

How to sort bars after grouped top_n in facet_wrap with ggplot2?

I'm facing an issue with sorting bars when using facet_wrap (which is commonly reported here, here and others) after group variables and get top values.
When I run the code without factor conversion, bars are ordered:
iris %>%
gather(key = measurements, value = values, - Species) %>%
mutate(kk = factor(measurements, levels = unique(.$measurements)),
species_l = with(., paste(Species, .$measurements, sep = "_"))) %>%
ggplot(aes(x = reorder(species_l, values),
y = values,
fill = kk)) +
geom_bar(stat = "identity") +
facet_wrap(.~kk,
scales = "free")
But now I want to order decreasingly bars within facet_wrap and after top_n.
Heres is what I've tried so far:
library(tidyverse)
iris %>%
gather(key = measurements, value = values, - Species) %>%
within(.,
Species <- factor(Species,
levels=names(sort(table(Species),
decreasing=FALSE)))) %>%
ggplot(aes(x = Species,
y = values,
fill = measurements)) +
geom_bar(stat = "identity") +
facet_wrap(.~ measurements,
scales = "free")
and this:
iris %>%
gather(key = measurements, value = values, - Species) %>%
group_by(measurements, Species) %>%
top_n(5, wt = values) %>%
ggplot(aes(x = reorder(Species, Species,
function(x)-length(x)),
y = values,
fill = measurements)) +
geom_bar(stat = "identity") +
facet_wrap(.~measurements,
scales = "free")
and this:
iris %>%
gather(key = measurements, value = values, - Species) %>%
mutate(kk = factor(measurements, levels = unique(.$measurements)),
species_l = with(., paste(Species, .$measurements, sep = "_"))) %>%
group_by(measurements, Species) %>%
top_n(5, wt = values) %>%
ungroup() %>%
ggplot(aes(x = reorder(species_l, values),
y = values,
fill = kk)) +
geom_bar(stat = "identity") +
facet_wrap(.~kk,
scales = "free")
This is what I get:
As you can see Sepal.Width bars are not sorted.
Your first attempt was close -- you need to make sure you're reordering per facet, and not just reordering the factor based on the top 5 values of all measurements. Julia Silge explains thoroughly here
library(tidytext)
library(tidyverse)
library(magtrittr)
iris %>%
gather(key = measurements, value = values, - Species) %>%
mutate(kk = factor(measurements, levels = unique(.$measurements)),
#The '-values' below specifies to order in descending
Species = reorder_within(Species, -values, measurements)) %>%
ggplot(aes(x = Species, y = values, fill = kk)) +
geom_bar(stat = "identity") +
facet_wrap(.~kk, scales = "free") +
scale_x_reordered()`

Draw a line on top of stacked bar_plot

I would like to draw a line (or making points) on top of my stacked bar_plots. As I have no real data points I can refer to (only the spereated values and not the sum of them) I don't know how I can add such line. The Code produce this plot:
I want to add this black line(my real data are not linear):
library(tidyverse)
##Create some fake data
data3 <- tibble(
year = 1991:2020,
One = c(31:60),
Two = c(21:50),
Three = c(11:40)
)
##Gather the variables to create a long dataset
new_data3 <- data3 %>%
gather(model, value, -year)
##plot the data
ggplot(new_data3, aes(x = year, y = value, fill=model)) +
geom_bar(stat = "identity",position = "stack")
You can use stat_summary and sum for the summary function:
ggplot(new_data3, aes(year, value)) +
geom_col(aes(fill = model)) +
stat_summary(geom = "line", fun.y = sum, group = 1, size = 2)
Result:
You could get sum by year and plot it with new geom_line
library(dplyr)
library(ggplot2)
newdata4 <- new_data3 %>%
group_by(year) %>%
summarise(total = sum(value))
ggplot(new_data3, aes(x = year, y = value, fill=model)) +
geom_bar(stat = "identity",position = "stack") +
geom_line(aes(year, total, fill = ""), data = newdata4, size = 2)

ggplot: Desaturate/Make gray bars based on column value

I would like to desaturate (or make gray) certain bars in a bar chart. I figured out how to make certain bars more transparent than others based on a column value using alpha=column_name in geom_bar. But I would like to make them gray not transparent. Is there a way to do it natively in ggplot?
Below is the code to make certain columns transparent based on column S. Is there a way to desaturate/make gray specific bars based on column value?
Edited code to include data.
data_t<-as.data.frame(Titanic)
join_table<- data_t %>% group_by(X=Class) %>% summarise(Y=sum(Freq)) %>%
mutate(S=ifelse(Y-mean(Y)>0,"a","b")) %>% select(X, S); join_table
data_t %>% group_by(X=Class, groups=Sex) %>% summarise(Y=sum(Freq)) %>%
inner_join(join_table, by=c("X")) %>%
ggplot(aes(x=X, y=Y)) + theme_minimal() +
geom_bar(aes(fill=groups, alpha=S), stat = "identity", position = "dodge")
Thanks in advance!
You can subset the data plotted each time and adjust the fill colour accordingly. The following should work:
library(tidyverse)
data_t <- as.data.frame(Titanic)
join_table <- data_t %>%
group_by(X = Class) %>%
summarise(Y = sum(Freq)) %>%
mutate(S = ifelse(Y - mean(Y) > 0, "a", "b")) %>%
select(X, S)
data_t %>%
group_by(X = Class, groups = Sex) %>%
summarise(Y = sum(Freq)) %>%
inner_join(join_table, by = c("X")) %>%
ggplot(aes(x = X, y = Y)) +
theme_minimal() +
geom_bar(data = . %>% filter( S == "b"),
aes(fill = groups), stat = "identity", position = "dodge") +
geom_bar(data = . %>% filter( S == "a"),
aes(group = groups),
fill = "grey80", stat = "identity", position = "dodge")
You can adjust fill = "grey80" to get the exact colour you'd like.

Resources