Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating boxplot based on three variables

How is it possible to create a box plot like this

data.frame(category = c(2, 3, 3, 3, 2, 2, 3, 3, 3, 3, 2, 3, 1, 3), educational_years = c(6, 15, 16, 6, 6, 6, 12, 12, 12, 4, 12, 15, 6, 4), gender = c("M", "F", "F", "F", "M", "M", "F", "M", "F", "F", "M", "F", "M", "F"))

from a data frame like this and x axis have the category y axis age and factor gender

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You don’t have enough data to create a boxplot like the one shown. For example, you only have a single data point for category 1, so you will only get a single horizontal line here. You only have "M" values for category 2, so you will only get a single box here. You have only a single value for "M" in category three, so you will get a horizontal line instead of a box.

Assuming this is only a sample of your data, rather than the full data set, the code would look like this:

library(ggplot2)

ggplot(df, aes(factor(category), educational_years, fill = gender)) + 
  geom_boxplot()

At the moment, the result obtained looks like this:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading