This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Plotting matrix of values around specific genomic position

Hi I have a data.frame that looks like the following:

    V1       V2            V3        V4
5022970 196.2992494 191.5615348 186.9297418
5023003 98.0686565  97.03803578 96.12656665
5023061 68.8391627  69.53623005 70.76395006
5023121 96.98746392 97.94463568 98.81860247

The first column is a set of genomic locations and columns extending from V2 to V402 are values I want to plot, now I can do this for two columns (col1 = V1, col2 = V2) as follows:

df %>% ggplot() + 
  geom_point(aes(x = V1, y = V2)) +
  scale_y_continuous(limits=c(0,200)) +   
  xlim(5022800, 5023050) + xlab("Genomic Position") +  
  geom_smooth(aes(x = V1, y = V2), 
              method="loess", span=.22, color = "red", fill = "black") + 
  theme_bw()

However I am not sure how to plot all columns from V2-V402 relative to those specific genomic points. Thanks.

r data.frame matrix ggplot2

1 answer

Although not sure how informative this would be with 400 lines in one plot, we could reshape from wide to long, then plot:

library(data.table)
library(ggplot2)

#example data
dt <- fread(text = "    V1       V2            V3        V4
5022970 196.2992494 191.5615348 186.9297418
5023003 98.0686565  97.03803578 96.12656665
5023061 68.8391627  69.53623005 70.76395006
5023121 96.98746392 97.94463568 98.81860247")

#reshape wide-to-long
plotDat <- melt(dt, id.vars = "V1")

#plot
ggplot(plotDat, aes(x = V1, y = value, colour = variable)) +
  geom_point() +
  geom_smooth()

Log in to answer this question.