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
>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:
