失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 7 时间图

7 时间图

时间:2022-12-19 18:32:12

相关推荐

7 时间图

第7章 时间图

时间图是用于显示随着时间变化的强大工具。最常见的时间图是时间序列图,其他选项包括哑铃图和斜率图。

7.1 时间序列

时间序列是在连续时间点获得的一组定量值。时间点(例如,小时,天,周,月或年)之间的间隔通常相等。

考虑ggplot2包中的Economics time series。它包含从1967年1月到1月收集的美国月度经济数据。让我们绘制个人储蓄率(psavert)。我们可以用简单的线图来做到这一点。

library(ggplot2)library(tidyverse)ggplot(economics, aes(x = date, y = psavert)) +geom_line() +labs(title = "Personal Savings Rate",x = "Date",y = "Personal Savings Rate")

图7.1:简单的时间序列

scale_x_date功能可用于重新格式化日期。在下图中,刻度线每5年出现一次,日期以MMM-YY格式显示。此外,时间序列线呈现红色并变得更厚,添加趋势线标题,简化主题

library(ggplot2)library(scales)ggplot(economics, aes(x = date, y = psavert)) +geom_line(color = "indianred3", size=1 ) +geom_smooth() +scale_x_date(date_breaks = '5 years', labels = date_format("%b-%y")) +labs(title = "Personal Savings Rate",subtitle = "1967 to ",x = "",y = "Personal Savings Rate") +theme_minimal()

图7.2:带有修改日期轴的简单时间序列

绘制时间序列时,请确保日期变量是类date而不是类character。有关详细信息,

请参阅日期值。

让我们用多变量时间序列(多个系列)结束本节。我们将比较1月1日至7月31日AppleFacebook的收盘价。

# multivariate time series# one time install# install.packages("quantmod")library(quantmod)library(dplyr)

# get apple (AAPL) closing pricesapple <- getSymbols("AAPL", return.class = "data.frame", from="-01-01")apple <- AAPL %>% mutate(Date = as.Date(row.names(.))) %>%select(Date, AAPL.Close) %>%rename(Close = AAPL.Close) %>%mutate(Company = "Apple")apple1 <- AAPL %>% rownames_to_column(var = "data") %>% mutate(Date = as.Date(data)) %>% select(Date, AAPL.Close) %>% rename(Close = AAPL.Close) %>% mutate(Company = "Apple")

# get facebook (FB) closing pricesfacebook <- getSymbols("FB", return.class = "data.frame", from="-01-01")facebook <- FB %>% mutate(Date = as.Date(row.names(.))) %>%select(Date, FB.Close) %>%rename(Close = FB.Close) %>%mutate(Company = "Facebook")# combine data for both companiesmseries <- rbind(apple, facebook)

# plot datalibrary(ggplot2)ggplot(mseries, aes(x=Date, y= Close, color=Company)) + geom_line(size=1)ggplot(mseries, aes(x=Date, y= Close, color=Company)) + geom_line(size=1) +scale_x_date(date_breaks = '1 month', labels = scales::date_format("%b")) +scale_y_continuous(limits = c(150, 220), breaks = seq(150, 220, 10),labels = scales::dollar) +labs(title = "NASDAQ Closing Prices",subtitle = "Jan - Aug ",caption = "source: Yahoo Finance",y = "Closing Price") +theme_minimal() +scale_color_brewer(palette = "Dark2")

图7.3:多变量时间序列

你可以看到Facebook在7月底取得的巨大成功。

7.2 Dummbbell 图表

哑铃图可用于显示几个组或观察的两个时间点之间的变化。使用ggalt包中geom_dumbbell功能。

使用gapminder数据集,让我们绘制美洲1952年至预期寿命的变化。数据集采用长格式。我们需要将其转换为宽格式以创建哑铃图

library(ggalt)# load datadata(gapminder, package = "gapminder")# subset dataplotdata_long <- filter(gapminder,continent == "Americas" & year %in% c(1952, )) %>%select(country, year, lifeExp)# convert data to wide formatplotdata_wide <- spread(plotdata_long, year, lifeExp)names(plotdata_wide) <- c("country", "y1952", "y")plotdata_wide# create dumbbell plotggplot(plotdata_wide, aes(y = country,x = y1952,xend = y)) + geom_dumbbell()

