stackinator
stackinator

Reputation: 5819

ggplot2 error - replacement has 100 rows, data has 2

Generate the following data frames.

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

Then plot the following.

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

When I try to add some geom_point() I get an error.

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

How do I fix this issue?

Error in $<-.data.frame(*tmp*, "PANEL", value = c(1L, 1L, 1L, 1L, : replacement has 100 rows, data has 2

I can add points on top of points in the same fashion on the economics data set with no issue. See below. Why am I having trouble in my prime example?

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

Upvotes: 2

Views: 2712

Answers (1)

smanski
smanski

Reputation: 541

All you need to do is facet your new data. That is,

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

enter image description here

Upvotes: 3

Related Questions