I have a spreadsheet of data with numerical values in one column and a second column named sex where it is either male or female.
How can I plot this data using a Boxplot so that I have separate plots for male and female but on the same graph?
I have a spreadsheet of data with numerical values in one column and a second column named sex where it is either male or female.
How can I plot this data using a Boxplot so that I have separate plots for male and female but on the same graph?
d.b's comment is referring to the command and arguments one needs in order to generate a boxplot of miles per gallon for cars of differing numbers of cylinders using a dataset that comes with base R called mtcars
. The first argument mpg ~ cyl
specifies the formula (create a boxplot of mpg
with the grouping by cyl
) and the second argument specifies the dataset (mtcars
).
This should be enough to get you going where you subsitute your own formula for mpg ~ cyl
and your own dataset for mtcars
. Here's another example where I'm creating fake data with rnorm
and runif
(these functions randomly sample from a normal distribution and a uniform distribution respectively) and trunc
truncates numbers (we want runif
to give whole numbers):
df <- data.frame(vals = rnorm(n=100, mean=0, sd=1), sex = trunc(runif(n=100, min=0, max=2)))
boxplot(vals ~ sex, df)