pacman::p_load(
tidyverse,
data.table,
fixest,
sandwich,
modelsummary,
EconGeo,
economiccomplexity,
fplot
)
data(us_pub_econ, package = "fplot")
head(us_pub_econ)
| paper_id | year | institution | journal | jnl_top_25p | jnl_top_5p |
|---|---|---|---|---|---|
| 865948 | 1988 | university of california berkeley | university of san francisco law review | 0 | 0 |
| 2027349 | 1990 | university of wisconsin madison | climatic change | 1 | 0 |
| 2299436 | 1988 | columbia university | marine resource economics | 0 | 0 |
| 2558483 | 1988 | yale university | journal of accounting auditing & finance | 0 | 0 |
| 2672463 | 1989 | university of michigan | administration in social work | 0 | 0 |
| 2860349 | 1985 | university of kansas | administration in social work | 0 | 0 |
每个机构的出版物分布情况
plot_distr(us_pub_econ$institution)
plot_distr(us_pub_econ$institution,col = "red")
根据top5顶刊发文量加权的分布情况(机构发表top5顶刊的数量和占比)
plot_distr(jnl_top_5p ~ institution, us_pub_econ)
同时添加多个权重:同时绘制两种加权(多图拼接)
# 1表示不加权
plot_distr(1 + jnl_top_25p + jnl_top_5p ~ institution, us_pub_econ)
univ.paper <-
us_pub_econ |>
group_by(institution) |>
summarise(sum = length(paper_id)) |>
arrange(desc(sum))
univ <- univ.paper[1:3, 1]
us_pub_econ |>
filter(institution %in% c(univ$institution[1], univ$institution[2], univ$institution[3])) |>
filter(jnl_top_25p == 1) |>
group_by(institution) |>
summarise(sum = length(paper_id)) |>
arrange(desc(sum))
| institution | sum |
|---|---|
| harvard university | 482 |
| university of pennsylvania | 441 |
| university of california berkeley | 372 |
t <- us_pub_econ |>
filter(institution %in% c(univ$institution[1], univ$institution[2], univ$institution[3])) |>
filter(jnl_top_25p == 1)
越是按质量加权,分布就越偏向顶级机构。
现在我们通过询问前 3 个最有生产力的机构在哪些期刊上发表文章来说明条件图。
下图绘制的是三个最有生产力的机构的期刊分布图(本质是气泡图)
plot_distr(~ journal | institution, us_pub_econ) |> view()
# plot_distr(1 ~ journal | institution, us_pub_econ)#等于上式
# plot_distr(jnl_top_5p ~ journal | institution, us_pub_econ,mod.select = 4,total = T)
# 语法: y ~ x|分组,数据
显示每个机构最重要的7种期刊,规模对应于期刊在机构中的份额。可以使用选项 显示总份额,而不是显示组内份额。total = TRUE
plot_distr(~ journal | institution, us_pub_econ, total = T)
plot_distr(~ journal | institution, us_pub_econ, other = T, total = F)
title("TOP3机构的期刊发文量占机构发文总量的比例")
plot_distr(~ journal | institution, us_pub_econ, other = T, total = T)
title("TOP3机构的期刊发文量占总样本的比例")
支持正则表达式: 如果要选择其他调节器值,可以与正则表达式一起使用。让我们来看看马萨诸塞州的三所机构:mod.select
plot_distr(~ journal | institution, us_pub_econ, mod.select = "harvard|boston.+uni|mass.+inst", other = T)
title("麻州三所大学期刊发文量占各自发文量的比值")
默认情况下,拆分数据时,不显示包含“其他”信息的条形以节省空间。但是,当然,我们可以使用适当的选项添加它:
plot_distr(~ journal | institution, us_pub_econ, other = TRUE)
title("TOP3机构期刊占机构发文量的比例")
现在让我们说明函数在存在数值数据时的行为。我们首先导入贸易数据:
data(trade, package = "fixest")
head(trade)
| Destination | Origin | Product | Year | dist_km | Euros |
|---|---|---|---|---|---|
| LU | BE | 1 | 2007 | 139.5719 | 2966697 |
| BE | LU | 1 | 2007 | 139.5719 | 6755030 |
| LU | BE | 2 | 2007 | 139.5719 | 57078782 |
| BE | LU | 2 | 2007 | 139.5719 | 7117406 |
| LU | BE | 3 | 2007 | 139.5719 | 17379821 |
| BE | LU | 3 | 2007 | 139.5719 | 2622254 |
该数据包括不同时期欧洲国家对不同产品的进口量。我们来看看交易量的分布:
plot_distr(trade$Euros, log = T)
plot_distr(trade$Euros, log = F)
上图做了三件主要的事情。首先,数据已事先自动设置为对数形式,以便提供更好的显示。可以使用参数log更改此行为。其次,第一列包含所有低于
1,097
的剩余部分——这允许专注于图表中变化最大的部分。这就是为什么图形不是从 0
开始,而是专注于分布的模式。由于第一列,在确保更高的可读性的同时,不会丢失信息。最后,格式化数字刻度以便于阅读。
现在让我们看看法国和德国出口的分布:
plot_distr(~ Euros | Origin, trade, mod.select = c("DE", "FR"))
title("德国和法国出口额的分布情况")
plot_distr(~ Euros | Destination, trade, mod.select = c("DE", "FR"))
title("德国和法国进口额的分布情况")
我们使用参数来选择两个调节器值。对于每个对数“bin”,mod.select用来设置选择哪些国家。
现在,让我们看一下使用该数据集的另一组数值数据。iris
plot_distr(iris$Petal.Length) # 没有取对数
对于“分布良好”的连续数据,它看起来很像一个规则的直方图,但更漂亮一些。请注意,bin 大小始终是“离散的”以便于阅读(但实际上是连续的)。现在我们还可以按物种拆分数据:
plot_distr(~ Petal.Length | Species, iris)
title("按物种拆分数据")
箱子的全宽以虚线报告,这样用户就不会将总箱子的大小与单个物种的箱子大小混淆。
除了在同一轴上显示三个物种,我们还可以选择拆分数据:
plot_distr(~ Petal.Length | Species, iris, mod.method = "split")
title("三种物种的花瓣长度分布")
显示不同对象的数据的另一种方法是堆叠他们的值。让我们看看鸢尾属植物的萼片长度分布:
plot_distr(~ Sepal.Length | Species, iris, mod.method = "stack")
要显示累积分布,只需使用参数 。再看看前5%的发表的累计分布: cumul = TRUE
plot_distr(jnl_top_5p ~ institution, us_pub_econ, cumul = TRUE)
上图仅 10 家机构(粗黑线每五个值出现一次)就占所有前 5% 出版物的 33%。
您还可以使用函数显示某些变量的聚合演变。默认情况下,应用的函数是平均值,但可以随意修改。让我们看看在前 5% 的期刊中发表的论文比例的演变: plot_lines plot_lines
plot_lines(jnl_top_5p ~ year, us_pub_econ)
title("TOP5期刊平均年度发文量趋势")
数据首先在年度级别汇总,即应用于每年,然后绘制一个简单的散点图。
和前面的方法一样, 使图表有条件是很容易的。让我们看看三个顶级机构的演变:
让我们找出在论文发表方面排名前 3(最多5) 的机构, 发表TOP5期刊的趋势,我们使用 plot_distr 而不绘制:
info <- plot_distr(us_pub_econ$institution, plot = F) # 获取发文量最大的机构排名
top3_instit <- head(info$x, 3) # 获取前3的机构
top5_instit <- head(info$x, 5) # 最多可以是TOP5机构
plot_lines(
jnl_top_5p ~ year | institution,
us_pub_econ[institution %in% top5_instit]
)
title("发文总量TOP5的机构发表TOP5顶刊的占比趋势")
这个案例也说明fplot生成的图形是可以获得原始数据的。
如果我们想显示每年频率的条件演变,我们可以使用 1 作为变量,这会将默认函数设置为以获取频率: sum
plot_lines(1 ~ year | institution, us_pub_econ[institution %in% top5_instit])
title("发文量TOP5机构发文频次变化趋势")
可以使用以下选项添加一些简单的平滑: smoothing_window
plot_lines(1 ~ year | institution, us_pub_econ[institution %in% top5_instit],
smoothing_window = 1
)
平滑使用第 n
期未来和过去的年份来平滑数据。
该函数用于绘制条件箱线图:一个变量有条件地分配给一个或两个调节器。以发表论文的数据为例。首先,我们创建每个机构每年前 5% 期刊的出版物数量,然后我们显示年度箱线图: plot_box
base_pub <- us_pub_econ[, .(nb_pub_top_5 = sum(jnl_top_5p)),
by = .(year, institution)
]
# 等价于:
# base_pub <- us_pub_econ %>%
# group_by(year, institution) %>% # 按年份和机构分组
# summarise(nb_pub_top_5 = sum(jnl_top_5p)) # 计算每组的顶级期刊发表数
plot_box(nb_pub_top_5 ~ year, base_pub)
title("机构在TOP5期刊每年度发文量箱线图")
默认情况下,当箱线图由于离群值的规模而消失时,不显示离群值。但是,当异常值不显示时,会显示均值,此处用红色菱形表示。我们可以看到,虽然第三个四分位数在此期间几乎翻了一番,但平均值几乎翻了三倍,这意味着异常值随着时间的推移而增加。
重做相同的分析,但将排名前 5 的机构与其他机构区分开来:
base_pub[, isTop5 := institution %in% top5_instit]
plot_box(nb_pub_top_5 ~ year | isTop5, base_pub)
title("排名前5的机构与其他机构相比,顶刊发文量变化")
绿色代表发文量TOP5的机构,每年能在顶刊发文的平均数、中位数、两个分位数
现在报告异常值,为了避免这种行为,我们可以使用参数: outlier = FALSE
plot_box(nb_pub_top_5 ~ year | isTop5, base_pub, outlier = FALSE)
也可以一次绘制多个变量。让我们举一个使用鸢尾花数据的例子:
plot_box(Petal.Width ~ Species, iris)
plot_box(. ~ Species, iris) # `.`代表全部数据
plot_box(. ~ Species, iris, outlier = FALSE) # `.`代表全部数据
set.seed(10)
收入 <- sample(x = 20, replace = T)
支出 <- sample(x = 20, replace = T)
个体 <- sample(x = letters[1:5], size = 20, replace = T)
地点 <- sample(x = LETTERS[1:3], size = 20, replace = T)
性别 <- sample(c(0, 1), size = 20, replace = TRUE)
城乡 <- sample(c(0, 1), size = 20, replace = TRUE)
df <- data.frame(个体, 地点, 收入, 支出, 性别, 城乡)
df
| 个体 | 地点 | 收入 | 支出 | 性别 | 城乡 |
|---|---|---|---|---|---|
| a | C | 11 | 18 | 0 | 0 |
| e | C | 9 | 18 | 0 | 0 |
| b | C | 10 | 13 | 1 | 1 |
| a | B | 16 | 5 | 1 | 1 |
| a | A | 12 | 1 | 0 | 0 |
| b | B | 8 | 7 | 1 | 0 |
| e | C | 7 | 18 | 0 | 0 |
| d | B | 19 | 4 | 0 | 0 |
| c | B | 15 | 18 | 0 | 1 |
| a | B | 15 | 19 | 0 | 1 |
| c | B | 10 | 18 | 0 | 0 |
| b | C | 7 | 11 | 1 | 0 |
| b | A | 10 | 15 | 0 | 1 |
| b | C | 2 | 1 | 1 | 1 |
| b | A | 13 | 10 | 1 | 0 |
| e | A | 8 | 10 | 1 | 1 |
| c | B | 14 | 14 | 1 | 1 |
| e | B | 7 | 13 | 0 | 0 |
| e | B | 6 | 1 | 0 | 0 |
| e | B | 7 | 9 | 1 | 1 |
plot_distr(df$个体)
title("个体频次")
plot_distr(df$地点)
title("地点频次")
plot_distr(收入 ~ 个体, df)
title("个体收入")
plot_distr(支出 + 收入 ~ 个体, df)
title("支出")
plot_distr(支出 ~ 个体 | 地点, df)
title("TOP3个体的地点和支出-组内份额占比")
plot_distr(支出 ~ 个体 | 地点, df, total = T)
title("TOP3个体的地点和支出-总份额占比")
plot_distr(收入 ~ 地点 | 个体, df, mod.select = 5)
title("个体在不同地点的支出")
plot_distr(性别 ~ 个体, df)
title("个体会见男性占比")
plot_distr(收入 + 城乡 ~ 个体, df)
title("个体去的城市及其个人收益占比")
df <- bruceR::import("G:/PatenticsClient64/Documents/Patentics/Txt/管道-管廊.xlsx") |>
mutate(法律状态2 = str_replace_all(法律状态,c("有效"="efficient","公开"="publication","驳回"="turndown")))
√ Successfully imported: 97 obs. of 11 variables
plot_distr(~法律状态2, df)
plot_distr(~ 国际主分类 | 法律状态2, df, mod.select = 5, total = T, other = T)
# janitor::clean_names(df) |> names()
plot_distr( ~ year(申请日)|法律状态2, df)
base_pub <- us_pub_econ[, .(nb_pub_top_5 = sum(jnl_top_5p)),
by = .(year, institution)
]
# 等价于:
# base_pub <- us_pub_econ %>%
# group_by(year, institution) %>% # 按年份和机构分组
# summarise(nb_pub_top_5 = sum(jnl_top_5p)) # 计算每组的顶级期刊发表数
plot_box(nb_pub_top_5 ~ year, base_pub)
title("机构在TOP5期刊每年度发文量箱线图")
year(df$优先权日)
[1] 2009 NA 2013 NA 2014 NA 2014 2015 2015 NA 2015 NA 2015 2015 NA
[16] NA 2015 2015 NA NA 2016 2016 NA 2016 NA 2016 NA NA NA NA
[31] NA NA 2016 NA NA NA NA NA NA NA NA NA NA NA NA
[46] NA NA 2016 2016 2016 2016 2016 2016 2016 NA 2016 2016 2016 NA NA
[61] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[76] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[91] NA NA NA NA NA NA NA
plot_box(year(公开日)~法律状态, df)
ggplot(df,aes(法律状态))+
geom_boxplot()
ggplot(mpg,aes(class,hwy))+
geom_boxplot()
ggplot(mpg,aes(class))+
geom_boxplot()
df$法律状态2
[1] "efficient" "efficient" "efficient" "efficient" "efficient"
[6] "efficient" "efficient" "efficient" "efficient" "efficient"
[11] "efficient" "efficient" "efficient" "efficient" "efficient"
[16] "efficient" "efficient" "efficient" "efficient" "efficient"
[21] "efficient" "efficient" "efficient" "efficient" "efficient"
[26] "efficient" "efficient" "efficient" "efficient" "efficient"
[31] "efficient" "efficient" "efficient" "publication" "efficient"
[36] "efficient" "efficient" "efficient" "efficient" "efficient"
[41] "efficient" "efficient" "efficient" "efficient" "efficient"
[46] "efficient" "efficient" "efficient" "efficient" "efficient"
[51] "efficient" "efficient" "turndown" "turndown" "efficient"
[56] "efficient" "efficient" "efficient" "publication" "efficient"
[61] "efficient" "efficient" "efficient" "publication" "publication"
[66] "publication" "publication" "efficient" "efficient" "efficient"
[71] "efficient" "efficient" "publication" "publication" "publication"
[76] "efficient" "efficient" "efficient" "efficient" "efficient"
[81] "publication" "efficient" "publication" "efficient" "publication"
[86] "efficient" "publication" "publication" "publication" "efficient"
[91] "efficient" "efficient" "efficient" "publication" "efficient"
[96] "publication" "publication"
# 加载 mpg 数据集
# 绘制箱线图
ggplot(mpg, aes(x = class, y = hwy)) +
geom_boxplot() +
stat_summary(fun.y = "mean", geom = "point", shape=20, size=3, colour="red", fill="white") +
ggtitle("mpg数据集中车辆类型和高速公路里程的关系")+
cowplot::theme_minimal_hgrid()
plot_box(hwy~class,mpg,addMean = T,inCol = "white")
title("mpg数据集中车辆类型和高速公路里程的关系")