Help with plotting data

Hi, i am currently creating some plots in R but i need help.

I have currently made a plot with the following code:

#plot of age
plot(WTC_age$`Crowding – many`, type = 'l', main = "Effect of Age on the Willingness to Cycle",
     col="darkblue", ylab = "Distance (Km)", xlab = "Age", ylim=c(0,18))

lines(WTC_age$`Congestion – light`, col='purple')
lines(WTC_age$`Three stops`, col='cornflowerblue')
legend("topleft",                                       # Add legend to plot
       legend = c("Crowding","Congestion","Stops"),
       col = c("darkblue", "purple", "cornflowerblue"),
       lty = 1)

However, the x-axis is wrong as the age of the respondents varies from 18 to 78 years old.. instead of the way it is shown on the axis now (with 0-60). I have tried simply leaving out the x-axis and relabeling this with the axis function but then the plot also doesn't make sense anymore.

Basically what i want to do is ignore the numbering done by Rstudio itself, and changing the x-axis to the Age data. Can anyone help me?

Screenshot 2020-06-17 at 20.13.44

Try this as the plot() command

plot(WTC_age$Age, WTC_age$`Crowding – many`, type = 'l', main = "Effect of Age on the Willingness to Cycle",
     col="darkblue", ylab = "Distance (Km)", xlab = "Age", ylim=c(0,18))

That tells plot() to use the Age column for the values on the x axis.

Edit: You may also have to include the Age column when you call lines. I am not sure.

1 Like

Thank you, it worked!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.