【问题标题】:Plot Percentages in R with different basic totality绘制具有不同基本总体的 R 中的百分比
【发布时间】:2019-11-26 10:25:58
【问题描述】:

我想用 ggplot2 包直观地比较 R 中美国两个州的交通站点的两个数据集。我将它们组合到一个数据框中,显示每年和州的交通站点总数。由于这些数字非常不同,我想比较每个州的人口百分比。首先,这是一个示例 df 以及我到目前为止所取得的成就。我在代码中使用了tidyverselubridate

df <- data.frame(ID=c("CA-2013-0000001","CA-2014-0000001", "TX-2013-0000001", "TX-2014-0000001"),
                    State=c("CA", "CA", "TX", "TX"),
                    Stop_Date=ymd("2013-01-01","2014-01-01", "2013-01-01", "2014-01-01"))

df %>%
  group_by(year = year(Stop_Date), state = State) %>%
  count() %>%
  ggplot(aes(year, n, col = state))+
  geom_point(stat = "identity")+
  geom_line(stat = "identity")

使用该代码,我得到了一个有两条线的图,每条线都反映了我看到的两种状态。

我想创建完全相同的图,但我想显示与州人口相关的百分比,而不是总数,即population_ca &lt;- 38620000population_tx &lt;- 26980000

我尝试了这两种方法,但每次运行代码时都会返回不同的错误:

df %>%
  group_by(year = year(Stop_Date), state = State) %>%
  summarise(PercentStopsToPopulation = if_else(state == "CA",
                                                 ((n()/population_ca)*100),
                                                 ((n()/population_tx)*100))) %>%
  ggplot(aes(year, PercentStopsToPopulation, col = state))+
  geom_point(stat = "identity")+
  geom_line(stat = "identity")

df %>%
  group_by(year = year(Stop_Date), state = State) %>%
  summarise(PercentCA = ifelse(state == "CA",((n()/population_ca)*100)),
            PercentTX = ifelse(state == "TX", ((n()/population_tx)*100))) %>%
  ggplot(aes(year, PercentCA))+
  geom_point(stat = "identity")+
  geom_line(stat = "identity")+
  geom_point(aes(year, PercentTX), stat = "identity")+
  geom_line(aes(year, PercentTX), stat = "identity")

我真的希望有人可以帮助我并告诉我我的错误在哪里。 提前谢谢!

【问题讨论】:

  • 能否请您再次检查您的数据?我想知道它是否可能遗漏了什么。

标签: r ggplot2


【解决方案1】:

以下是我解决此问题的方法。我使用 tidyverse,所以你会注意到一些变化。

library("tidyverse")

#keep organized and avoid for loops by organizing population data in a tibble
pop <- tibble(state = c("CA", "TX"),
              population = c(38620000, 26980000))

#I made this a tibble instead of a dataframe, just to stay consistent in the tidyverse approach.
df <- tibble(ID=c("CA-2013-0000001","CA-2014-0000001", "TX-2013-0000001", "TX-2014-0000001"),
                 state=c("CA", "CA", "TX", "TX"),
                 stop_date=ymd("2013-01-01","2014-01-01", "2013-01-01", "2014-01-01")) %>%
  #I prefer to err on the side of making more fields, to make it easier to see what we're doing down the road.
  mutate(year = year(stop_date))

# summarise data
df_count <- df %>%
  group_by(year, state) %>%
  count() %>%
  #Join with population table. I prefer this over a for loop - easier to scale up, in case you decide to add more states.
  full_join(pop) %>%
  # Calculate the percent of population
  mutate(percent = 100*n/population)


#Now, we graph!
df_count %>%
  ggplot(aes(year, percent, col = state))+
  geom_point()+
  geom_line()

如果您有任何问题,请告诉我。 :)

【讨论】:

  • 很少有新用户给出如此好的、透彻、解释清楚的答案。我看到你已经活跃了几个月,但让我说欢迎来到这个网站:)
  • 非常感谢您的快速答复。 :) 我对 r 很陌生,因此犯了很多不必要的错误。你解决了我已经工作了两天的问题。所以,再次,非常感谢你。 :)
  • 谢谢,@Gregor!我很高兴来到这里。我很高兴这有帮助,@Articweasel。 :)
猜你喜欢
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
  • 2020-11-18
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
相关资源
最近更新 更多