Making basic graphics

Hello, basic beginner here.

I'm trying out different graphics in R and I made this scatter plot for Temperature and Solar Radiation using the basic airquality dataset. I thought it would be useful to visualise which points related to which month, however as they used numbers rather than names, the scale for the colour is continuous and I get a graduated colour palette rather than 5 distinct colours.

ggplot(airquality, aes(x=Temp, y=Solar.R))+
geom_point(aes(colour=Month))

Is there a way of assigning the numbers in the "Month" column to a specific name (ie 5="May") and then mapping that to a colour?
Thanks!

R has a datatype called factor, which is under the hood, integer values, but which prints as character strings.
Also here I use month.abb which is a built in vector of month abbreviations in R.

library(tidyverse)

(my_airq <- mutate(airquality,
                  Month = factor(Month,labels=month.abb[5:9])))

ggplot(my_airq, aes(x=Temp, y=Solar.R))+
  geom_point(aes(colour=Month))
1 Like

You've fixed my two-day headache thank you!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.