anikaM
anikaM

Reputation: 429

How to add second axis on ggplot?

I am doing Manhattan plot for 2 phenotypes and therefore I am melting data for columns GWAS and GTEX in my dataframe which looks like this:

   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
...

The problem is that GTEX data is much smaller than GWAS so I would need to have two y axis to represent them.

I am suppose to use something like this:

scale_y_continuous(sec.axis = sec_axis...

but I am unsure how to implement that in my case.

right now this is my code:

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)

I need to have 2 Y axis one for GWAS and another one for GTEX. GTEX values are much smaller than those for GWAS.

I plotted with the code above this and it looks like this:

a busy cat ![two muppets][1]

UPDATE

I tired to use locus.zoom() from ggforce library but still results is not good. How do I get just the zoomed GWAS values?

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)

a busy cat ![one muppet][1]

UPDATE

per suggestion bellow I did:

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")
  )

But I don't know how to integrate in this these two 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)+

If I use it with the above code I am getting this error:

 Error: geom_point requires the following missing aesthetics: x, y

I tried doing it like this but nothing happens:

ggplot(tmp.tidy) +
  geom_count(aes(pos.end, -log10(value), color = key)) +
  facet_wrap(~key, scales = "free") +
  guides(size = FALSE) +
  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(aes(x = pos.end, y = -log10(value), label = pos.end), size = 2)
  theme(
panel.background = element_rect(fill = "white", color = "grey90"),
panel.spacing = unit(2, "lines")
  )

Upvotes: 1

Views: 318

Answers (1)

yake84
yake84

Reputation: 3236

I tried to approximate your problem with the diamonds dataset. Could you add an identifier in your data and then use facet_wrap() on it?

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

To update your code per the conversation below you would use

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
)

Upvotes: 1

Related Questions