0

I have the following dataframe:

Year Ocean      O2_Conc
   <dbl> <chr>        <dbl>
 1 2010. Reference 0.000237
 2 2010. Pacific   0.000165
 3 2010. Southern  0.000165
 4 2012. Reference 0.000237
 5 2012. Pacific   0.000165
 6 2012. Southern  0.000165
 7 2012. Reference 0.000237
 8 2012. Pacific   0.000165
 9 2012. Southern  0.000165

I would like to plot this data in ggplot2 to produce a scatter plot with different oceans as different colours. I have tried the following code, which has worked for similar data:

ggplot(data=df, aes(x="Year", y="O2_Conc", color="Ocean")) + geom_point()

This has given me this output. Can someone explain why the numbers are not coming through on the graph's axes? GGplot output

1
  • 1
    Remove the double quotes from the ggplot call. Commented Jan 12, 2021 at 12:41

1 Answer 1

1

The following code plots the points, not the string "Ocean" but does more. It creates a new variable n counting the repeats of O2_Conc by year and ocean and treats the years as dates.

library(ggplot2)
library(dplyr)

df %>% 
  group_by(Year, Ocean) %>%
  mutate(n = n()) %>%
  mutate(Year = as.Date(paste(Year, "01", "01", sep = "-"))) %>%
  ggplot(aes(Year, O2_Conc, color = Ocean)) +
  geom_point(aes(size = n), alpha = 0.5, show.legend = FALSE) +
  scale_x_date(date_breaks = "year", date_labels = "%Y")

Data

df <- read.table(text = "
Year Ocean      O2_Conc
 1 2010. Reference 0.000237
 2 2010. Pacific   0.000165
 3 2010. Southern  0.000165
 4 2012. Reference 0.000237
 5 2012. Pacific   0.000165
 6 2012. Southern  0.000165
 7 2012. Reference 0.000237
 8 2012. Pacific   0.000165
 9 2012. Southern  0.000165
", header = TRUE)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.