I have a column(dosage of a drug) of numbers ranging from 0.023 to 122 in a data frame (the numbers don't follow a pattern or anything they are random, and the min and max are 0.023 and 122 ). How do I create intervals and plot the frequency of those intervals as bar graphs in R? Additionally, how do I plot dosage VS Sex(Male, Female and Unkown) as bar graphs .
I'm new to using R so please help. Thank you!
CodePudding user response:
Using the hist()
function is a base R approach, which you can set the intervals with the breaks
argument.
For instance, if you have the following data frame:
set.seed(123)
df <- data.frame(dosage = runif(1000, min = 0.023, max = 122))
You can do a simple histogram where R sets the breaks for you:
hist(df$dosage)
Or you can set the breaks yourself:
# Randomly set the breaks (just for demonstration purposes)
brks1 <- 20 # even breaks
brks2 <- c(0, 1, 5, 10, 20, 50, 100, 150) # uneven breaks
hist(df$dosage,
breaks = brks1)
And if you are unfamiliar with plotting in R, of course you can make it prettier (or in this example, arguably uglier):
hist(df$dosage,
breaks = brks1,
xlab = "Dosage",
col = rep(2:6, each = 5),
main = "Histogram of Dosage")