Individually change x labels using expressions in ggplot2 boxplot with facet_grid in R - r

I want to individually change the x labels of my ggplot2 boxplot when using a facet_grid. I made the following simple example:
library(ggplot2)
data1 <- InsectSprays
data1$group <- "group 1"
data2 <- InsectSprays
data2$group <- "group 2"
plotData <- rbind(data1, data2)
ggplot(plotData, aes(x=spray, y=count, fill=spray))+
guides(fill=FALSE) +
facet_grid(. ~ group) +
geom_boxplot()
I want to change the labels on the x axis (A, B, C,...), but individually for the two groups. One way changing the labels would be, using:
scale_x_discrete(labels=c("label 1", "label 2", ...))
but this would change the labels in both groups to the same values. At the end I also need to be able to use expressions for the labels. Is there any way to achieve what I want?
EDIT:
There is a very simple way to solve my problem (thanks #Axeman). By using:
scale_x_discrete(labels=c('A' = expression(beta)))
I can change the labels. In my example this would change both groups, but for me it is possible to rename the labels to individual labels beforehand and than use this trick to use expressions for the labels.

plotData$x <- interaction(plotData$spray, plotData$group)
plotData$x <- factor(plotData$x, labels = paste('labels', 1:12))
ggplot(plotData, aes(x=x, y=count, fill=spray))+
geom_boxplot(show.legend = FALSE) +
facet_grid(. ~ group, scales = 'free')
I would have expected the following to work, but it doesn't!
ggplot(plotData, aes(x=interaction(spray, group), y=count, fill=spray))+
geom_boxplot(show.legend = FALSE) +
facet_grid(. ~ group, scales = 'free') +
scale_x_discrete(labels = paste('labels', 1:12))

Related

Change axes label and scale using ggplot and patchwork in R

(I am trying to make this question as short and concise as possible, as other related answers may be tough for the non-savvy like myself.)
With the following code in mind, is it possible to have both y-axes on the same scale (that of the graph with the highest y-limit), and to have independent labels for each of the axes (namely the y-axes)? I tried to use facet_wrap but haven't so far been able to succeed as Layer 1 is missing)
library(ggplot2)
library(patchwork)
d <- cars
d$Obs <- c(1:50)
f1 <- function(a) {
ggplot(data=d, aes_string(x="Obs", y=a)) +
geom_line() +
labs(x="Observation",y="Speed/Distance")
}
f1("speed") + f1("dist")
You could add two additional arguments to your function, one for the axis label and one for your desired limits.
library(ggplot2)
library(patchwork)
d <- cars
d$Obs <- c(1:50)
f1 <- function(a, y_lab) {
ggplot(data = d, aes_string(x = "Obs", y = a)) +
geom_line() +
scale_y_continuous(limits = range(c(d$speed, d$dist))) +
labs(x = "Observation", y = y_lab)
}
f1("speed", "Speed") + f1("dist", "Distance")
Reshape wide-to-long, then use facet. Instead of having different y-axis labels we will have facet labels:
library(ggplot2)
library(tidyr)
pivot_longer(d, 1:2, names_to = "grp") %>%
ggplot(aes(x = Obs, y = value)) +
geom_line() +
facet_wrap(vars(grp))

How to modify axis labels within ggplot labs()

Say I want to modify a ggplot axis label with the str_to_title() function.
library(tidyverse)
mtcars %>%
ggplot(aes(x = wt, y = mpg)) +
geom_point() +
labs(x = ~str_to_title(.x))
Rather than my x-axis being labeled 'Wt' it will be labeled 'str_to_title(.x)'. Is there a way to apply functions within the labs() function?
labs doesn't do programmatic NSE like many other components of ggplot2. One option is to define the columns programmatically, use aes_ and as.name (or other ways too) and it'll work.
library(ggplot2)
library(stringr) # str_to_title
xx <- "wt"; yy <- "mpg"
ggplot(mtcars, aes_(x = as.name(xx), y = as.name(yy))) +
geom_point() +
labs(x = str_to_title(xx))

R ggplot2: place value of column on top of stacked bars

There might be a duplicate, but I do not find an answer that applies to my particular case...
I just have a very simple data frame like the one below, with counts in two columns (Number_NonHit_Cells, Number_Hit_Cells) that I want to show in stacked bars, having the value of another column (Freq) placed on top of the stacked bars.
The MWE below is the best I have been able to get so far, but I only need the value of Freq once, and at the very top of the bars combined...
It would even be better if Freq could be calculated inside the ggplot2 call.
This is my MWE:
clono_df_long <- data.frame(Clonotype=LETTERS[1:5], Number_Hit_Cells=c(234,56,568,34,46),
Number_NonHit_Cells=c(c(52,12,234,21,31)))
clono_df_long$Clonotype_Size <- clono_df_long$Number_Hit_Cells+clono_df_long$Number_NonHit_Cells
clono_df_long$Freq <- round(clono_df_long$Number_Hit_Cells/clono_df_long$Clonotype_Size,4)*100
clono_df_long <- as.data.frame(tidyr::pivot_longer(clono_df_long,
-c(Clonotype,Clonotype_Size,Freq),
names_to = "Cells", values_to = "Value"))
clono_df_long$Clonotype <- factor(clono_df_long$Clonotype, levels=unique(clono_df_long$Clonotype))
clono_df_long$Cells <- factor(clono_df_long$Cells, levels=c('Number_NonHit_Cells','Number_Hit_Cells'))
P <- ggplot2::ggplot(clono_df_long, ggplot2::aes(x=Clonotype, y=Value, fill=Cells)) +
ggplot2::geom_bar(stat="identity") +
ggplot2::scale_fill_manual(values=c('gray70', 'gray40')) +
ggplot2::geom_text(ggplot2::aes(label=paste0(Freq,'%')), vjust=-1) +
ggplot2::theme_light()
grDevices::pdf(file='test.pdf', height=6, width=6)
print(P)
grDevices::dev.off()
Which produces this:
You may try
clono_df_long$Freq <- ifelse(clono_df_long$Cells == "Number_NonHit_Cells", clono_df_long$Freq, NA)
ggplot2::ggplot(clono_df_long, ggplot2::aes(x=Clonotype, y=Value, fill=Cells)) +
ggplot2::geom_bar(stat="identity") +
ggplot2::scale_fill_manual(values=c('gray70', 'gray40')) +
#ggplot2::geom_text(ggplot2::aes(label=paste0(Freq,'%')), vjust=-1) +
ggplot2::theme_light() +
ggplot2::geom_text(aes(label = scales::percent(Freq/100) ),position = "stack")

add number of observations to a multiple ggplot2 boxplots

I'm trying to create boxplots with descriptive information (mean, count, etc.).
I found a lot of examples of how to add the numbers for one boxplot with different groups, but I didn't found a way to add those numbers for multiple boxplots grid (facet_wrap).
for example, this article describes how to add numbers for one boxplot - I'm trying to do the same for multiple boxplots
library(reshape2)
library(ggplot2)
df.m <- melt(iris, id.var = "Species")
p <- ggplot(data = df.m, aes(x=variable, y=value)) +
geom_boxplot(aes(fill=Species))
p + facet_wrap( ~ variable, scales="free")
and on top of this plot - I want to add the relevant descriptive information on top of each box.
Create the function that makes counts and means
stat_box_data <- function(y) {
return(
data.frame(
y = 0.5+1.1*max(y), #may need to modify this depending on your data
label = paste('count =', length(y), '\n',
'mean =', round(mean(y), 1), '\n')
)
)
}
)
}
df.m <- melt(iris, id.var = "Species")
You may want to use this or something similar if you have large outliers instead of the y=0.5... bit above:
y=quantile(y,probs=0.95)*1.1,
Plot the data and use stat_summary with your custom function
ggplot(data = df.m, aes(x=Species, y=value)) +
geom_boxplot(aes(fill=Species))+
stat_summary(
fun.data = stat_box_data,
geom = "text",
hjust = 0.5,
vjust = 0.9
) +
facet_wrap( ~ variable, scales="free")

