【问题标题】:ggplot2_Error: geom_point requires the following missing aesthetics: yggplot2_Error:geom_point 需要以下缺失的美学:y
【发布时间】:2015-04-05 17:05:00
【问题描述】:

我正在尝试在 RStudio 中运行 rWBclimate 包。我从ROpenSci 复制了以下代码并粘贴到 RStudio 中。但我收到错误消息“不知道如何为列表类型的对象自动选择比例。默认为连续 错误:geom_point 需要以下缺失的美学:y

gbr.dat.t <- get_ensemble_temp("GBR", "annualavg", 1900, 2100)

## Loading required package: rjson

### Subset to just the median percentile
gbr.dat.t <- subset(gbr.dat.t, gbr.dat.t$percentile == 50)
## Plot and note the past is the same for each scenario
     ggplot(gbr.dat.t,aes(x=fromYear,y=data,group=scenario,colour=scenario))    
+ geom_point() +
geom_path() +
theme_bw() +
xlab("Year") +
ylab("Annual Average Temperature in 20 year increments")

我也尝试通过以下方式使用 geom_point(stat="identity") 但没有奏效:

ggplot(gbr.dat.t,aes(x=fromYear,y=data,group=scenario,colour=scenario))    
+ geom_point(stat="identity") +
geom_path() +
theme_bw() +
xlab("Year") +
ylab("Annual Average Temperature in 20 year increments")

我仍然收到相同的消息“不知道如何为列表类型的对象自动选择比例。默认为连续 错误:geom_point 需要以下缺失的美学:y"

另外,str(gbr.dat.t) 的结果如下:

> str(gbr.dat.t)
'data.frame':   12 obs. of  6 variables:
$ scenario  : chr  "past" "past" "past" "past" ...
$ fromYear  : int  1920 1940 1960 1980 2020 2020 2040 2040 2060 2060 ...
$ toYear    : int  1939 1959 1979 1999 2039 2039 2059 2059 2079 2079 ...
$ data      :List of 12
..$ : num 9.01
..$ : num 9.16
..$ : num 9.05
..$ : num 9.36
..$ : num 10
..$ : num 9.47
..$ : num 9.92
..$ : num 10.7
..$ : num 10.3
..$ : num 11.4
..$ : num 12.1
..$ : num 10.4
$ percentile: int  50 50 50 50 50 50 50 50 50 50 ...
$ locator   : chr  "GBR" "GBR" "GBR" "GBR" ...

寻找有用的答案。

【问题讨论】:

  • 请将str(gbr.dat.t) 的输出添加到您的问题中。

标签: r ggplot2


【解决方案1】:

希望这会有所帮助。我所做的只是将 gbr.dat.t$data 转换为数字向量

library('rWBclimate')
library("ggplot2")

gbr.dat.t <- get_ensemble_temp("GBR", "annualavg", 1900, 2100)

## Loading required package: rjson

### Subset to just the median percentile
gbr.dat.t <- subset(gbr.dat.t, gbr.dat.t$percentile == 50)

#This is the line you were missing
gbr.dat.t$data <- unlist(gbr.dat.t$data)

## Plot and note the past is the same for each scenario
ggplot(gbr.dat.t,aes(x=fromYear,y=data,group=scenario,colour=scenario)) + geom_point() +
  geom_path() +
  theme_bw() +
  xlab("Year") +
  ylab("Annual Average Temperature in 20 year increments")

【讨论】:

  • 谢谢。它刚刚奏效。但不知道为什么我们需要取消列出这个?这是我第一次在 ggplot2 中遇到这样的事情,虽然我是新手。
  • 这是因为 $data 中的每个值都是一个列表项。这让ggplot感到困惑。或者,您可以在 ggplot ..y=unlist(data).. 中取消列出,这样您就不会修改原始数据集
猜你喜欢
  • 2019-06-06
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 2019-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多