2

I am trying to create boxplot using R script from the following type of tab delimited file "New.txt" where the number of rows and columns will be variable

Chr Start   End Name    18NGS31 18MPD168    18NGS21 18NGS29 18NGS33 18NGS38
chr9    1234    1234    ABL1    1431    1   1112    1082    1809    1647
chr9    2345    2345    ASXL1   3885    37  3578    1974    2921    3559
chr9    3456    3456    ETV6    3235    188 2911    1578    2344    2673
chr9    4567    4567    MYD88   3198    187 2860    1547    2289    2621

After skipping first four columns create box plot in R from 5th column on wards using following commands

file <- "new.txt"
x=read.table(file,skip=1)
boxplot(x$V5,x$V6,x$V7,x$V9,x$V10,x$V11,col=rainbow(54),xlab="abc",ylab="Coverage",main="Coverage Metrics")

And I am getting following box plot

[![R ploy][1]][1]

I want to modify this command such that I can incorporate any number of columns that will be present in the tab delimited file and label each box plot as per its column head.

1 Answer 1

3

I recommend reshaping from wide to long .

Here is a minimal example using ggplot2

# Sample data
df <- data.frame(id = paste0("id", 1:100), matrix(rnorm(1000), ncol = 10))

library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
    gather(key, value, -id) %>%
    mutate(key = factor(key, levels = paste0("X", 1:10))) %>%
    ggplot(aes(x = key, y = value)) +
    geom_boxplot()

enter image description here

Explanation: Reshaping from wide to long stores the column names in a new column key and its values in value; we can then simply map key to x. This works for an arbitrary number of columns.


Update

Using your sample data

df <- read.table(text =
    "Chr Start   End Name    18NGS31 18MPD168    18NGS21 18NGS29 18NGS33 18NGS38
chr9    1234    1234    ABL1    1431    1   1112    1082    1809    1647
chr9    2345    2345    ASXL1   3885    37  3578    1974    2921    3559
chr9    3456    3456    ETV6    3235    188 2911    1578    2344    2673
chr9    4567    4567    MYD88   3198    187 2860    1547    2289    2621", header = T)

df %>%
    gather(key, value, -Chr, -Start, -End, -Name) %>%
    ggplot(aes(x = key, y = value, fill = key)) +
    geom_boxplot()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.