绘制 R 中因子的时间序列数据列表
Plot lists of time series data for factors in R
我有一系列描述事件持续时间(以天为单位)的列表,我想将这些数据绘制成线条以比较这些列表。
下面是一些示例数据,说明在学校的哪些日子提供哪些午餐选项。我已经解析了我的数据,这是简化的形式。本来是复杂字符串的形式。
soup = c(15:18)
grilledcheese = c(0:19)
pasta = c(3:13)
我想创建一个与此类似的图表,x 轴为天数,y 轴为 soup
、grilled cheese
和 pasta
:
我在网上看了看,但不确定为此使用哪种图表。部分困难在于数据不是从 0 开始的,y 轴应该代表因子。
我尝试了什么:
我尝试在 ggplot 中绘制它,但它只需要数据帧。我想知道是否有办法直接从列表中绘制。似乎这里应该有一个简单的解决方案,也许我错过了。
我也试过这个
plot(x = grilledcheese, y = rep(1, length(grilledcheese)))
这更接近我想要的,但我不确定如何在 y 轴上绘制多个因素。
首先,让我们以更易于使用 ggplot2 处理的形式处理数据:
library(tidyverse)
soup = c(15:18)
grilledcheese = c(0:19)
pasta = c(3:13)
df <- data.frame(soup_min = c(min(soup),max(soup)),
grilledcheese = c(min(grilledcheese),max(grilledcheese)),
pasta = c(min(pasta),max(pasta)))
df <- pivot_longer(df, cols = 1:3) %>%
group_by(name) %>%
mutate(minv = min(value),
maxv = max(value)) %>%
ungroup() %>%
select(-value) %>%
distinct()
数据
# A tibble: 3 x 3
name minv maxv
<chr> <int> <int>
1 soup_min 15 18
2 grilledcheese 0 19
3 pasta 3 13
图表
然后我们可以绘制您想要的不同元素:每条线的起点和终点、线本身和轴主题。
ggplot(df) +
geom_segment(aes(x = minv, xend = maxv, y = name, yend = name)) +
geom_point(aes(x = minv, y = name)) +
geom_point(aes(x = maxv, y = name)) +
scale_x_continuous(breaks = c(0:20),
labels = c(0:20),
limits = c(0,20),
expand = c(0,0)) +
theme(axis.ticks.x = element_line(size = 1),
axis.ticks.y = element_blank(),
axis.ticks.length =unit(.25, "cm"),
axis.line.x = element_line(size = 1),
panel.background = element_blank()) +
labs(x = "",
y = "")
我们得到这个:
这应该可以解决问题。
额外定制
现在,如果您想在刻度线之间添加刻度线标签,您可能需要检查 here,因为您将不得不重塑数据,并在完成所有操作后完成图表你想要的食物类型。直到,我只是在标签中添加间距:
ggplot(df) +
geom_segment(aes(x = minv, xend = maxv, y = name, yend = name)) +
geom_point(aes(x = minv, y = name)) +
geom_point(aes(x = maxv, y = name)) +
scale_x_continuous(breaks = c(0:20),
labels = paste(" ",0:20),
limits = c(0,20),
expand = c(0,0)) +
theme(axis.ticks.x = element_line(size = 1),
axis.ticks.y = element_blank(),
axis.ticks.length =unit(.25, "cm"),
axis.line.x = element_line(size = 1),
panel.background = element_blank()) +
labs(x = "",
y = "")
您首先需要将数据设计成数据框。你可以这样做,例如
soup = c(15:18)
grilledcheese = c(0:19)
pasta = c(3:13)
## make dataframe
library(tidyverse)
my_x_axis <- as_tibble(seq(0,20))
names(my_x_axis) <- 'x'
my_x_axis %>% mutate(soup_y = 1*ifelse(as.numeric(x %in% soup) == 1, 1, NA)) %>%
mutate(grilledcheese_y = 2*ifelse(as.numeric(x %in% grilledcheese) == 1, 1, NA)) %>%
mutate(pasta_y = 3*ifelse(as.numeric(x %in% pasta) == 1, 1, NA)) -> data
在这里,我使用的知识是您的 x 轴值在 0 到 20 之间。您也可以通过以下方式选择它们,例如通过 min(c(soup,grilledcheese,pasta))
和 min(c(soup,grilledcheese,pasta))
或其他一些逻辑。
根据 的想法,我将三种食物的 y 轴位置硬编码为 1、2 和 3。
ggplot 命令读作:
library(ggplot2)
ggplot() +
geom_line(data=data, aes(x = x, y=soup_y)) +
geom_line(data=data, aes(x = x, y=grilledcheese_y)) +
geom_line(data=data, aes(x = x, y=pasta_y)) +
scale_y_discrete(labels = NULL, breaks = NULL) + labs(y = "") + ## drop y axis labels
scale_x_continuous(labels=seq(0,20,1), breaks=seq(0,20,1)) + # x axis tick marks
geom_text(aes(label = c('soup','grilledcheese','pasta'), x = 0, y = c(1,2,3), vjust = -.2,hjust=-.3)) # add labels
我有一系列描述事件持续时间(以天为单位)的列表,我想将这些数据绘制成线条以比较这些列表。
下面是一些示例数据,说明在学校的哪些日子提供哪些午餐选项。我已经解析了我的数据,这是简化的形式。本来是复杂字符串的形式。
soup = c(15:18)
grilledcheese = c(0:19)
pasta = c(3:13)
我想创建一个与此类似的图表,x 轴为天数,y 轴为 soup
、grilled cheese
和 pasta
:
我在网上看了看,但不确定为此使用哪种图表。部分困难在于数据不是从 0 开始的,y 轴应该代表因子。
我尝试了什么:
我尝试在 ggplot 中绘制它,但它只需要数据帧。我想知道是否有办法直接从列表中绘制。似乎这里应该有一个简单的解决方案,也许我错过了。
我也试过这个
plot(x = grilledcheese, y = rep(1, length(grilledcheese)))
这更接近我想要的,但我不确定如何在 y 轴上绘制多个因素。
首先,让我们以更易于使用 ggplot2 处理的形式处理数据:
library(tidyverse)
soup = c(15:18)
grilledcheese = c(0:19)
pasta = c(3:13)
df <- data.frame(soup_min = c(min(soup),max(soup)),
grilledcheese = c(min(grilledcheese),max(grilledcheese)),
pasta = c(min(pasta),max(pasta)))
df <- pivot_longer(df, cols = 1:3) %>%
group_by(name) %>%
mutate(minv = min(value),
maxv = max(value)) %>%
ungroup() %>%
select(-value) %>%
distinct()
数据
# A tibble: 3 x 3
name minv maxv
<chr> <int> <int>
1 soup_min 15 18
2 grilledcheese 0 19
3 pasta 3 13
图表
然后我们可以绘制您想要的不同元素:每条线的起点和终点、线本身和轴主题。
ggplot(df) +
geom_segment(aes(x = minv, xend = maxv, y = name, yend = name)) +
geom_point(aes(x = minv, y = name)) +
geom_point(aes(x = maxv, y = name)) +
scale_x_continuous(breaks = c(0:20),
labels = c(0:20),
limits = c(0,20),
expand = c(0,0)) +
theme(axis.ticks.x = element_line(size = 1),
axis.ticks.y = element_blank(),
axis.ticks.length =unit(.25, "cm"),
axis.line.x = element_line(size = 1),
panel.background = element_blank()) +
labs(x = "",
y = "")
我们得到这个:
这应该可以解决问题。
额外定制
现在,如果您想在刻度线之间添加刻度线标签,您可能需要检查 here,因为您将不得不重塑数据,并在完成所有操作后完成图表你想要的食物类型。直到,我只是在标签中添加间距:
ggplot(df) +
geom_segment(aes(x = minv, xend = maxv, y = name, yend = name)) +
geom_point(aes(x = minv, y = name)) +
geom_point(aes(x = maxv, y = name)) +
scale_x_continuous(breaks = c(0:20),
labels = paste(" ",0:20),
limits = c(0,20),
expand = c(0,0)) +
theme(axis.ticks.x = element_line(size = 1),
axis.ticks.y = element_blank(),
axis.ticks.length =unit(.25, "cm"),
axis.line.x = element_line(size = 1),
panel.background = element_blank()) +
labs(x = "",
y = "")
您首先需要将数据设计成数据框。你可以这样做,例如
soup = c(15:18)
grilledcheese = c(0:19)
pasta = c(3:13)
## make dataframe
library(tidyverse)
my_x_axis <- as_tibble(seq(0,20))
names(my_x_axis) <- 'x'
my_x_axis %>% mutate(soup_y = 1*ifelse(as.numeric(x %in% soup) == 1, 1, NA)) %>%
mutate(grilledcheese_y = 2*ifelse(as.numeric(x %in% grilledcheese) == 1, 1, NA)) %>%
mutate(pasta_y = 3*ifelse(as.numeric(x %in% pasta) == 1, 1, NA)) -> data
在这里,我使用的知识是您的 x 轴值在 0 到 20 之间。您也可以通过以下方式选择它们,例如通过 min(c(soup,grilledcheese,pasta))
和 min(c(soup,grilledcheese,pasta))
或其他一些逻辑。
根据
ggplot 命令读作:
library(ggplot2)
ggplot() +
geom_line(data=data, aes(x = x, y=soup_y)) +
geom_line(data=data, aes(x = x, y=grilledcheese_y)) +
geom_line(data=data, aes(x = x, y=pasta_y)) +
scale_y_discrete(labels = NULL, breaks = NULL) + labs(y = "") + ## drop y axis labels
scale_x_continuous(labels=seq(0,20,1), breaks=seq(0,20,1)) + # x axis tick marks
geom_text(aes(label = c('soup','grilledcheese','pasta'), x = 0, y = c(1,2,3), vjust = -.2,hjust=-.3)) # add labels