Reputation: 889
I'm struggling to graph a boxplot from a dataframe. I have a dataframe df which contains 3 columns: df$A
, df$B
, df$C
. I want to have df$C
in the X-axis and plot df$A
and df$B
as boxplot on the Y-axis in one graph. So, for each value of x-axis, it should be two values df$A
and df$B
. I want to have the boxes are aligned for each value of X-axis (df$C
). I have tried to use interaction to combine (A
,B
) in column then graph it like this:
df$AandB <- interaction(df$A, df$fB)
ggplot(aes(y = AandB, x = df$C), data = df) + geom_boxplot()
BUT it didn’t work. It showed me only horizontal lines-sorry I couldn't upload the image as I'm new user.
I found some suggestions to use fill or colour but it didn’t work.
Any suggestions?
sample of my df:
A B C
200.12 30.11 28.75
100.75 26.17 29.98
27.33 25.58 34.98
25.19 22.6 35.56
40.03 21.02 37.51
20.3 18.31 44.75
Upvotes: 0
Views: 450
Reputation: 81713
The data:
df <- read.table(text="A B C
200.12 30.11 28.75
100.75 26.17 29.98
27.33 25.58 34.98
25.19 22.6 35.56
40.03 21.02 37.51
20.3 18.31 44.75", header = TRUE)
First, the data needs to be arranged in the long format. The values of A
and B
are combined in one column with the reshape2
package.
library(reshape2)
df_l <- melt(df, id.vars = "C")
Now, the plot can be created:
library(ggplot2)
ggplot(df_l, aes(x = C, y = value)) +
stat_summary(aes(group = C),
fun.y = mean, fun.ymin = min, fun.ymax = max, geom = "crossbar")
The crossbar denotes both the range and the mean of the data.
Upvotes: 1