faceting based on geom_point when ggplot() empty a

2019-08-30 06:12发布

问题:

I am attempting to shade multiple regions of interest on a scatter plot. Based on this answer, I believe I am forced to leave the intial ggplot() call empty, and supplie the geom_rect() calls before the geom_point() call. I have gotten all of this to work. I can not however apply a facet based on data passed to geom_point().

The following correctly plots my data (scatter plot over red and blue regions of different sizes):

ggplot() + 
geom_rect(aes(xmin=885, xmax=1544, ymin=-Inf, ymax=Inf), alpha=.2, fill = "red") +
geom_rect(aes(xmin=1858, xmax=2580, ymin=-Inf, ymax=Inf), alpha=.2, fill = "blue") +
geom_point(data=df, aes(x=Position, y=Max_Freq))

However all of the following produce errors as follows:

+ facet_wrap(~Replicate)
#Error in if (empty(data)) { : missing value where TRUE/FALSE needed

+ facet_wrap(data~Replicate)
#Error in combine_vars(data, params$plot_env, rows, drop = params$drop):
#  At least one layer must contain all variables used for facetting

+ facet_wrap(data$Replicate)
#Error in data$Replicate : object of type 'closure' is not subsettable

In other graphs when df is supplied in the ggplot() call (ie ggplot(df, aes(x=Position, y=Max_Freq)) the first option correctly facets the data. Admittedly, I am not well versed in R, but it seems like this should have a simple solution.

回答1:

One approach is to create a data frame with the rect coordinates that will also have the facet variable. Example:

some data:

df <- data.frame(x = rnorm(20),
                 y = runif(20),
                 facet = sample(c("A", "B"),
                                20,
                                replace = TRUE))

create a data frame with geom_rect coordinates:

rect1 <- data.frame(xmin = -1,
                    xmax = 1,
                    ymin = -Inf,
                    ymax = Inf,
                    facet = c("A", "B"))

plot it:

ggplot() + 
  geom_rect(data = rect1 , aes(xmin = xmin,
                               xmax = xmax,
                               ymin = ymin,
                               ymax = ymax),
            alpha = 0.2, fill = "blue") +
  geom_point(data = df, aes(x = x, y = y))+
  facet_wrap(~facet)

This allows for per facet customization of rectangles. Example:

rect2 <- data.frame(xmin = c(-1, 0),
                    xmax = c(0, 2),
                    ymin = c(-Inf, 0.25),
                    ymax = Inf,
                    facet = c("A", "B"))

ggplot() + 
  geom_rect(data = rect2 , aes(xmin = xmin,
                               xmax = xmax,
                               ymin = ymin,
                               ymax = ymax,
                               fill = facet),
            alpha = 0.2) +
  geom_point(data = df, aes(x = x, y = y))+
  facet_wrap(~facet)

or just to plot rectangles in some facets:

rect3 <- data.frame(xmin = -1,
                    xmax = 0, 
                    ymin = 0.25,
                    ymax = Inf,
                    facet = "A")