简体   繁体   中英

ggforce facet_zoom error with ggplot2 on R

I have a data.frame in R 4.0.2 with a continuous variable in one column and two possible values of a categorical variable (variable 'type': known or novel) in another, which I use to color them differently (using a palette from ggsci 2.9 package). I represent an histogram (stat_bin) with ggplot2 3.3.2 and I want to use the facet_zoom function of ggforce 0.3.2 to zoom only the data belonging to one of the 'types' (using the option zoom.data, as it is done in the volcano example on http://cran.univ-paris1.fr/web/packages/ggforce/vignettes/Visual_Guide.html#contextual-zoom ), however I get this error:

Error: Aesthetics must be either length 1 or the same as the data (2000): x

Reproducible example:

library(ggplot2)
library(ggsci)
library(ggforce)

testdata <- as.data.frame(sort(rnorm(1000)))
testdata$type <- "known"
testdata[501:1000,2] <- "novel"

# Working code
ggplot(testdata) +
  stat_bin(aes(x=testdata[,1], fill = type), binwidth = 1, color="white") +
  scale_fill_npg() + theme_light() +
  facet_zoom(xlim = c(0, 4), ylim = c(0, 300), horizontal = TRUE, zoom.size = 0.3)

# Desired code
ggplot(testdata) +
  stat_bin(aes(x=testdata[,1], fill = type), data = cbind(testdata, zoom = FALSE), binwidth = 1, color="white") +
  stat_bin(aes(x=testdata[testdata$type == "novel",1]), data = cbind(testdata, zoom = TRUE), binwidth = 0.5) +
  scale_fill_npg() + theme_light() +
  facet_zoom(xlim = c(0, 4), ylim = c(0, 300), horizontal = TRUE, zoom.size = 0.3, zoom.data = zoom)

Thanks!

The issue is that you pass the whole dataset as data in the second stat_bin . Simply pass the subsetted df instead of trying to subset in aes() :

BTW: I also renamed the first variable in your data as x.

library(ggplot2)
library(ggsci)
library(ggforce)

set.seed(42)

testdata <- data.frame(x = sort(rnorm(1000)))
testdata$type <- "known"
testdata[501:1000,2] <- "novel"

# Desired code
ggplot(testdata) +
  stat_bin(aes(x = x, fill = type), data = cbind(testdata, zoom = FALSE), binwidth = 1, color="white") +
  stat_bin(aes(x = x), data = cbind(testdata[testdata$type == "novel", ], zoom = TRUE), binwidth = 0.5) +
  scale_fill_npg() + theme_light() +
  facet_zoom(xlim = c(0, 4), ylim = c(0, 300), horizontal = TRUE, zoom.size = 0.3, zoom.data = zoom)

To only show the type == "novel" data in the zoomed plot, try this:

library(tidyverse)
library(ggsci)
library(ggforce)

testdata <- data.frame(values = sort(rnorm(1000)))
testdata$type <- "known"
testdata[501:1000,2] <- "novel"

# Desired code
ggplot(testdata) +
  stat_bin(aes(x = values, fill = type),
           binwidth = 1, color="white") +
  scale_fill_npg() + theme_light() +
  facet_zoom(zoom.data = ifelse(type == "novel", NA, FALSE), 
             xlim = c(0, 4), ylim = c(0, 300),
             horizontal = TRUE)

例子.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM