如何放大部分绘图并删除缩放区域外的注释?
How to zoom in part of the plot and remove annotation outside the zoomed area?
我用 geom_rect
创建了一个图,并用 geom_text_repel
添加了注释,但是当我想创建多个图时,我放大了原始图的一部分。缩放区域外区域的标签也会出现。
这是一个最小的例子:
start = c(1, 5,8, 14, 19, 25)
end =c(3, 6,12, 16, 22, 30)
label = c(1,2,3, 4, 5, 6)
library(ggplot2)
library(ggrepel)
regions = tibble::tibble(label, start, end)
ggplot() +
scale_x_continuous() +
scale_y_continuous(name = "") +
geom_rect(
data = regions,
mapping = aes(
xmin = start,
xmax = end,
ymin = 1.5,
ymax = 1.8),
color = "black",
fill = "#56B4E9"
) +
geom_text_repel(
data = regions,
aes(
x = start + (end - start) / 2,
y = 1.8,
label = label,
),
size = 10,
force_pull = 0,
nudge_y = 0.05,
direction = "x",
angle = 90,
vjust = 0,
segment.size = 0.5,
) +
ylim(1.38, 2.2) +
ylab("") +
xlab("") +
theme_minimal()
此代码生成此图:
我想放大方框 3,所以我尝试添加 + xlim(8,12)
或 +facet_zoom(xlim = c(8, 12))
但放大的图在旁边有方框 1、方框 2 的注释(标签)...正如您在此处看到的(右侧为 1,2,左侧为 4,5,6)
和 + xlim(8,12)
类似的结果
如何删除缩放区域之外的标签(注释)(缩放图右侧的 1,2 和左侧的 4,5,6?)
我能想到两个快速修复方法,第一个就是您已经提到的那个。也许你打错了,我可以 运行 没关系。
- 设置
xlim(8,12)
library(ggrepel)
start = c(1, 5,8, 14, 19, 25)
end =c(3, 6,12, 16, 22, 30)
label = c(1,2,3, 4, 5, 6)
regions = data.frame(label, start, end)
ggplot() +
scale_x_continuous() +
scale_y_continuous(name = "") +
geom_rect(
data = regions,
mapping = aes(
xmin = start,
xmax = end,
ymin = 1.5,
ymax = 1.8),
color = "black",
fill = "#56B4E9"
) +
geom_text_repel(
data = regions,
aes(
x = start + (end - start) / 2,
y = 1.8,
label = label,
),
size = 10,
force_pull = 0,
nudge_y = 0.05,
direction = "x",
angle = 90,
vjust = 0,
segment.size = 0.5,
) +
ylim(1.38, 2.2) +
xlim(8, 12) +
ylab("") +
xlab("") +
theme_minimal()
如果我运行这个我得到下面的图像
但是,并不总是建议使用 xlim()
,因为它会丢弃所有其他不满足条件的点。尽管对于您的情况来说这可能是有利的。
- 子集
regions
并使用 coord_cartesian()
正确放大。
ggplot() +
scale_x_continuous() +
scale_y_continuous(name = "") +
geom_rect(
data = regions,
mapping = aes(
xmin = start,
xmax = end,
ymin = 1.5,
ymax = 1.8),
color = "black",
fill = "#56B4E9"
) +
geom_text_repel(
data = subset(regions, label == 3),
aes(
x = start + (end - start) / 2,
y = 1.8,
label = label,
),
size = 10,
force_pull = 0,
nudge_y = 0.05,
direction = "x",
angle = 90,
vjust = 0,
segment.size = 0.5,
) +
ylim(1.38, 2.2) +
coord_cartesian(xlim = c(8, 12)) +
ylab("") +
xlab("") +
theme_minimal()
这会产生相同的图像(据我所知)
我用 geom_rect
创建了一个图,并用 geom_text_repel
添加了注释,但是当我想创建多个图时,我放大了原始图的一部分。缩放区域外区域的标签也会出现。
这是一个最小的例子:
start = c(1, 5,8, 14, 19, 25)
end =c(3, 6,12, 16, 22, 30)
label = c(1,2,3, 4, 5, 6)
library(ggplot2)
library(ggrepel)
regions = tibble::tibble(label, start, end)
ggplot() +
scale_x_continuous() +
scale_y_continuous(name = "") +
geom_rect(
data = regions,
mapping = aes(
xmin = start,
xmax = end,
ymin = 1.5,
ymax = 1.8),
color = "black",
fill = "#56B4E9"
) +
geom_text_repel(
data = regions,
aes(
x = start + (end - start) / 2,
y = 1.8,
label = label,
),
size = 10,
force_pull = 0,
nudge_y = 0.05,
direction = "x",
angle = 90,
vjust = 0,
segment.size = 0.5,
) +
ylim(1.38, 2.2) +
ylab("") +
xlab("") +
theme_minimal()
此代码生成此图:
我想放大方框 3,所以我尝试添加 + xlim(8,12)
或 +facet_zoom(xlim = c(8, 12))
但放大的图在旁边有方框 1、方框 2 的注释(标签)...正如您在此处看到的(右侧为 1,2,左侧为 4,5,6)
和 + xlim(8,12)
如何删除缩放区域之外的标签(注释)(缩放图右侧的 1,2 和左侧的 4,5,6?)
我能想到两个快速修复方法,第一个就是您已经提到的那个。也许你打错了,我可以 运行 没关系。
- 设置
xlim(8,12)
library(ggrepel)
start = c(1, 5,8, 14, 19, 25)
end =c(3, 6,12, 16, 22, 30)
label = c(1,2,3, 4, 5, 6)
regions = data.frame(label, start, end)
ggplot() +
scale_x_continuous() +
scale_y_continuous(name = "") +
geom_rect(
data = regions,
mapping = aes(
xmin = start,
xmax = end,
ymin = 1.5,
ymax = 1.8),
color = "black",
fill = "#56B4E9"
) +
geom_text_repel(
data = regions,
aes(
x = start + (end - start) / 2,
y = 1.8,
label = label,
),
size = 10,
force_pull = 0,
nudge_y = 0.05,
direction = "x",
angle = 90,
vjust = 0,
segment.size = 0.5,
) +
ylim(1.38, 2.2) +
xlim(8, 12) +
ylab("") +
xlab("") +
theme_minimal()
如果我运行这个我得到下面的图像xlim()
,因为它会丢弃所有其他不满足条件的点。尽管对于您的情况来说这可能是有利的。
- 子集
regions
并使用coord_cartesian()
正确放大。
ggplot() +
scale_x_continuous() +
scale_y_continuous(name = "") +
geom_rect(
data = regions,
mapping = aes(
xmin = start,
xmax = end,
ymin = 1.5,
ymax = 1.8),
color = "black",
fill = "#56B4E9"
) +
geom_text_repel(
data = subset(regions, label == 3),
aes(
x = start + (end - start) / 2,
y = 1.8,
label = label,
),
size = 10,
force_pull = 0,
nudge_y = 0.05,
direction = "x",
angle = 90,
vjust = 0,
segment.size = 0.5,
) +
ylim(1.38, 2.2) +
coord_cartesian(xlim = c(8, 12)) +
ylab("") +
xlab("") +
theme_minimal()
这会产生相同的图像(据我所知)