ggplot2错误 - 替换有100行,数据有2

时间:2018-03-23 21:29:30

标签: r ggplot2

生成以下数据框。

library(tidyverse)
library(qicharts2)
plot1 <- qic(age,
              data    = tail(cabg, 100), 
              chart   = 'i',
              title   = 'Age of the last 100 patients (I chart)',
              ylab    = 'Years',
              xlab    = 'Patient #',
              facet   = ~ gender)
p1 <- plot1$data

然后绘制以下内容。

plot2 <- ggplot(p1, aes(x, y)) +
  geom_ribbon(ymin = p1$lcl, ymax = p1$ucl, fill = "black", alpha = 0.05) +
  geom_line(colour = "black", size = 1) + 
  geom_line(aes(x, cl)) +
  geom_point(colour = "black" , fill = "black", size = 2) +
  ggtitle(label = "") +
  labs(x = NULL, y = NULL) +
  scale_y_continuous(breaks = seq(0, 100, by = 10)) + 
  facet_grid(~ p1$facet1) + 
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
plot2

当我尝试添加一些geom_point()时,我收到错误。

plot2 + geom_point(
    data = p1 %>% filter(sigma.signal == TRUE),
    color = "red"
  )

如何解决此问题?

  

$<-.data.frame中的错误(*tmp*,&#34; PANEL&#34;,值= c(1L,1L,1L,1L,   :替换有100行,数据有2个

我可以在经济数据集上以相同的方式在点上添加点,没有任何问题。见下文。为什么我的主要例子有问题?

ggplot(economics, aes(date, unemploy)) + 
  geom_point() + 
  geom_point(
    data = economics %>% 
      filter(date > as.Date("2009-12-31")), 
    color = "red"
    )

1 个答案:

答案 0 :(得分:3)

您需要做的就是面对新数据。也就是说,

plot2 + geom_point(
  data = p1 %>% filter(sigma.signal == TRUE),
  color = "red") + facet_grid(~ facet1)

enter image description here