sagar
sagar

Reputation: 65

How to generate boxplot

I have a data and from that, I want to generate boxplot. My file is saved in "1.txt" file and it seems like this

R  S1G1   S1G2   S2G1   S2G2
1  0.98   0.98   0.96   0.89
2  0.89   0.89   0.98   0.88
3  0.88   0.99   0.89   0.87

I am using this code:

x<-read.table("1.txt", header=T)

boxplot(R~S1G1, data=x, main = "Output result",las = 2, pch=16, cex = 1,  
        col = "lightblue", xlab = "R",ylab = "SNP values",ylim =c(-0.4,1.0), 
        border ="blue", boxwex = 0.3)

can anyone tell me how to generate boxplot in R?

Upvotes: 2

Views: 15802

Answers (3)

katcho
katcho

Reputation: 1

After reading this post i found my solution to be sticking the table in data.frame(). Using the above example:

Xtab <- data.frame(x)
boxplot(Xtab$Freq ~ Xtab$Var1)

Upvotes: -1

joran
joran

Reputation: 173537

Your comments are a little tough to decipher, but I'm guessing that maybe you wanted a boxplot for each column S1G1, etc. In that case I'd melt your data:

xx <- read.table(textConnection("R  S1G1   S1G2   S2G1   S2G2
1  0.98   0.98   0.96   0.89
2  0.89   0.89   0.98   0.88
3  0.88   0.99   0.89   0.87"),header = TRUE, sep ="")

xx1 <- melt(xx, id.vars = "R")

and then you can make side-by-side boxplots using any of the popular graphing idioms:

ggplot(xx1, aes(x = variable, y = value)) + 
    geom_boxplot()

enter image description here

Or you can use base graphics or lattice (plots omitted):

boxplot(value~variable, data = xx1)

bwplot(value~variable,data = xx1)

Upvotes: 3

Alex
Alex

Reputation: 929

Maybe you want to reshape your data first:

x1 <- reshape(x, idvar="R", varying=list(2:5), direction="long")

And than plot it:

boxplot(S1G1 ~ R, data=x1, main = "Output result",las = 2, pch=16, cex = 1,
    col = "lightblue", xlab = "R",ylab = "SNP values",ylim =c(-0.4,1.2), 
    border ="blue", boxwex = 0.3)

boxplot

Upvotes: 0

Related Questions