0

我一直在使用一些 tidycensus 数据进行分配,并且已经到了尝试生成平滑线图并没有绘制我的数据集的地步。

我目前的代码是:

PA_county_list %>%
  filter(county %in% c("Chester County","Bucks County")) %>%
  ggplot() +
  geom_smooth(mapping = aes (x = total.pop , y = mean.white, color = county)) +
  labs(title = "Comparing Percent White Race in Chester County and Buck County",
       subtitle = "2010 ACS 5 year census survey",
       y = "White Race Claims",
       x = "Total Population")


这是我正在使用的数据示例:

county            total.pop    mean.white            mean.income        per_white
<chr>               <dbl>          <dbl>                 <dbl>             <dbl>
Chester County      41413         3694.957             88997.22           3.716587

Bucks County        47969         3946.140             79940.48           3.969241 

打印脚本的结果导致标记为空白图。其中标签完好无损,但未列出来自total.pop(人口)和mean.white(白人种族)的数据。

在这一点上,任何见解将不胜感激。

谢谢。

4

2 回答 2

0

所以我发现我做错了什么!显然,我为图形生成列出的数据集是计算作业中其他问题的平均值的数据集。它由单次平均观察组成。

所以解决这个问题的方法是回到我最初清理的数据集,并在取平均值之前更改参数以反映旧变量。

于 2021-02-07T00:54:16.820 回答
0

从情节标题来看,您的数据中只有两个点。如果是这种情况,那么您将不会/无法顺利。您可以使用以下方法简单地连接这些点geom_line

ggplot(df, mapping = aes (x = total.pop , y = mean.white)) +
  geom_point(aes(color = county)) +
  geom_line() +
  labs(title = "Comparing Percent White Race in Chester County and Buck County",
       subtitle = "2010 ACS 5 year census survey",
       y = "White Race Claims",
       x = "Total Population")

在此处输入图像描述

如果你有更多的数据点,你可以像这样平滑:

ggplot(df, mapping = aes (x = total.pop , y = mean.white)) +
  geom_smooth(method = "loess", formula = y ~ x, color = "black") +
  geom_point(aes(color = county)) 

在此处输入图像描述


数据

set.seed(1)
df <- data.frame(county = c("Chester", "Bucks", "Berks", "Montgomery", "Delaware", "Schuylkill"),
                 total.pop = rnorm(6, 48000, 3800)) %>% 
  dplyr::mutate(mean.white = rbeta(6, 5, 2) * total.pop)
于 2021-02-07T00:54:58.247 回答