开发者

How to generate boxplot

开发者 https://www.devze.com 2023-03-29 15:41 出处:网络
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

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

开发者_StackOverflowR  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?


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

How to generate boxplot

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

boxplot(value~variable, data = xx1)

bwplot(value~variable,data = xx1)


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)

How to generate boxplot


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号