收到错误消息“必须使用aes创建映射”,有关如何解决该错误的任何建议?

时间:2019-12-23 17:37:09

标签: r ggplot2

我有两个ggplots,它们分别工作,但是当我尝试将它们组合在一起时,出现使用es的错误,但正在使用它。任何帮助将不胜感激!

ggplot(TomFrostDO, aes( x = TomFrostDO$Date, y = TomFrostDO$Surface))+
  geom_point(color = "red")+
  geom_line(color = "red")+
  scale_x_date(date_breaks = "months", 
               date_labels = "%b%y")+
  scale_y_continuous(limits = c(0,15), breaks = c(0,5,10,15))


p<- ggplot(Chla, aes( x = as.Date(Chla$Date, format = "%d/%m/%Y"), y = Chla$`Chlorophyll a`))+
  geom_point(color = "red")+
  geom_line(color = "red")+
  scale_x_date(date_breaks = "months", 
               date_labels = "%b%y")+
  scale_y_continuous(limits = c(0,90), breaks = c(0,10,20,30, 40, 50, 60, 70, 80, 90)) 

p + geom_line(TomFrostDO, aes( x = as.Date(Date, format = "%d/%m/%Y"),y= Surface, color="blue"))

数据:

Tom Frost   
Surface
Date
1   6.66    2019-05-29
2   5.31    2019-06-10
3   1.90    2019-07-08
4   2.05    2019-07-11
5   6.72    2019-07-22
6   13.65   2019-08-05
7   0.69    2019-08-19
8   12.80   2019-08-22
9   3.83    2019-09-04
10  7.57    2019-09-16
11  9.33    2019-09-30
12  11.63   2019-10-14
13  9.82    2019-11-14


Chlorophyll a
                  Date
1   20.50560    2019-05-13
2   8.16000 2019-05-29
3   11.24604    2019-06-24
4   80.50050    2019-07-22
5   5.28660 2019-08-19
6   28.19520    2019-09-16

1 个答案:

答案 0 :(得分:1)

您的数据:

Chla <- structure(list(`Chlorophyll a` = c(20.5056, 8.16, 11.24604, 80.5005, 
5.2866, 28.1952), Date = c("2019-05-13", "2019-05-29", "2019-06-24", 
"2019-07-22", "2019-08-19", "2019-09-16")), row.names = c(NA, 
6L), class = "data.frame")

TomFrostDO <-structure(list(Surface = c(6.66, 5.31, 1.9, 2.05, 6.72, 13.65, 
0.69, 12.8, 3.83, 7.57, 9.33, 11.63, 9.82), Date = structure(1:13, .Label = c("2019-05-29", 
"2019-06-10", "2019-07-08", "2019-07-11", "2019-07-22", "2019-08-05", 
"2019-08-19", "2019-08-22", "2019-09-04", "2019-09-16", "2019-09-30", 
"2019-10-14", "2019-11-14"), class = "factor")), class = "data.frame", row.names = c(NA, 
-13L))

首先,我们将Chla中的Date转换为日期,而不是您的格式是Y-m-d,而不是您代码中的d / m / Y”

Chla$Date = as.Date(as.character(Chla$Date),format = "%Y-%m-%d")
p<- ggplot(Chla, aes_(x=as.name("Date"),y=as.name("Chlorophyll a")))+
  geom_point(color = "red")+
  geom_line(color = "red")+
  scale_x_date(date_breaks = "months", 
               date_labels = "%b%y")+
  scale_y_continuous(limits = c(0,90), 
breaks = c(0,10,20,30, 40, 50, 60, 70, 80, 90)) 

第二部分,您需要指定data =:

p+geom_line(data=TomFrostDO,
aes( x = as.Date(Date, format = "%Y-%m-%d"),y= Surface), color="blue")

enter image description here