I have a problem with ggplot. Assuming I have the dataframe 'df_temp' I would like to plot the boxplots for the 7 columns. However, the second and third columns have the same name 'B'. How can I have a plot where on the x-axis at the letter 'B' I have two boxplots (related to the second and third columns)? I attach the code for a toy example. Thanks to anyone who can help me out!
df_temp <- data.frame(
A = c(0.206, 0.192, 0.187, 0.213, 0.167, 0.209, 0.213, 0.155, 0.190, 0.192),
B = c(0.260, 0.210, 0.215, 0.280, 0.271, 0.230, 0.232, 0.238, 0.220, 0.278),
B = c(0.185, 0.276, 0.233, 0.225, 0.239, 0.242, 0.231, 0.195, 0.198, 0.200),
C = c(0.204, 0.246, 0.212, 0.252, 0.209, 0.244, 0.206, 0.212, 0.210, 0.276),
D = c(0.218, 0.201, 0.230, 0.189, 0.241, 0.193, 0.225, 0.198, 0.239, 0.199),
E = c(0.275, 0.268, 0.257, 0.273, 0.258, 0.262, 0.210, 0.274, 0.285, 0.306),
F = c(0.234, 0.239, 0.213, 0.233, 0.250, 0.217, 0.253, 0.217, 0.277, 0.221))
library(reshape2)
df_long <- melt(df_temp)
library(ggplot2)
ggplot(data = df_long, aes(x = variable, y = value)) +
geom_boxplot(width = 0.5, outliers = FALSE) +
ylab("Value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 0, hjust = 0.5, size = 12), text = element_text(size = 14)) +
theme(axis.text.y = element_text(angle = 0, hjust = 0.5, size = 12)) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank()) +
theme(axis.text.x = element_text(size = 12, angle = 90, hjust = 0.5)) +
coord_cartesian(ylim = c(0.17, 0.45))
Or if you want the two "B" columns to appear in the same x position, you could assign them to the same x variable, while keeping them in different groups.
df_long <- melt(df_temp) |>
transmute(x_var = ifelse(variable == "B.1", "B", as.character(variable)),
variable, value)
library(ggplot2)
ggplot(data = df_long, aes(x = x_var, y = value, group = variable)) +
...