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.
One approach is to create a data frame with the rect coordinates that will also have the facet variable. Example:
some data:
create a data frame with
geom_rect
coordinates:plot it:
This allows for per facet customization of rectangles. Example:
or just to plot rectangles in some facets: