ggplot combine geom_text_repel with facet_zoom

Solution for ggplot combine geom_text_repel with facet_zoom
is Given Below:

I have the following code sample:

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

I would like to display on the main panel only the names of points that are not in the facet zoom, while in the facet zoom I would like to display only the names of the visible points. Is there a way to do both at the same time?

I would like also to avoid the usage of geom_text

I think you can use zoom.data argument from facet_zoom:

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.

First, add a zoom column to your dataset, and set to TRUE if x is less than 2 (this will be shown in the zoom panel). Otherwise zoom should be set to FALSE (this will be shown in context panel).

dataset$zoom <- ifelse(dataset$x < 2, TRUE, FALSE)

For facet_zoom use the zoom.data argument and set to the new zoom column:

facet_zoom(x = x < 2, horizontal = FALSE, zoom.data = zoom, zoom.size = 0.3, show.area = FALSE)

Here is the entire code for reproducibility:

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

Plot

plot with zoom