This is my sample data:
x1 <- c(1, 2, 3, 4, 5)
x2 <- c(6, 7, 4, 5, 7)
x3 <- c(4, 5, 3, 7, 1)
x4 <- c(3, 5, 6, 4, 2)
x5 <- c(1, 3, 4, 4, 2)
x6 <- c(4, 5, 4, 3, 5)
df <- data.frame(x1 = x1, x2 = x2, x3 = x3, x4 = x4, x5 = x5, x6 = x6)
df <- df %>%
rowwise() %>%
mutate(
var1_mean = mean(c(x1, x2, x3)),
var2_mean = mean(c(x4, x5, x6))
)
What I want, is a boxplot that shows the mean of var1_mean
, including standard deviation, in the first box, and the same thing for var2_mean
in the other box. The code below seems to do the job, but I'm not sure because I don't understand this rep
function. Could you please clarify this to me?
plot_df <- data.frame(
Variable = c(rep("var1_mean", nrow(df)), rep("var2_mean", nrow(df))),
Value = c(df$var1_mean, df$var2_mean)
)
ggplot(plot_df, aes(x = Variable, y = Value, fill = Variable)) +
geom_boxplot() +
labs(x = "", y = "Mean Value") +
ggtitle("Box Plot of var1_mean and var2_mean") +
theme_minimal()