自定义绘图功能找不到现有列

时间:2019-11-21 19:39:47

标签: r ggplot2 dplyr

我写了一个快速的函数来绘制一些人口统计信息,但是它无法识别我选择的变量。我知道poverty_rate是我数据框中的一列,当我执行names(df)并且在先前的代码块中使用过它时,它会显示出来。是的,当我运行下面的函数时,我得到Error in mean(demog, na.rm = TRUE) : object 'poverty_rate' not found。我在做什么错了?

dense_plot_demos <- function(df, id_list, demog, color_id, demo_name, qual) {
  df %>%
  filter(GEOID %in% id_list) %>%
  group_by(GEOID) %>%
  mutate(avg_var = mean(demog, na.rm=TRUE)) %>%
  ggplot(aes(x=avg_var)) + 
  geom_histogram(aes(y=..density..), color="black", fill="white", bins=20) + 
  geom_density(alpha=.5, fill=color_id) + 
  theme_bw() + 
  xlab(demo_name) + 
  ylab("Density") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(title = paste(demo_name, "in", qual, "Filing Counties"))
}
top_pov <- dense_plot_demos(df, top_ids, poverty_rate, color_pal[1], "Poverty Rate", "Highest")

ETA::谢谢您的所有建议和资源。我发现以下代码对我的函数进行了最小的修改:

dense_plot_demos <- function(df, id_list, demog, color_id, demo_name, qual) {
  demog <- sym(demog)
  df %>%
  filter(GEOID %in% id_list) %>%
  group_by(GEOID) %>%
  mutate(avg = (mean(!! demog, na.rm=TRUE))) %>%
  ggplot(aes(x=avg)) + 
  geom_histogram(aes(y=..density..), color="black", fill="white", bins=20) + 
  geom_density(alpha=.5, fill=color_id) + 
  theme_bw() + 
  xlab(demo_name) + 
  ylab("Density") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(title = paste(demo_name, "in", qual, "Filing Counties"))
}

top_pov <- dense_plot_demos(ecplot, top_lists, "poverty_rate", color_pal[1], "Poverty Rate", "Highest")

1 个答案:

答案 0 :(得分:1)

您好carousallie @ camille,@ ravic_的建议将解决您的问题。要点是正确评估mean(demog, na.rm=TRUE)。对于您编写的代码,可以通过创建一个称为demog的虚拟列来快速修复,该伪列是您的demog变量的副本:

dense_plot_demos <- function(df, id_list, demog, color_id, demo_name, qual) {

  #add this line
  df$demog <- df[,demog]
  df %>%
  filter(GEOID %in% id_list) %>%
  group_by(GEOID) %>%
  mutate(avg_var = mean(demog, na.rm=TRUE)) %>%
  ggplot(aes(x=avg_var)) + 
  geom_histogram(aes(y=..density..), color="black", fill="white", bins=20) + 
  geom_density(alpha=.5, fill=color_id) + 
  theme_bw() + 
  xlab(demo_name) + 
  ylab("Density") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(title = paste(demo_name, "in", qual, "Filing Counties"))
}

#simulate data
set.seed(100)
df <- data.frame(
GEOID = sample(letters,300,replace=TRUE),
poverty_rate = rnorm(300)
)
# some values for your other input
top_ids = letters[1:10]
color_pal = "lightblue"
# plot, this works
# note use "poverty_rate" as demog
top_pov <- dense_plot_demos(df, top_ids, 
"poverty_rate", color_pal[1], "Poverty Rate", "Highest")

以上方法之所以有效,是因为您在数据框中没有称为“ demog”的列(我希望如此),因此您的后续代码功能就像普通的一样。

如果您想真正让它在没有虚拟人的情况下正常工作,则将mutate部分更改为:

mutate(avg_var=eval(parse(text=paste("mean(",demog,",na.rm=TRUE)"))))

most likely does not look like your plot lol