首页 > 解决方案 > 条形图上的误差线形状不正确

问题描述

我已经生成了这个图:

阴谋

但是,我的误差线没有正确定位(见图)。我尝试将 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"
)

标签: rggplot2

解决方案


下面做你想要的。我使用pivot_longer(较新版本的gather)将正确的 SD 和平均值收集在一起。在情节中,我添加color = Sampleggplot()调用,以确保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")

推荐阅读