【问题标题】:Error Bars on Bar Plot are Incorrectly Shaped条形图上的误差线形状不正确
【发布时间】:2021-08-13 18:29:03
【问题描述】:

我已经生成了这个图:

但是,我的误差线没有正确定位(见图)。我尝试将 fill = RepElement 放入 ggplot aes,但问题仍然存在。我不确定如何在我的图表上正确定位这些误差线。有谁知道我做错了什么?

# Ggplot Code

    pd <- peak_intersect_rep_elements_means %>%
  mutate(Antibody = factor(Antibody, sample_order)) %>%
  select(Sample, Mean_Alu_frac, 
         Mean_L1_frac, Antibody, CellLine, SD_Alu_frac, SD_L1_frac) %>%
  gather(RepElement, Frac, -Sample, -Antibody, 
         -CellLine, -SD_Alu_frac, -SD_L1_frac)

pd <- pd %>%
  gather(SdElement, FracSD, -RepElement, -Frac,-Sample, -Antibody, 
         -CellLine)

pd %>%
  ggplot(aes(Antibody, Frac, fill = RepElement)) +
  facet_wrap(~CellLine, scales = 'free_x') +
  geom_bar(stat = "identity", position = "dodge", size = 0.1,
           aes(fill = RepElement,
               color = Sample)) +
  geom_errorbar(aes(ymin=Frac - FracSD , ymax=Frac + FracSD), width=.3,
                position=position_dodge(.9)) +
  #theme(axis.text.x = element_text(angle = 45, hjust=1)) +
  scale_color_manual(values = c("ADAR062" = "black", "ADAR112" = "black",
                                 "ADAR004" = "black")) +
  #scale_fill_manual(values = c("Alu" = "#125863",
  #                             "L1" =  "#2BA8B3")) +
  scale_fill_manual(values = c("Mean_Alu_frac" = "#125863",
                               "Mean_L1_frac" =  "#2BA8B3")) +
  scale_y_continuous(labels = scales::percent) +
  guides(color = F) +
  theme_minimal() +
  labs(y = '', x = '', color = '', fill = 'Rep element',
       title = "Intersection of ADAR1 IP peaks and repetetive elements") + 
  NULL

Ggplot 补充代码

library(tidyverse)
library(parallel)
library(devtools)
library(scales)


# Peak info 
```{r}
peak_intersect_rep_elements <-
  tribble(
    ~Sample,                ~CellLine, ~Rep,  ~Total_peaks, ~Alu_intersect, ~L1_intersect, ~Antibody,
    "ADAR062", "HEK293xT", "rep1",   4407,           3329,        201, "p110/p150\nAb1",
    "ADAR062", "HEK293xT", "rep1",   19103,          3481,        8737, "p150\nAb3",
    "ADAR062", "HEK293xT", "rep1",   1782,           836,         109, "p110/p150\nAb4",
    "ADAR112", "HEK293xT", "rep1",   2269,           1852,        61, "p110/p150\nAb1",
    "ADAR112", "HEK293xT", "rep1",   28573,          5725,        17037, "p150\nAb3",
    "ADAR112", "HEK293xT", "rep1",   5115,           4448,        213, "p110/p150\nAb4",
    "ADAR112", "K562",     "rep1",   1367,           770,         49, "p110/p150\nAb1",
    "ADAR112", "K562",     "rep1",   12195,          2889,        5323, "p150\nAb3",
    "ADAR112", "K562",     "rep1",   1178,           656,         58, "p110/p150\nAb4",
    "ADAR004", "HEK293xT", "rep1",   4130,           3289,        136, "p110/p150\nAb1",
    "ADAR004", "HEK293xT", "rep2",   3447,           2816,        135, "p110/p150\nAb1",
    "ADAR004", "HEK293xT", "rep3",   4607,           3697,        176, "p110/p150\nAb1",
    "ADAR004", "HEK293xT", "rep1",   9711,           8450,        373, "p110/p150\nAb4",
    "ADAR004", "HEK293xT", "rep2",   7275,           6163,        294, "p110/p150\nAb4",
    "ADAR004", "HEK293xT", "rep3",   6789,           5704,        256, "p110/p150\nAb4"
  )    
```

