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?
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"))