Reputation: 87
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
Upvotes: 0
Views: 178
Reputation: 76402
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)
Upvotes: 1