Display Greek symbols and charge facet titles at the same time with ggplot

I know how to modify titles in ggplot without altering the original data. Suppose I have the following data frame and I want to change the labels. Then, I would do so in the following way
df <- data.frame(x = 1:4, y = 1:4, label = c(c("params[1]", "params[2]", "params[3]",
"params[4]")))
params_names <- list(
'params[1]'= "beta[11]",
'params[2]'= "beta[22]",
'params[3]'= "beta[33]",
'params[4]'= "beta[44]"
)
param_labeller <- function(variable, value){
params_names[value]
}
ggplot(df, aes(x=x,y=y)) +
geom_point() +
facet_grid(~label, labeller = param_labeller)
If I wanted to display the subscripts, I would just do this
ggplot(df, aes(x=x,y=y)) +
geom_point() +
facet_grid(~label, labeller = label_parsed)
How do I apply both operations at the same time?
I don't know exactly if this conflicts with you not wanting to "alter" the original data, but you add the labelling information to the factor itself:
df$label2 <- factor(df$label,
labels = c("beta[4]", "beta[24]", "beta[42]", "beta[43]"))
ggplot(df, aes(x = x, y = y)) +
geom_point() +
facet_grid( ~ label2, labeller = label_parsed)
This produces the following plot:
Plot with formatted facet labels

Resources