gpt4 book ai didi

r - 如何在一个坐标系中绘制两个图形,结合一列的所有值?

转载 作者:行者123 更新时间:2023-12-04 08:50:39 24 4
gpt4 key购买 nike

我想要一个坐标系,但问题是:
我在数据集中拥有来自我所有状态的所有图表,颜色正确,并且仅根据派对在图例中,但仍然是 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()
enter image description here

关于r - 如何在一个坐标系中绘制两个图形,结合一列的所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64115665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com