如何使用 R 绘制这样的图形
How to plot graph like this using R
我什至不知道这个图的名称以及制作它需要什么样的数据,所以很抱歉我真的没有最小的可重现样本。 x轴是时间,y轴是所有变量,在每个时期每个变量都可以选择或不选择,用蓝色表示。
此图来自 Freyberger、Joachim、Andreas Neuhierl 和 Michael Weber。 “非参数地剖析特征。”金融研究评论 33.5 (2020): 2326-2377.
您可以将其构建为热图,其中的值只能为 0 或 1。以下是如何使用 ggplot 实现此目的的示例(来自 here):
ggplot(df1, aes(variable, indv)) + geom_tile(aes(fill = value),
colour = "white")
你可以使用 ggplot
.
首先我们将创建一些合理的样本数据:
set.seed(1)
df <- data.frame(on_off = c(replicate(26, cumsum(rnorm(20)))) > 3,
year = rep(2001:2020, 26),
factor = rep(LETTERS, each = 20))
绘图代码是这样的:
library(ggplot2)
ggplot(df, aes(year, factor, fill = on_off)) +
geom_tile() +
scale_fill_manual(values = c('TRUE' = 'blue2', 'FALSE' = 'white')) +
theme_classic() +
theme(legend.position = 'none')
我什至不知道这个图的名称以及制作它需要什么样的数据,所以很抱歉我真的没有最小的可重现样本。 x轴是时间,y轴是所有变量,在每个时期每个变量都可以选择或不选择,用蓝色表示。
此图来自 Freyberger、Joachim、Andreas Neuhierl 和 Michael Weber。 “非参数地剖析特征。”金融研究评论 33.5 (2020): 2326-2377.
您可以将其构建为热图,其中的值只能为 0 或 1。以下是如何使用 ggplot 实现此目的的示例(来自 here):
ggplot(df1, aes(variable, indv)) + geom_tile(aes(fill = value),
colour = "white")
你可以使用 ggplot
.
首先我们将创建一些合理的样本数据:
set.seed(1)
df <- data.frame(on_off = c(replicate(26, cumsum(rnorm(20)))) > 3,
year = rep(2001:2020, 26),
factor = rep(LETTERS, each = 20))
绘图代码是这样的:
library(ggplot2)
ggplot(df, aes(year, factor, fill = on_off)) +
geom_tile() +
scale_fill_manual(values = c('TRUE' = 'blue2', 'FALSE' = 'white')) +
theme_classic() +
theme(legend.position = 'none')