Search code examples
rsortingplotmultiple-axes

Sorting the x axes in R


I built a logistic regression model (called 'mylogit') using the glm function in R as follows:

mylogit <- glm(answer ~ as.factor(gender) + age, data = mydata, family = "binomial")

where age is numeric and gender is categorical (male and female).

I then proceeded to make predictions with the model built.

pred <- predict(mylogit, type = "response")

I can easily make a time series plot of the predictions by doing:

plot.ts(ts(pred))

to give a plot that looks like this:

Plot of Time against Predictions

which gives a plot of the predictions.

My question is this: Is it possible to put the x axis in segments according to gender (male or female) which was specified in the glm? In other words, can I have predictions on the y axis and have gender (divided into male and female) on the x axis?

A sample of the data I want to plot from is similar to this:

I did:

bind = cbind(mydata, pred)

'bind' looks like this:

pred          age        gender
0.9461198     32          male
0.9463577     45         female
0.9461198     45         female
0.9461198     37         female
0.9477645     40          male
0.8304513     32         female

Solution

  • I don't think you need to use ts and plot.ts because the data you have is not a time series, right? Just sort pred before plotting.

    # Get data
    str <- "pred,age,gender
    0.9461198,32,male
    0.9463577,45,female
    0.9461198,45,female
    0.9461198,37,female
    0.9477645,40,male
    0.8304513,32,female"
    bind <- read.csv(textConnection(str))
    
    # Plot
    bind <- bind[order(bind$gender),]
    plot(bind$pred, col = bind$gender)
    
    library(ggplot2)
    ggplot(bind, aes(x = gender, y = pred)) +
      geom_point(position = position_jitter(width = .3))
    

    Or without creating bind you could do plot(pred[order(mydata$gender)]).