Search code examples
rplotstripchart

stripchart by groups in R


I am still a newbie to R and would like to ask for help in graphing stripcharts.

stripchart(Study_intensive$AGE, method="stack",
at=c(0.05), pch=20, cex=2, xaxt="n", frame.plot=F, main= "Age Range in weight group(yr)")
axis(1, at=seq(0, 75, by=5) , labels= seq(0, 75, by=5),  
cex.axis=0.75)

This is my code at the moment and I am trying to group it by another column called "weightclass"; Basically using another color for each weightclass. "weightclass" has 4 values: 1, 2, 3, 4 respectively. Is there something I can do to easily do so?

Thank you for the help!


Solution

  • Here's an example using mtcars:

    library(RColorBrewer)
    testColor=brewer.pal(6, 'RdBu')
    stripchart(mtcars$mpg~mtcars$gear, col=testColor, method="stack", pch=20, cex=2, xaxt="n", frame.plot=F, main= "Age Range in weight group(yr)")
    axis(1, at=seq(0, 75, by=5) , labels= seq(0, 75, by=5), cex.axis=0.75)
    

    EDIT-1

    With ggplot there's a bit of a way to get what you asked for all in one strip and colored by group:

    library(ggplot2)
    library(RColorBrewer)
    testColor=brewer.pal(6, 'RdBu')
    mtcars$color=testColor[mtcars$gear] #to get the colors your after
    mtcars$strip=1 #to get them into a single strip
    ggplot(mtcars, aes(x=strip, y=mpg, color=color)) +
      geom_jitter(position=position_jitter(0.2)) + xlim(0, 2)