图7.4:简单的哑铃图

如果对国家进行排序,并对点进行大小颜色调整,图表将更容易阅读。在下一幅图中,我们将按1952年预期寿命排序,修改线条和点的大小、点的颜色、添加标题标签,并简化主题

# create dumbbell plotggplot(plotdata_wide, aes(y = reorder(country, y1952),x = y1952,xend = y)) + geom_dumbbell(size = 1.2,size_x = 3, size_xend = 3,colour = "grey", colour_x = "blue", colour_xend = "red") +theme_minimal() + labs(title = "Change in Life Expectancy",subtitle = "1952 to ",x = "Life Expectancy (years)",y = "")

图7.5:分类的彩色哑铃图

这里更容易辨别模式。例如,海地开始时的预期寿命在1952年最低,而且仍然是的最低值。Paraguay开始时相对较高。

7.3 斜率图

当有多个组和多个时间点时,斜率图可能会有所帮助。让我们描绘1992年,1997年,2002年和六个中美洲国家的预期寿命。

我们再次使用gapminder数据

要创建斜率图,我们将使用CGPfunctions包中的newggslopegraph函数。

newggslopegraph函数的参数是(按顺序)

数据框时间变量(必须是一个factor)要绘制的数字变量分组变量(每组创建一行)。

library(CGPfunctions)# Select Central American countries data # for 1992, 1997, 2002, and df <- gapminder %>% filter(year %in% c(1992, 1997, 2002, ),country %in% c("Panama", "Costa Rica", "Nicaragua", "Honduras", "El Salvador", "Guatemala","Belize")) %>% mutate(year = factor(year),lifeExp = round(lifeExp))# create slope graphnewggslopegraph(df, year, lifeExp, country) +labs(title="Life Expectancy by Country", subtitle="Central America", caption="source: gapminder")

图7.6:斜率图

在上图中,哥斯达黎加在所研究的年限范围内具有最高的预期寿命。危地马拉是最低的,并在2002年赶上洪都拉斯(也低至69)。

7.4 区域图表

简单的面积图基本上是一个折线图,从线到x轴的填充。

# basic area chartggplot(economics, aes(x = date, y = psavert)) +geom_area(fill="lightblue", color="black") +labs(title = "Personal Savings Rate",x = "Date",y = "Personal Savings Rate")

图7.7:基本面积图

堆积面积图可用于显示组间随时间的差异。考虑gcookbook包中的uspopage数据集。

我们将绘制1900年到2002年美国人口的年龄分布。

# stacked area chartdata(uspopage, package = "gcookbook")uspopage %>% ggplot(aes(Year, Thousands)) +geom_area()ggplot(uspopage, aes(x = Year,y = Thousands, fill = AgeGroup)) +geom_area() +labs(title = "US Population by age",x = "Year",y = "Population in Thousands")

图7.8:堆积面积图

在图表中最好避免使用科学符号。一般读者知道3e+05表示3亿的可能性有多大?在ggplot2中很容易更改比例。

只需将千位变量除以1000并将其报告为百万。

创建黑色边框以突出显示组之间的差异将组的顺序颠倒,以匹配不断增长的年龄改善标签选择不同的配色方案选择一个更简单的主题。

可以使用forcats包中的fct_rev函数逆转AgeGroup变量的级别。

# stacked area chartdata(uspopage, package = "gcookbook")ggplot(uspopage, aes(x = Year,y = Thousands/1000, fill = forcats::fct_rev(AgeGroup))) +geom_area(color = "black") +labs(title = "US Population by age",subtitle = "1900 to 2002",caption = "source: U.S. Census Bureau, , HS-3",x = "Year",y = "Population in Millions",fill = "Age Group") +scale_fill_brewer(palette = "Set2") +theme_minimal()

图7.9:具有更简单比例的堆积面积图

显然,在过去的100年里,幼儿的数量并没有太大变化。

当对(1) 组随时间的变化和(2) 总体随时间的变化都感兴趣时,堆叠区域图是最有用的。把最重要的组放在底部。在这种类型的图中,这些是最容易解释的。

如果觉得《7 时间图》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。