【问题标题】:Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "function"UseMethod(“filter_”)中的错误:没有适用于“filter_”的方法应用于类“function”的对象
【发布时间】:2021-06-06 20:06:16
【问题描述】:

正在尝试运行此代码

data %>% filter('region' != "Antarctica") %>%
    ggplot(aes(long, lat, group=group)) + 
    geom_polygon(fill="black", color ="white", size=0.15) +
    geom_curve(data = clean_flight, aes(x=src_long, xend=dest_long,
                                        y=src_lat, yend=dest_lat),
                                        alpha=0.25, size=0.05,
                                        color="white", inherit.aes=FALSE)+
    theme_void() +
    theme(plot.background=element_rect(fill="black"), legend.position="none") +
    coord_equal()

我指的是这个网站
Visualizing flight data with ggplot2
无法弄清楚为什么我会收到此错误以及我应该怎么做。
提前致谢。

【问题讨论】:

  • 您的环境中是否可能没有名为 data 的数据集?

标签: r ggplot2 dplyr tidyverse


【解决方案1】:

由于未定义对象名称而发生错误

data %>%
   filter('region' != "Antarctica")
Error in UseMethod("filter") : 
  no applicable method for 'filter' applied to an object of class "function"

data默认是function(检查?data),它加载指定的数据集,其用法是

data(iris)
head(iris)

OP 可能想先从csvexcel 文件中读取数据

dat1 <- read.csv('file.csv')
dat1 %>%
   filter(region != "Antarctica")

请注意,我们不需要在列名周围加上引号


根据更新,使用的数据是clean_flights而不是clean_flight

library(ggplot2)
data1 <- map_data('world')
library(readr)
library(dplyr)
airports <- read_csv("https://raw.githubusercontent.com/sjfox/sjfox.github.io/master/post/data/flights/airports.csv",
 col_names = c("id", "name", "city", "country", "iata", "icao", "lat", "long",
                                   "alt", "tz", "dst", "tz2", "type", "source"), na = c("\\N"))
flights <- read_csv("https://raw.githubusercontent.com/sjfox/sjfox.github.io/master/post/data/flights/routes.csv",
 col_names = c("airline", "id", "src_airport", "src_airport_id", "dest_airport",
                                  "dest_airport_id", "codeshare", "stops", "equip"), na = c("\\N"))
airports <- airports %>% select(id, lat, long)
flights <- flights %>% select(src_airport_id, dest_airport_id) %>%
              filter(!is.na(src_airport_id) & !is.na(dest_airport_id))

clean_flights <- flights %>%  
  left_join(airports, by = c("src_airport_id" = "id")) %>%  
  rename(src_lat=lat, src_long=long) %>%  
  left_join(airports, by = c("dest_airport_id" = "id")) %>%  
  rename(dest_lat=lat, dest_long=long)  %>%  
  filter(src_airport_id != dest_airport_id)  %>%
  mutate(ordered_pair = if_else(src_airport_id > dest_airport_id,
                                paste0(dest_airport_id,src_airport_id),
                                paste0(src_airport_id,dest_airport_id))) %>%
  filter(!duplicated(ordered_pair)) %>%
  mutate(is_europe = if_else(dest_lat < 60 & dest_lat > 25 & src_lat < 60 & src_lat > 10 &
                             dest_long < 50 & dest_long > -15 & src_long < 50 & src_long > -15, TRUE,FALSE)) %>%
  filter(!is.na(is_europe))
  
data1 %>% filter(region != "Antarctica") %>%
    ggplot(aes(long, lat, group=group)) + 
    geom_polygon(fill="black", color ="white", size=0.15) +
    geom_curve(data = clean_flights, aes(x=src_long, xend=dest_long,
                                        y=src_lat, yend=dest_lat),
                                        alpha=0.25, size=0.05,
                                        color="white", inherit.aes=FALSE)+
    theme_void() +
    theme(plot.background=element_rect(fill="black"), legend.position="none") +
    coord_equal()

-输出

【讨论】:

  • 我更改了变量名。 ``` data1
  • @HansikaSachdeva 你从哪里得到clean_flight 数据
  • clean_flight 是机场和航班的组合数据集
猜你喜欢
  • 2020-04-10
  • 2019-11-28
  • 1970-01-01
  • 2019-09-12
  • 2022-12-15
  • 2023-01-22
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多