ggplot2意外的vapply错误

时间:2018-05-17 06:49:17

标签: r ggplot2

让我们考虑以下示例:

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"

这是一个错误吗?我错过了什么?我想保持条纹设置的美学......

1 个答案:

答案 0 :(得分:0)

我不知道您的代码中存在什么问题,它位于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")

输出是这个...希望这有助于Output Plot