如何使用 ggplot2 在同一绘图区域内绘制绘图的缩放?
How to plot a zoom of the plot inside the same plot area using ggplot2?
这道题好像很难理解,不过为了说明,我拿个图来举例子:
我正在尝试复制这张图。到目前为止,我已经分别完成了图形,但我不知道如何将它们放在一起,就像示例中那样。
有什么帮助吗?
time <- seq(from = 0,
to = 10,
by = 0.5)
line_1 <- c(0, 0, 0, 66, 173, 426, 1440, 800, 1200, 400, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
line_2 <- c(0, 0, 0, 0, 0, 0, 0, 0, 1000, 25000, 5000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
df <- data.frame(time, line_1, line_2)
library(ggpubr)
#the plot
ggplot(data = df, aes(x = time)) +
geom_line(aes(y = line_2), color = "red",
position = position_nudge(x = 0.5, y = 1000)) +
geom_line(aes(y = line_1),color = "blue") +
geom_rect(aes(xmin = 1, xmax = 5, ymin = 0, ymax = 1500), color = "black", alpha = 0) +
theme_pubr( base_size = 8,
border = TRUE)
#The plot with a zoom
ggplot(data = df, aes(x = time, y = line_1)) +
geom_line(color = "blue") +
xlim (1, 5) +
ylim (0, 1500) +
theme_pubr( base_size = 8,
border = TRUE)
您可以使用自定义注释
p1 = ggplot(data = df, aes(x = time)) +
geom_line(aes(y = line_2), color = "red", position = position_nudge(x = 0.5, y = 1000)) +
geom_line(aes(y = line_1),color = "blue") +
geom_rect(aes(xmin = 1, xmax = 5, ymin = 0, ymax = 1500), color = "black", alpha = 0) +
theme_pubr( base_size = 8, border = TRUE)
#The plot with a zoom
p2 = ggplot(data = df, aes(x = time, y = line_1)) +
geom_line(color = "blue") +
xlim (1, 5) +
ylim (0, 1500) +
theme_pubr( base_size = 8,border = TRUE)
p1 +
annotation_custom(ggplotGrob(p2), xmin = 0, xmax = 4, ymin = 5000, ymax = 20000) +
geom_rect(aes(xmin = 0, xmax = 4, ymin = 5000, ymax = 20000), color='black', linetype='dashed', alpha=0) +
geom_path(aes(x,y,group=grp),
data=data.frame(x = c(1,0,5,4), y=c(1500,5000,1500,5000),grp=c(1,1,2,2)),
linetype='dashed')
这道题好像很难理解,不过为了说明,我拿个图来举例子:
我正在尝试复制这张图。到目前为止,我已经分别完成了图形,但我不知道如何将它们放在一起,就像示例中那样。
有什么帮助吗?
time <- seq(from = 0,
to = 10,
by = 0.5)
line_1 <- c(0, 0, 0, 66, 173, 426, 1440, 800, 1200, 400, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
line_2 <- c(0, 0, 0, 0, 0, 0, 0, 0, 1000, 25000, 5000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
df <- data.frame(time, line_1, line_2)
library(ggpubr)
#the plot
ggplot(data = df, aes(x = time)) +
geom_line(aes(y = line_2), color = "red",
position = position_nudge(x = 0.5, y = 1000)) +
geom_line(aes(y = line_1),color = "blue") +
geom_rect(aes(xmin = 1, xmax = 5, ymin = 0, ymax = 1500), color = "black", alpha = 0) +
theme_pubr( base_size = 8,
border = TRUE)
#The plot with a zoom
ggplot(data = df, aes(x = time, y = line_1)) +
geom_line(color = "blue") +
xlim (1, 5) +
ylim (0, 1500) +
theme_pubr( base_size = 8,
border = TRUE)
您可以使用自定义注释
p1 = ggplot(data = df, aes(x = time)) +
geom_line(aes(y = line_2), color = "red", position = position_nudge(x = 0.5, y = 1000)) +
geom_line(aes(y = line_1),color = "blue") +
geom_rect(aes(xmin = 1, xmax = 5, ymin = 0, ymax = 1500), color = "black", alpha = 0) +
theme_pubr( base_size = 8, border = TRUE)
#The plot with a zoom
p2 = ggplot(data = df, aes(x = time, y = line_1)) +
geom_line(color = "blue") +
xlim (1, 5) +
ylim (0, 1500) +
theme_pubr( base_size = 8,border = TRUE)
p1 +
annotation_custom(ggplotGrob(p2), xmin = 0, xmax = 4, ymin = 5000, ymax = 20000) +
geom_rect(aes(xmin = 0, xmax = 4, ymin = 5000, ymax = 20000), color='black', linetype='dashed', alpha=0) +
geom_path(aes(x,y,group=grp),
data=data.frame(x = c(1,0,5,4), y=c(1500,5000,1500,5000),grp=c(1,1,2,2)),
linetype='dashed')