如何在ggplot上添加第二轴?

时间:2019-05-10 23:02:37

标签: r ggplot2

我正在为2个表型绘制曼哈顿图,因此我在数据框中融合了GWAS和GTEX列的数据,如下所示:

"https://something.com/share/"

问题是GTEX数据比GWAS小得多,因此我需要有两个y轴来表示它们。

我想使用这样的东西:

"https://something.com/share/"+id

但是我不确定如何在我的情况下实现。

现在这是我的代码:

   pos.end       GWAS        GTEX
1 16975756 0.71848040 2.82508e-05
2 16995937 0.02349431 4.54958e-11
3 17001098 0.04310933 1.93264e-20
4 17001135 0.04354486 8.52552e-21
5 17002964 0.02352996 1.84111e-15
6 17005677 0.01046168 2.09734e-11
...

我需要2个Y轴,一个用于GWAS,另一个用于GTEX。 GTEX值远小于GWAS。

我用上面的代码进行绘制,看起来像这样:

a busy cat ![两个木偶] [1]

更新

我厌倦了使用ggforce库中的locus.zoom(),但结果仍然不好。如何只获取缩放的GWAS值?

scale_y_continuous(sec.axis = sec_axis...

a busy cat ![一个布偶] [1]

更新

按照我的建议,

library(dplyr)
library(ggplot2)
library(tibble)
library(ggrepel)

snpsOfInterest = c("17091307")

tmp = read.table("nerve_both_manh", header=T)
tmp.tidy <- tmp %>% 
  tidyr::gather(key, value, -pos.end) %>%
  mutate(is_highlight = ifelse(pos.end %in% snpsOfInterest, "yes", "no")) %>%
  mutate(is_annotate = ifelse(-log10(value) > 5, "yes", "no"))

ggplot(tmp.tidy, aes(pos.end, -log10(value), color = key)) +
  geom_point(data = subset(tmp.tidy, is_highlight == "yes"), 
             color = "purple", size = 2)+
  geom_label_repel(data = subset(tmp.tidy, is_annotate == "yes"), 
                   aes(label = pos.end), size = 2)

但是我不知道如何整合这两行:

ggplot(tmp.tidy, aes(pos.end, -log10(value), color=key)) +
  facet_zoom(xy = key == "GWAS")+
  geom_point(data=subset(tmp.tidy, is_highlight=="yes"), color="purple", size=2)+
  geom_label_repel( data=subset(tmp.tidy, is_annotate=="yes"),  aes(label=pos.end), size=2)

如果在上面的代码中使用它,则会出现此错误:

ggplot(tmp.tidy) +
  geom_count(aes(pos.end, -log10(value), color = key)) +
  facet_wrap(~key, scales = "free") +
  guides(size = FALSE) +
  theme(
    panel.background = element_rect(fill = "white", color = "grey90"),
    panel.spacing = unit(2, "lines")
  )

我试图这样做,但是什么也没发生:

  geom_point(data=subset(tmp.tidy, is_highlight=="yes"), color="purple", size=2)+
  geom_label_repel( data=subset(tmp.tidy, is_annotate=="yes"), aes(label=pos.end), size=2)+

1 个答案:

答案 0 :(得分:1)

我试图用diamonds数据集来估算您的问题。您可以在数据中添加一个标识符,然后在其上使用facet_wrap()吗?

df <-
  diamonds %>% 
  slice(1:2000) %>% 
  filter(price < 400 | price > 3000) %>% 
  mutate(type = ifelse(price < 500 & row_number() < 100, "GWAS", "GTEX"))


ggplot(df) +
  geom_count(aes(table, price, color = type)) +
  facet_wrap(~type, scales = "free") +
  guides(size = FALSE) +
  theme(
    panel.background = element_rect(fill = "white", color = "grey90"),
    panel.spacing = unit(2, "lines")
  )

enter image description here

要在下面的每次会话中更新代码,请使用

geom_point(
  data = subset(tmp.tidy, is_highlight == "yes"), 
  aes(x = pos.end, y = -log10(value)),
  color = "purple", size = 2
 ) +
geom_label_repel(
  data = subset(tmp.tidy, is_annotate == "yes"), 
  aes(x = pos.end, y = -log10(value), label = pos.end), 
  size = 2
)
相关问题