作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个坐标系,但问题是:
我在数据集中拥有来自我所有状态的所有图表,颜色正确,并且仅根据派对在图例中,但仍然是 50 个图表。
我只想要两个——一个结合所有共和州,一个结合所有民主国家。
corona_usa_simple <- readr::read_csv("https://raw.githubusercontent.com/datasets/covid-19/master/data/us_simplified.csv")
corona_us_states <- corona_usa_simple %>%
select(- FIPS, -`Country/Region`) %>% rename(State=`Province/State`)
us_states_population <- corona_us_states %>%
group_by(State, Date) %>%
summarise(totalPopulation = sum(Population),
totalConfirmed = sum(Confirmed),
totalDeaths = sum(Deaths))
wiki_governors <- htmltab("https://en.wikipedia.org/wiki/List_of_United_States_governors",
header = 2) %>% slice(-1)
wiki_governors[23,3] <- "Democratic" #Democratic-Farmer-Labor to Democratic
corona_party_states <- left_join(wiki_governors, us_states_population, by= "State") %>%
select(- Governor, - Born, - `Prior public experience`, - Inauguration, - `End of term`, - `Past governors`)
corona_party_states <- transform(corona_party_states, CasesPop = totalConfirmed*100000/totalPopulation) %>%
rename(state = State)
## Plot for the graphs
ggplot(data = corona_party_states) +
geom_point(aes(x = Date, y = CasesPop, color = Party)) +
scale_color_manual(values=c("Democratic" = "blue", "Republican" = "red"))+
ggtitle("COVID-19 Cases per Capita by Party")+
ylab("Cases/100K")+
xlab("Timeline")+
theme_bw()
到目前为止,这是我的代码。我将不胜感激任何帮助!
corona_party_cases <- corona_party_states %>%
group_by(Party, Date) %>% select(-state) %>% summarise(CasesPop)
最佳答案
如果我理解正确,你的情节上需要两行;共和党各州每 100,000 例病例一条线,民主党各州每 100,000 例病例一条线。在这种情况下,您需要做更多的工作来清理和汇总数据。绘图代码本身很好。
corona_party_states %>%
select(-Party) %>%
rename(Party = Party.1) %>%
mutate(Party = substr(Party, 1, 10)) %>%
group_by(Date, Party) %>%
summarise(total_pop = sum(totalPopulation),
total_confirmed = sum(totalConfirmed),
CasesPop = 100000 * total_confirmed/total_pop) %>%
ggplot() +
geom_line(aes(x = Date, y = CasesPop, color = Party), size = 2) +
scale_color_manual(values = c(Democratic = "blue", Republican = "red")) +
ggtitle("COVID-19 Cases per Capita by Party")+
ylab("Cases/100K")+
xlab("Timeline")+
theme_bw()
关于r - 如何在一个坐标系中绘制两个图形,结合一列的所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64115665/
我是一名优秀的程序员,十分优秀!