```{r}
peak_intersect_rep_elements.exp1_exp2 <- 
  peak_intersect_rep_elements %>%
  filter(Sample %in% c("ADAR062", "ADAR112")) %>%
  mutate(Alu_frac = Alu_intersect / Total_peaks,
         L1_frac = L1_intersect / Total_peaks) %>%
  rename(Mean_Alu_frac = Alu_frac,
         Mean_L1_frac = L1_frac) %>%
  select(Sample, CellLine, Antibody, Mean_Alu_frac, Mean_L1_frac) %>%
  mutate(SD_Alu_frac = 0, SD_L1_frac = 0)

peak_intersect_rep_elements.exp3 <- 
  peak_intersect_rep_elements %>%
  filter(Sample == "ADAR004") %>%
  mutate(Alu_frac = Alu_intersect / Total_peaks,
         L1_frac = L1_intersect / Total_peaks) %>%
  group_by(Sample, CellLine, Antibody) %>%
  summarise(Mean_Alu_frac = mean(Alu_frac),
            Mean_L1_frac = mean(L1_frac),
            SD_Alu_frac = sd(Alu_frac),
            SD_L1_frac = sd(L1_frac))

peak_intersect_rep_elements_means <-
  bind_rows(
    peak_intersect_rep_elements.exp1_exp2, 
    peak_intersect_rep_elements.exp3 ) 

peak_intersect_rep_elements_means

sample_order <-c(
  "p110/p150\nAb1",
  "p110/p150\nAb4",
  "p150\nAb3"
)

【问题讨论】:

  • 如果您也将position = "dodge" 添加到您的geom_errorbar 通话中,是否有效?
  • 嗨,Bas,刚刚尝试过,并且 position = "dodge" 改变了误差线的位置,但它们仍然像我上图一样漂浮在误差线上方。
  • 你确定ymin=Frac + SD_Alu_frac , ymax=Frac+ SD_L1_frac 是正确的吗?你不想减去标准差得到ymin,而不是相加吗?
  • 我不肯定他们是正确的。我尝试从 ymin 中减去,但仍然遇到同样的问题。
  • 我认为这就是问题所在。仔细检查您是否从正确的Frac 中减去正确的SD。我认为您可能还需要gather SD 列。

标签: r ggplot2


【解决方案1】:

下面做你想要的。我使用pivot_longergather 的较新版本)来收集正确的 SD 和平均值。在图中,我将color = Sample 添加到ggplot() 调用中,以确保position="dodge" 避开不同的错误栏。

pd <- peak_intersect_rep_elements_means %>%
  mutate(Antibody = factor(Antibody, sample_order)) %>%
  select(Sample, Mean_Alu_frac, 
         Mean_L1_frac, Antibody, CellLine, SD_Alu_frac, SD_L1_frac)

pd <- pd %>% 
  pivot_longer(ends_with("_frac"),
               names_pattern = "(.*)_(.*)_frac",
               names_to = c(".value", "metric"))

pd %>%
  ggplot(aes(Antibody, Mean, fill = metric, color = Sample)) +
  facet_wrap(~CellLine, scales = 'free_x') +
  geom_bar(stat = "identity", position = "dodge", size = 0.1) +
  geom_errorbar(aes(ymin=Mean - SD , ymax=Mean + SD), width=.3,
                position=position_dodge(.9)) +
  scale_color_manual(values = c("ADAR062" = "black", "ADAR112" = "black",
                                "ADAR004" = "black")) +
  scale_fill_manual(values = c("Alu" = "#125863",
                               "L1" =  "#2BA8B3")) +
  scale_y_continuous(labels = scales::percent) +
  guides(color = F) +
  theme_minimal() +
  labs(y = '', x = '', color = '', fill = 'Rep element',
       title = "Intersection of ADAR1 IP peaks and repetetive elements")

【讨论】:

  • 谢谢!这正是我需要的,你今天救了我。与我的代码相比,这段代码要直观得多。我从中学到了很多。
  • 好东西,乐于助人! pivot_longerpivot_wider 非常强大,但学习起来可能有点棘手。如果它回答了你的问题,你能接受这个答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 2022-10-03
  • 1970-01-01
  • 2016-01-04
相关资源
最近更新 更多