具有重叠的 Ggplot (geom_line)
Ggplot (geom_line) with overlaps
我想要一个让重叠非常清晰的情节。更具体地说,我想绘制给定个人的工作地点。因为个人可能同时在不同的工作场所工作,所以我会为每个工作场所绘制一个geom_line。我的问题是:有重叠的时候怎么说清楚?我尝试在 geom_line() 中使用一些透明度,但我正在寻找使重叠突出的东西
您可以在下面看到一个包含 3 个人和 2 个工作场所的简单示例。个人 A 从工作场所 1 切换到工作场所 2,个人 B 也这样做,但中间有一段时间失业,个人 C 在短时间内在两个地方工作(这是我看到重叠的地方)。
# individual A
a_id <- c(rep('A',25))
a_period <- c(seq(1, 13), seq(13,24))
a_workplace <-c(rep(1,13), rep(2,12))
# individual B
b_id <- c(rep('B',19))
b_period <- c(seq(2,8), seq(13,24))
b_workplace <-c(rep(1,7), rep(2,12))
# individual C
c_id <- c(rep('C',9))
c_period <- c(seq(1,4), seq(2,6))
c_workplace <-c(rep(1,4), rep(2,5))
# final affiliation data
id <- c(a_id, b_id, c_id)
period <- c(a_period, b_period, c_period)
workplace <- c(a_workplace, b_workplace, c_workplace)
mydata <- data.frame(id, period, workplace)
# affiliation data by workplace
mydata_1 <- mydata %>%
filter(workplace==1) %>%
mutate(workplace=as.factor(workplace))
mydata_2 <- mydata %>%
filter(workplace==2) %>%
mutate(workplace=as.factor(workplace))
我尝试了下面的方法,但仍然想让重叠部分更清晰。也许建议颜色组合使重叠更清晰?
ggplot(mydata_1, aes(period, id, group=id, col=workplace)) +
geom_line(alpha=0.4) +
geom_line(data=mydata_2, alpha=0.4, aes(period, id, group=id, col=workplace)) +
labs(x="time", y=NULL, title="Work affiliation") +
scale_x_continuous(breaks = seq(0,24, by=2)) +
scale_y_discrete(limits=rev) +
scale_color_manual(values=c("dodgerblue","firebrick1")) +
theme(legend.position = c(.7, .92), legend.direction = "horizontal",
legend.background = element_rect(linetype="solid", colour ="black"),
panel.background = element_rect(fill = "grey97"))
我也不明白为什么我在图例中看不到正确的职场颜色
我想我会合并数据集,然后用 position_dodge
进行单个 geom_line
调用。这会简化您的代码、显示重叠并正确显示您的图例。
all_data <- rbind(mydata_1, mydata_2)
ggplot(all_data, aes(x = id, y = period, color = workplace)) +
geom_line(position = position_dodge(width = 0.1), size = 2) +
labs(y = "time", title = "Work affiliation") +
scale_y_continuous(breaks = seq(0, 24, by = 2)) +
scale_x_discrete(limits = rev) +
scale_color_manual(values = c("dodgerblue", "firebrick1")) +
coord_flip() +
theme(legend.position = c(.7, .92),
legend.direction = "horizontal",
legend.background = element_rect(linetype = "solid", colour = "black"),
panel.background = element_rect(fill = "grey97"))
我想要一个让重叠非常清晰的情节。更具体地说,我想绘制给定个人的工作地点。因为个人可能同时在不同的工作场所工作,所以我会为每个工作场所绘制一个geom_line。我的问题是:有重叠的时候怎么说清楚?我尝试在 geom_line() 中使用一些透明度,但我正在寻找使重叠突出的东西
您可以在下面看到一个包含 3 个人和 2 个工作场所的简单示例。个人 A 从工作场所 1 切换到工作场所 2,个人 B 也这样做,但中间有一段时间失业,个人 C 在短时间内在两个地方工作(这是我看到重叠的地方)。
# individual A
a_id <- c(rep('A',25))
a_period <- c(seq(1, 13), seq(13,24))
a_workplace <-c(rep(1,13), rep(2,12))
# individual B
b_id <- c(rep('B',19))
b_period <- c(seq(2,8), seq(13,24))
b_workplace <-c(rep(1,7), rep(2,12))
# individual C
c_id <- c(rep('C',9))
c_period <- c(seq(1,4), seq(2,6))
c_workplace <-c(rep(1,4), rep(2,5))
# final affiliation data
id <- c(a_id, b_id, c_id)
period <- c(a_period, b_period, c_period)
workplace <- c(a_workplace, b_workplace, c_workplace)
mydata <- data.frame(id, period, workplace)
# affiliation data by workplace
mydata_1 <- mydata %>%
filter(workplace==1) %>%
mutate(workplace=as.factor(workplace))
mydata_2 <- mydata %>%
filter(workplace==2) %>%
mutate(workplace=as.factor(workplace))
我尝试了下面的方法,但仍然想让重叠部分更清晰。也许建议颜色组合使重叠更清晰?
ggplot(mydata_1, aes(period, id, group=id, col=workplace)) +
geom_line(alpha=0.4) +
geom_line(data=mydata_2, alpha=0.4, aes(period, id, group=id, col=workplace)) +
labs(x="time", y=NULL, title="Work affiliation") +
scale_x_continuous(breaks = seq(0,24, by=2)) +
scale_y_discrete(limits=rev) +
scale_color_manual(values=c("dodgerblue","firebrick1")) +
theme(legend.position = c(.7, .92), legend.direction = "horizontal",
legend.background = element_rect(linetype="solid", colour ="black"),
panel.background = element_rect(fill = "grey97"))
我也不明白为什么我在图例中看不到正确的职场颜色
我想我会合并数据集,然后用 position_dodge
进行单个 geom_line
调用。这会简化您的代码、显示重叠并正确显示您的图例。
all_data <- rbind(mydata_1, mydata_2)
ggplot(all_data, aes(x = id, y = period, color = workplace)) +
geom_line(position = position_dodge(width = 0.1), size = 2) +
labs(y = "time", title = "Work affiliation") +
scale_y_continuous(breaks = seq(0, 24, by = 2)) +
scale_x_discrete(limits = rev) +
scale_color_manual(values = c("dodgerblue", "firebrick1")) +
coord_flip() +
theme(legend.position = c(.7, .92),
legend.direction = "horizontal",
legend.background = element_rect(linetype = "solid", colour = "black"),
panel.background = element_rect(fill = "grey97"))