Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

r - Add error bars to multiple geom_point variables given sd values

I'd like to plot my error bars based on my SD values from each of my variables. I have tried few things (see bottom) but I believe there is a way to plot individual sd for each data point for each variable?

Location = c(1,2,3,4,5,6,7)
A = c(1.23, 0.95,0.65,0.74,0.51,0.34,0.28)
B = c(6.77,7.56,3.88,6.52,4.38,11.94,14.97)
C = c(75.45,86.66,103.36,123.2,107.53,128.9,128.49)
SD_A =c(0.10,0.03,0.01,0.05,0.00,0.01,0.02)
SD_B=c(0.02,1.05,1.97,1.45,0.60,1.88,1.45)
SD_C = c(3.56,7.46,26.1,10,10.8,10,29.03)

data = data.frame(Location, A, B, C)
data_sd = data.frame(Location, A, B, C, SD_A, SD_B, SD_C)

library(ggplot2)
library(tidyr)
library(dplyr)

data %>% pivot_longer(.,-Location, names_to = "var",values_to = "val") %>%
  filter(!is.na(val)) %>%
  mutate(NewVar = var) %>%
  add_row(., Location = c(1,1),
          var = c("B","B"),
          val = c(0,30),
          NewVar = c("Out","Out")) %>%
  ggplot(aes(x = Location, y = val, group = NewVar))+
  geom_point(aes(Location, val, shape=var), size =2) +
  ylab("")+
  facet_wrap(.~var, strip.position = "left", ncol = 1, scales = "free_y", labeller = as_labeller(c(A= "A", B= "B", C= "C")))+
  theme_bw() + theme(text = element_text(size=15), axis.text.x = element_text(colour = "black"), axis.text.y = element_text(colour = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), strip.background = element_blank(),
    strip.placement = "outside")+
  scale_x_continuous(breaks = 1:7) + theme(legend.position = "none") +scale_shape_manual(values=c(19, 0, 15))+ 

I have tried the following but I think maybe the preoblem is re-structuring the data?

geom_errorbar(aes(ymin = var-sd, ymax = var+sd),width = 0.2) # I do not have a sd column 
geom_errorbarh(aes(xmin = xmin,xmax = xmax)) # I don't want xmax and xmin since the sd are provided already
  • Note I have create a df with data only and one with data+sd data_sd which is the one we want to use.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One approach is to change the way you are using pivot_longer:

data_sd %>%
  rename_with(.cols = A:C,~str_c("Data_",.) ) %>%
  pivot_longer(-Location, names_sep = "_", names_to = c(".value","Var"))
# A tibble: 21 x 4
   Location Var     Data    SD
      <dbl> <chr>  <dbl> <dbl>
 1        1 A       1.23  0.1 
 2        1 B       6.77  0.02
 3        1 C      75.4   3.56
 4        2 A       0.95  0.03
 5        2 B       7.56  1.05
 6        2 C      86.7   7.46
 7        3 A       0.65  0.01
 8        3 B       3.88  1.97
 9        3 C     103.   26.1 
10        4 A       0.74  0.05
# … with 11 more rows

Now you have both the Data value and it's SD on the same line.

data_sd %>%
  rename_with(.cols = A:C,~str_c("Data_",.) ) %>%
  pivot_longer(-Location, names_sep = "_", names_to = c(".value","Var")) %>%
  filter(!is.na(Var)) %>%
  mutate(NewVar = Var) %>%
  add_row(Location = c(1,1),
          Var = c("B","B"),
          Data = c(0,30),
          NewVar = c("Out","Out")) %>%
ggplot(aes(x = Location, y = Data, group = NewVar))+
  geom_point(aes(Location, Data, shape=Var), size =2) +
  ylab("")+
  facet_wrap(.~Var, strip.position = "left", ncol = 1, scales = "free_y", labeller = as_labeller(c(A= "A", B= "B", C= "C")))+
  theme_bw() + theme(text = element_text(size=15), axis.text.x = element_text(colour = "black"), axis.text.y = element_text(colour = "black"),
                     panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), strip.background = element_blank(),
                     strip.placement = "outside")+
  scale_x_continuous(breaks = 1:7) + theme(legend.position = "none") +scale_shape_manual(values=c(19, 0, 15)) +
  geom_errorbar(aes(ymin = Data-SD, ymax = Data+SD),width = 0.2)

enter image description here

If you want to change the y axis limits individually, you might use the facetscales package.

#remotes::install_github("zeehio/facetscales")
library(facetscales)
data_sd %>%
  rename_with(.cols = A:C,~str_c("Data_",.) ) %>%
  pivot_longer(-Location, names_sep = "_", names_to = c(".value","Var")) %>%
  filter(!is.na(Var)) %>%
  mutate(NewVar = Var) %>%
  ggplot(aes(x = Location, y = Data, group = NewVar))+
  geom_point(aes(Location, Data, shape=Var), size =2) +
  facet_grid_sc(rows = vars(Var), switch = "y",
                scales = list(y = list(A = scale_y_continuous(), 
                                       B = scale_y_continuous(limits = c(0,30)),
                                       C = scale_y_continuous()))) +
  theme_bw() + theme(text = element_text(size=15), axis.text.x = element_text(colour = "black"), axis.text.y = element_text(colour = "black"),
                     panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), strip.background = element_blank(),
                     strip.placement = "outside")+
  scale_x_continuous(breaks = 1:7) + theme(legend.position = "none") + scale_shape_manual(values=c(19, 0, 15)) +
  geom_errorbar(aes(ymin = Data-SD, ymax = Data+SD),width = 0.2)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...