如何修复R中的“ summarise_impl(.data,点)中的错误:评估错误:非NA点为零”错误?

时间:2019-01-20 21:42:59

标签: r ggplot2 data-visualization

我正在尝试复制this visualization (for practice)

我面临的问题是该错误提示

  

summarise_impl(.data,点)中的错误:评估错误:非NA点为零。

我似乎不知道发生了什么事。
我试过删除est_alive_today变量,它生成了一个图,但是数据看起来很不合理(因此,该变量似乎对准确生成图很重要)。

library(babynames)
library(Hmisc)
library(ggplot2)

BabynamesDist <- make_babynames_dist()
data("babynames")
com_fem <- na.omit(com_fem)
com_fem <- BabynamesDist %>%
  filter(sex == "F") %>%
  group_by(name) %>%
  summarise(N = n(), 
            est_num_alive = sum(est_alive_today),
            q1_age = wtd.quantile(age_today, est_alive_today, probs = 0.25),
            median_age = wtd.quantile(age_today, est_alive_today, probs = 0.5),
            q3_age = wtd.quantile(age_today, est_alive_today, probs = 0.75)) %>%
  arrange(desc(est_num_alive)) %>%
  head(25)

w_plot <- ggplot(data = com_fem, 
                 aes(x = reorder(name, -median_age), y = median_age)) + 
  xlab(NULL) + ylab("Age (in years)") +
  ggtitle("Median ages for females with the 25 most common names")
w_plot <- w_plot + 
  geom_linerange(aes(ymin = q1_age, ymax = q3_age),
                 color = "#f3d478", size = 10, alpha = 0.8)
w_plot <- w_plot +
  geom_point(fill = "#ed3324", colour = "white", size = 4, shape = 21)
w_plot +
  geom_point(aes(y = 55, x = 24), 
             fill = "#ed3324", colour = "white", size = 4, shape = 21) +
  geom_text(aes(y = 58, x = 24, label = "median")) +
  geom_text(aes(y = 26, x = 16, label = "25th")) +
  geom_text(aes(y = 51, x = 16, label = "75th percentile")) +
  geom_point(aes(y = 24, x = 16), shape = 17) +
  geom_point(aes(y = 56, x = 16), shape = 17) +
  coord_flip()

我应该得到一个情节(不是我上面放置图片链接的那个副本),但是有点足够。

我的实际结果是错误:

  

summarise_impl(.data,点)中的错误:评估错误:非NA点为零。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

将此代码用于com_fem分配

com_fem <- BabynamesDist %>%
  filter(sex == "F") %>% 
  group_by(name) %>%
  summarise(
    N = n(),
    est_num_alive = sum(est_alive_today)
  ) %>% 
  arrange(desc(est_num_alive)) %>% 
  head(25) %>% 
  select(name) %>% 
  left_join(., BabynamesDist, by = "name") %>% 
  group_by(name) %>% 
  summarise(
    N = n(),
    est_num_alive = sum(est_alive_today),
    q1_age = wtd.quantile(age_today, weight = est_alive_today, probs = 0.25),
    median_age = wtd.quantile(age_today, weight = est_alive_today, probs = 0.5),
    q3_age = wtd.quantile(age_today, weight = est_alive_today, probs = 0.75) 
  )

此网站的道具:http://www.sci.csueastbay.edu/~esuess/classes/Statistics_651/Presentations/03_ggplot2_02/ggplot2_02_examples.Rmd