giordano
giordano

Reputation: 3152

ggplot: Prevent adding CI-region for one level of two

I have a dataset with a factor with two levels, a continuous variable and a continuous outcome. I would like to show the two smoothed curves but only one curve should have a confidence region. Here is the dataset:

# Data             
xdf <- data.frame(x = rep(1:10,2)
                 , y = c(rpois(10,10),rpois(10,20))
                 , g = rep(c("A","B"), each=10)
                 )

and here is the plot

# Plot             
windows(width=12, height=20)             
ggplot(xdf, aes(x,y,linetype=g)) +
          geom_smooth(se=TRUE) +
          geom_point()

enter image description here

Any idea how to prevent one CI-region?
Thanks for help.

Upvotes: 1

Views: 24

Answers (1)

tonytonov
tonytonov

Reputation: 25608

The simplest way I see is overriding data argument like so:

ggplot(xdf, aes(x,y,linetype=g)) +
  geom_smooth(se = F) +
  geom_point() + 
  geom_smooth(data = xdf[xdf$g == "A", ], se = T)

enter image description here

Upvotes: 1

Related Questions