I have a dataset (test) as given below:
Type Met1 Met2 Met3 Met4
TypeA 65 43 97 77
TypeA 46 25 76 77
TypeA 44 23 55 46
TypeA 46 44 55 77
TypeA 33 22 55 54
TypeB 66 8 66 47
TypeB 55 76 66 65
TypeB 55 77 88 46
TypeB 36 67 55 44
TypeB 67 55 76 65
I have checked a lot of links on box plots, but I still have not succeeded for the type of box plot I want. I wish to have a boxplot with my X-axis having type A (yellow, orange) for all the Mets (Met1, Met2, Met3, Met4). In essence, I want something like the following (taken from here):
I am trying somethings like,
boxplot(formula = len ~ Type , data = test, subset == "TypeA")
boxplot(formula = len ~ Type , data = test, subset == "TypeA", add=TRUE)
Legend(legend = c( "typeA", "typeB" ), fill = c( "yellow", "orange" ) )
But I am not able to work it out with any of it. Can anyone help me know how do I make such box plots on my test data in the corrected way?
Box plot for multiple groups In order to create a box plot by group in R you can pass a formula of the form y ~ x , being x a numerical variable and y a categoriacal variable to the boxplot function. You can pass the variables accessing the data from the data frame using the dollar sign or subsetting the data frame.
Boxplots are particularly useful for assessing quickly the location, dispersion, and symmetry or skewness of a set of data, and for making comparisons of these features in two or more data sets.
Use boxplots and individual value plots when you have a categorical grouping variable and a continuous outcome variable. The levels of the categorical variables form the groups in your data, and the researchers measure the continuous variable.
A solution with ggplot2
.
First, transform your data frame test
to the long format using melt
:
library(reshape2)
test.m <- melt(test)
Plot the data:
library(ggplot2)
ggplot(test.m, aes(x = variable, y = value, fill = Type)) +
geom_boxplot() +
scale_fill_manual(values = c("yellow", "orange"))
As others have said, first you need to melt your data.
df <- read.table(text="Type Met1 Met2 Met3 Met4
TypeA 65 43 97 77
TypeA 46 25 76 77
TypeA 44 23 55 46
TypeA 46 44 55 77
TypeA 33 22 55 54
TypeB 66 8 66 47
TypeB 55 76 66 65
TypeB 55 77 88 46
TypeB 36 67 55 44
TypeB 67 55 76 65",header=TRUE)
library(reshape2)
df2 <- melt(df)
boxplot(
formula = value ~ variable,
data = df2,
boxwex = 0.25,
at = 1:4 - 0.2,
subset = Type == "TypeA",
col = "yellow",
main = "blah",
xlab = "x",
ylab = "y",
ylim = c( 0, ceiling( max( df2$value ) ) + 1 ),
yaxs = "i" )
boxplot(
formula = value ~ variable,
data = df2,
boxwex = 0.25,
at = 1:4 + 0.2,
subset = Type == "TypeB",
col = "orange",
add = TRUE )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With