faceting based on geom_point when ggplot() empty and multiple shaded geom_rect exist

对着背影说爱祢 提交于 2019-12-13 05:27:52

问题


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



来源:https://stackoverflow.com/questions/51092214/faceting-based-on-geom-point-when-ggplot-empty-and-multiple-shaded-geom-rect-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!