0
votes

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?

2
Could you please explain what the notation means?Valerii Tarasova

2 Answers

1
votes

As d.b mentioned,

boxplot(mpg ~ cyl, mtcars)

where the tilde means "is explained by". So in his example, the mpg of a car "is explained by" the number of cylinders. The graph shows a box for each of those number of cylinders.

1
votes

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)