Change the colour of specific data points according to criteria in ggplot2 and add data labels to those points

Hi All

I have a NOAA dataset with 50+ entries of CO2 concentration against year. I want to point out the values for each decade i.e. those concentrations for 1980, 1990, 2000 etc. I think either a different colour and/or data labels would be good. I already tried to produce a scatterplot using ggplot2 and have an error (Error in +xlab("year") : invalid argument to unary operator), and I am especially stuck when it comes to labelling specific points or coloring specific points according to a rule (every 10 years).

This is my code so far:

ggplot(NOAA) + geom_point(aes(x=year,y=mean),size=1) + theme(axis.title = element_text(size=rel(1.5)))
+ xlab("year")+ ylab("Mean annual CO2 concentration [ppm]")

Any help with this would be really appreciated!


its problematic to start a new line with + symbol. you should rather end the previous line with the + symbol

Thanks, that helped with the error message. I just need some help with the other part now :slight_smile:

(fakenoa <- tibble(
  year = 1980:2021,
  mean = 100 + cumsum(sample((-10:10) / 10, 42, replace = TRUE))
) %>% mutate(is_decade = year %% 10 == 0))

ggplot(fakenoa) +
  geom_point(aes(x = year, y = mean, colour = is_decade, size = is_decade)) +
  theme(axis.title = element_text(size = rel(1.5))) +
  xlab("year") +
  ylab("Mean annual CO2 concentration [ppm]") +
  scale_size_manual(values = c(2, 5))

I would suggest creating a new variable decade and do something like

ggplot(NOAA) + geom_point(aes(x=year,y=mean, colour = decade))

The dataset looks like this but with 62 rows.

I am not interested in the third column.

Here is my code:
decade <- c(1960,1970,1980,1990,2000,2010,2020)
ggplot(NOAA) + geom_point(aes(x="year",y="mean",colour=decade))

I get this error message

Error: Aesthetics must be either length 1 or the same as the data (62): colour

I have no idea what the error message means in this case, though I have read about it in general.

the error means your 7 decades isnt the same length as your 62 years of info.

See the solution I provided above.

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.