Harshna Harshna - 2 months ago 12
R Question

How to Draw a Plot in R Which is Not a Box-and-Whiskers Plot

I have a large data set which has data like below:

Age Ethnicity Gender Crude_Rate
20-24 Pacific Islander Female 39.2
55-59 Caucasian Male 100.3
60-64 African Female 3.2
30-34 Asian Male 2.8


I am trying to draw three plots on R for:


  • crude rate (y-axis always) against age

  • crude rate against ethnicity

  • crude rate against gender



I am getting plots but it is a box-and-whisker plot with points above it which is not what I want. I would just like the points to be plotted i.e. a scatter plot.

The code I am using is:

melanoma <-read.csv("Melanoma Age.csv", header=TRUE, na.strings = c("Not Applicable"))
melanoma<-na.omit(melanoma)


plot(melanoma$Gender, melanoma$Crude_Rate,las=2, ylab="Crude Rate", xlab="Gender", main="Crude Rates for Both Genders")
plot(melanoma$Age, melanoma$Crude_Rate,las=2, ylab="Crude Rate", main="Crude Rates for Different Age Groups")
plot(melanoma$Ethnicity, melanoma$Crude_Rate,las=2, ylab="Crude Rate", main="Crude Rates for Different Ethnicities")


I don't get what I have done wrong because usually when I do this I get a scatter plot, not a box-and-whisker plot with points above it.

Thank you in advanced for any help

Answer

If your x values are a factor variable, you can produce a scatter plot like this:

#a reproducible example
set.seed(42)
x <- factor(sample(c("A", "B"), 20, TRUE))
y <- rnorm(20)

#use plot.default explicitly to avoid using plot.factor implicitly
plot.default(y ~ x, type = "p", 
             xlim = range(as.integer(unique(x))) + c(-0.4, 0.4), 
             xaxt = "n")
axis(1, at = seq_along(levels(x)), labels = levels(x))

resulting plot

Comments