0

我有以下数据框:

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

我想在 ggplot2 中绘制这些数据,以生成一个散点图,其中不同的海洋颜色不同。我尝试了以下代码,它适用于类似的数据:

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

这给了我这个输出。有人可以解释为什么数字没有出现在图表的轴上吗? GGplot输出

4

1 回答 1

0

以下代码绘制了点,而不是字符串"Ocean",但做了更多。它创建了一个新变量n,计算O2_Conc按年份和海洋的重复次数,并将年份视为日期。

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")

数据

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)
于 2021-01-12T12:51:26.507 回答