如何使用ggplot2在同一绘图区域内绘制绘图的缩放比例?

丹尼尔·瓦伦西亚(Daniel Valencia C.)

这个问题似乎很难理解,但是为了说明这一点,我以一个数字为例:

在此处输入图片说明

我正在尝试复制此图。到目前为止,我已经分别完成了图形处理,但是我不知道如何像示例中那样将它们组合在一起。

有什么帮助吗?

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)

在此处输入图片说明 在此处输入图片说明

dww

您可以使用自定义注释

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')

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ggplot2绘图区域内的两个轴标签

ggplot2:在绘图区域之外绘制geom_segment()

ggplot2:如何以对数比例在绘图上绘制水平线

ggplot2:如何从绘图对象读取比例转换

如何在R中使用ggplot绘制绘图区域的“外部”?

如何使用ggplot2在一个绘图中合并两个或多个绘图

确定ggplot2绘图区域的宽度

使用ggplot2绘制3D棱镜并绘图

将标题放置在绘图区域内时居中对齐ggplot标题

使用 ggplot2 包修改绘图

使用ggplot2进行绘图

绘图区域外部的ggplot2注解_ticks

获取绘图区域ggplot2的笛卡尔坐标

绘图中的不同区域如何具有不同的ggplot2背景

ggplot2:如何调整绘图区域和轴文本之间的间距

如何删除ggplot2中绘图区域和面板之间的边距?

如何并排排列`ggplot2`对象并确保绘图区域相等?

Matplotlib在绘图区域内移动刻度线标签

如何在ggplot2中将标签绘制到对应的线(在此绘图上)?

如何在绘图下方绘制坐标 - ggplot2?

R Shiny中使用ggplot2绘制绘图面板的可视化

d3.js 绘图未显示在 R Shiny 绘图区域内

使用 ggplot 更改 R 中绘图的比例

如何使用ggplot2中的shapefile从第一个绘图中保留scale_fill_color

使用R在一个布局/面/网格内绘制4个地图(2 * 2)时出现“ plot.new()中的错误:绘图区域太大”

如何控制ggplot的绘图区域比例,而不是将其拟合到R中的设备?

轴和绘图填充区域之间的填充区域(ggplot2)

如何减少ggplot绘图区域中的空白?

使用ggplot2进行绘图时遇到问题