首页 > 解决方案 > R中的参数图

问题描述

我正在尝试使用ggplot2UNvotes 数据制作一个绘图。我正在尝试参数化 Rmarkdown 中的绘图,但出现以下错误:

Error: Problem with `summarise()` input `perc_yes`. x non-numeric argument to binary operator i Input `perc_yes` is `sum(vote == "yes")/n`. i The error occurred in group 1: yr = 1991.

代码:

single_country %>% 
  inner_join(un_roll_calls %>% select(rcid, date), by = "rcid") %>%
  mutate(yr = year(date)) %>%
  group_by(yr) %>%
  summarize(perc_yes = sum(vote == "yes") / n) %>%
  ggplot(aes(x = yr, y = perc_yes)) + 
  geom_point() +
  geom_line() + 
  labs(title = plot_title)

不确定它是否缺少一些重新编码问题。

标签: rggplot2r-markdown

解决方案


在此之前更改此行ggplot

summarise(perc_yes = sum(vote == "yes") / n) %>%

用这条线:

summarise(n = n(), perc_yes = sum(vote == "yes") / n) %>%

其实你不见了n = n()

在此处输入图像描述


推荐阅读