ggplot 结合 geom_text_repel 和 facet_zoom
ggplot combine geom_text_repel with facet_zoom
我有以下代码示例:
x <- c(1,1.2,1.3,1.1,2,2.5,3.6)
y <- c(3,3.1,2.9,3.6,4.5,5.6,6.7)
z <- c('Austria',' Germany', 'Italy', 'France', 'Spain','Portugal', 'Belgium')
dataset <-data.frame(x,y,z)
ggp <- ggplot(dataset, mapping = aes(x=x, y=y)) +
geom_text_repel(mapping = aes(label = z),
size = 2,
min.segment.length = 0,
seed = 42,
box.padding = 0.4,
arrow = arrow(length = unit(0.007, "npc")),
nudge_x = .03,
nudge_y = .03,
color = "grey60") +
geom_point(data = dataset,aes(colour=z, size = y/x), alpha=0.6) +
facet_zoom(x = x < 2, horizontal = FALSE ,zoom.size = 0.3, show.area = FALSE) +
coord_cartesian(clip="off")
ggp
我想在主面板上只显示不在小平面缩放中的点的名称,而在小平面缩放中我只想显示可见点的名称。有没有办法同时进行?
我也想避免使用 geom_text
我认为你可以使用 facet_zoom
:
中的 zoom.data
参数
zoom.data: An expression evaluating to a logical vector. If TRUE the
data only shows in the zoom panels. If FALSE the data only show in the
context panel. If NA the data will show in all panels.
首先,向数据集添加一个 zoom
列,如果 x
小于 2,则设置为 TRUE
(这将显示在缩放面板中)。否则 zoom
应设置为 FALSE
(这将显示在上下文面板中)。
dataset$zoom <- ifelse(dataset$x < 2, TRUE, FALSE)
对于 facet_zoom
使用 zoom.data
参数并设置为新的 zoom
列:
facet_zoom(x = x < 2, horizontal = FALSE, zoom.data = zoom, zoom.size = 0.3, show.area = FALSE)
这里是可重复性的完整代码:
library(ggplot2)
library(ggrepel)
library(ggforce)
x <- c(1,1.2,1.3,1.1,2,2.5,3.6)
y <- c(3,3.1,2.9,3.6,4.5,5.6,6.7)
z <- c('Austria',' Germany', 'Italy', 'France', 'Spain','Portugal', 'Belgium')
dataset <-data.frame(x,y,z)
dataset$zoom <- ifelse(dataset$x < 2, TRUE, FALSE)
ggp <- ggplot(dataset, mapping = aes(x=x, y=y)) +
geom_text_repel(mapping = aes(label = z),
size = 2,
min.segment.length = 0,
seed = 42,
box.padding = 0.4,
arrow = arrow(length = unit(0.007, "npc")),
nudge_x = .03,
nudge_y = .03,
color = "grey60") +
geom_point(aes(colour=z, size = y/x), alpha=0.6) +
facet_zoom(x = x < 2, horizontal = FALSE , zoom.data = zoom, zoom.size = 0.3, show.area = FALSE) +
coord_cartesian(clip="off")
ggp
情节
我有以下代码示例:
x <- c(1,1.2,1.3,1.1,2,2.5,3.6)
y <- c(3,3.1,2.9,3.6,4.5,5.6,6.7)
z <- c('Austria',' Germany', 'Italy', 'France', 'Spain','Portugal', 'Belgium')
dataset <-data.frame(x,y,z)
ggp <- ggplot(dataset, mapping = aes(x=x, y=y)) +
geom_text_repel(mapping = aes(label = z),
size = 2,
min.segment.length = 0,
seed = 42,
box.padding = 0.4,
arrow = arrow(length = unit(0.007, "npc")),
nudge_x = .03,
nudge_y = .03,
color = "grey60") +
geom_point(data = dataset,aes(colour=z, size = y/x), alpha=0.6) +
facet_zoom(x = x < 2, horizontal = FALSE ,zoom.size = 0.3, show.area = FALSE) +
coord_cartesian(clip="off")
ggp
我想在主面板上只显示不在小平面缩放中的点的名称,而在小平面缩放中我只想显示可见点的名称。有没有办法同时进行?
我也想避免使用 geom_text
我认为你可以使用 facet_zoom
:
zoom.data
参数
zoom.data: An expression evaluating to a logical vector. If TRUE the data only shows in the zoom panels. If FALSE the data only show in the context panel. If NA the data will show in all panels.
首先,向数据集添加一个 zoom
列,如果 x
小于 2,则设置为 TRUE
(这将显示在缩放面板中)。否则 zoom
应设置为 FALSE
(这将显示在上下文面板中)。
dataset$zoom <- ifelse(dataset$x < 2, TRUE, FALSE)
对于 facet_zoom
使用 zoom.data
参数并设置为新的 zoom
列:
facet_zoom(x = x < 2, horizontal = FALSE, zoom.data = zoom, zoom.size = 0.3, show.area = FALSE)
这里是可重复性的完整代码:
library(ggplot2)
library(ggrepel)
library(ggforce)
x <- c(1,1.2,1.3,1.1,2,2.5,3.6)
y <- c(3,3.1,2.9,3.6,4.5,5.6,6.7)
z <- c('Austria',' Germany', 'Italy', 'France', 'Spain','Portugal', 'Belgium')
dataset <-data.frame(x,y,z)
dataset$zoom <- ifelse(dataset$x < 2, TRUE, FALSE)
ggp <- ggplot(dataset, mapping = aes(x=x, y=y)) +
geom_text_repel(mapping = aes(label = z),
size = 2,
min.segment.length = 0,
seed = 42,
box.padding = 0.4,
arrow = arrow(length = unit(0.007, "npc")),
nudge_x = .03,
nudge_y = .03,
color = "grey60") +
geom_point(aes(colour=z, size = y/x), alpha=0.6) +
facet_zoom(x = x < 2, horizontal = FALSE , zoom.data = zoom, zoom.size = 0.3, show.area = FALSE) +
coord_cartesian(clip="off")
ggp
情节