首页 > 解决方案 > ggplot2 意外的 vapply 错误

问题描述

让我们考虑以下示例:

library(ggplot2)

zzz <- data.frame(gp = c("a","b","c","d","e","f","g","h","i","j","k","l","m"), 
              c1 = c(1,1,1,1,1,1,1,1,1,1,1,1,1), 
              c2 = c(1,1,1,1,1,1,1,1,1,1,1,1,1))

ggplot(zzz, aes(x = c1, y = c2)) +
  facet_wrap(~ gp, scales = "free", strip.position = "bottom") +
  geom_point() +
  theme(
    aspect.ratio = 1,
    strip.background = element_blank(),
    strip.placement = "outside"
  )

为什么我会收到以下错误?我怎么能克服它?

Error in vapply(row_axes, is.zero, logical(length(row_axes))) : 
values must be length 3,
but FUN(X[[1]]) result is length 1

我测试的一些调整表明,如果:

a)我删除了 data.frame 中的一行,或者如果我再添加 2 行,每行都有一个新组,或者

b) 我删除strip.placement = "outside"strip.position = "bottom"

那是一个错误吗?我错过了什么?我想保留条带设置以保持美观...

标签: rggplot2

解决方案


我不知道你的代码有什么问题,它在scales/strip.plavcement.

这段代码对我有用:

library(ggplot2)

zzz <- data.frame(gp = c("a","b","c","d","e","f","g","h","i","j","k","l","m"), 
                  c1 = c(1,1,1,1,1,1,1,1,1,1,1,1,1), 
                  c2 = c(1,1,1,1,1,1,1,1,1,1,1,1,1))
str(zzz)
zzz$c1=as.character(zzz$c1)
zzz$c2=as.character(zzz$c2)
ggplot(zzz, aes(x = c1, y = c2)) +geom_point() +
  # facet_wrap(~ gp, scales = "free", strip.position = "bottom") 
  facet_wrap(~gp,nrow=1,strip.position = "bottom")+
  theme(panel.background = element_blank(),
        strip.background = element_blank(),axis.text.x=element_blank())
  # theme(
  #   aspect.ratio = 1,
  #   strip.background = element_blank(),
  #   strip.placement = "outside")

输出是这样的......希望这会有所帮助输出图


推荐阅读