utritala
utritala

Reputation: 61

Not getting confidence intervals ggplot R

I am struggling to get confidence interval around my regression line. My data consists of 7 columns and 50000 rows.

ID H.FC HFD N.FC NFD Group
G00000000004    1.08403833300442    0.00209119205547622 1.12705351468201    0.0017652841766293  BvsA
    G00000000059    1.70298155378132    0.000146008455537281    1.78927991144484    0.000126476263754446    BvsA
    G00000000067    1.48885136450707    1.94192154467639e-05    1.49169658915702    5.47633140183071e-05    CvsA
    G00000000081    5.92680429312136    3.63075878342954e-06    5.89059544062979    7.07992913581687e-06    DvsA
    G00000000086    0.499795076715132   0.00265935106849242 0.542319766242586   0.00212335608196823 BvsC
    G00000000102    -2.60510733887004   0.000669953697126189    -2.62720386931755   0.000122899865824463    BvsA
    G00000000104    -2.80909148854584   0.00686396994798396 -2.94362698679174   0.00342818761913247 BvsA
    G00000000106    0.255264785072867   0.0388723342557597  0.174743590276556   0.197263787912382   BvsD
    G00000000109    1.32895814248434    0.000311378914835491    1.30541212379603    0.000308851884560488    EvsF

Here is my code:

data <- read.table("grid_contrast_1-8.tsv", header=T)
df <- data.frame(data$H.FC, data$N.FC, data$Group)
png("grid_scatter.png")
ggplot(df, aes(data.H.FC, data.N.FC, color = data.H.FC)) + 
  geom_point(size = 0.5) +
  stat_smooth(geom = 'line', alpha = 0.5, se = TRUE, color = "black", level = 0.95) +
  facet_wrap(~ data.Group) +
  labs(x = "H", y = "F") +
  scale_color_gradient(low = "#0091ff", high = "#f0650e")
dev_off()

Above is what I get: Scatter plot grid for all groups

I would like to have confidence interval band around the regression line as given below: Example

As I haven't specified "se=FALSE" in stat_smooth, I should get the interval but somehow I am not getting it. Please can someone help?

Many thanks in advance.

Upvotes: 3

Views: 4018

Answers (1)

camille
camille

Reputation: 16842

The SE ribbon is there, it's just tiny! You have a ton of observations, and they fall in a pretty neat line, so it looks like your confidence intervals aren't very wide. I changed a few things to test this out. I took a sample of just 1% of each group and plotted that, so there would be a less narrow confidence interval. I also upped the confidence level to 0.999. Because there's fewer observations, geom_smooth defaults to a LOESS; not sure if you have specific parameters you want for the smoothing line, but you might want to change the method and its arguments.

library(tidyverse)
df <- read_tsv("~/Downloads/test_file.tsv") %>%
    select(2, 4, 6) %>%
    setNames(c("data.H.FC", "data.N.FC", "data.Group"))

set.seed(123)
df %>%
    group_by(data.Group) %>%
    sample_frac(0.01) %>%
    ggplot(aes(data.H.FC, data.N.FC, color = data.H.FC)) + 
    geom_point(size = 0.5) +
    stat_smooth(color = "black", size = 0.5, level = 0.999) +
    facet_wrap(~ data.Group) +
    labs(x = "H", y = "F") +
    scale_color_gradient(low = "#0091ff", high = "#f0650e")
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

All the way at the corners, you can now see the SE ribbon. It's not a problem with your code, just what becomes visible.

Created on 2018-04-06 by the reprex package (v0.2.0).

Upvotes: 5

Related Questions