Ask Your Question
2

How can I use R to create either a boxplot or barplot to display the frequency of sex in two diagnosis levels?

asked 2021-10-13 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-10-31 04:00:00 +0000

pufferfish gravatar image

Assuming you have a dataset with two columns: sex and diagnosis level, you can use the following code to create either a boxplot or a barplot:

Load the required libraries

library(ggplot2)

Create a sample dataset

sex <- c("Male", "Male", "Male", "Female", "Female", "Male", "Female", "Female", "Male", "Male") diagnosis <- c("Level 1", "Level 2", "Level 1", "Level 1", "Level 2", "Level 1", "Level 2", "Level 2", "Level 2", "Level 1") df <- data.frame(sex, diagnosis)

Create a boxplot

ggplot(df, aes(x = diagnosis, y = sex)) + geom_boxplot() + ylab("Sex") + xlab("Diagnosis Level")

Create a barplot

ggplot(df, aes(x = diagnosis, fill = sex)) + geombar(position = "dodge") + ylab("Frequency") + xlab("Diagnosis Level") + scalefill_manual(values = c("blue", "pink"), name = "Sex")

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-13 11:00:00 +0000

Seen: 10 times

Last updated: Oct 31 '22