2011-08-22 69 views
2

我有一个数据,并从中,我想生成boxplot。我的文件保存在“的1.txt”的文件,好像这个如何生成boxplot

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 

我使用这个代码:

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) 

谁能告诉我如何生成中的R箱图?

回答

1

也许你想先重塑你的数据:

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

而且比绘制它:

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

3

您的意见是有点困难破译,但我猜测,也许你想为每列S1G1等箱线图。在这种情况下,我会融化你的数据:

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

,然后你可以使用任何流行的图形成语并排侧箱线图:

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

enter image description here

或者你可以使用基础图形或lattice(略图):

boxplot(value~variable, data = xx1) 

bwplot(value~variable,data = xx1) 
-1

看完这篇文章后,我发现我的解决方案是坚持data.frame()中的表。使用 上面的例子:

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

如果传递的数据帧到boxplot(),它将自动创建各列的箱线图。因此,它可以很简单地使用

boxplot(x[,-1]) 

注意,-1是除去第一列,这是不是在想情节来完成。

enter image description here

的数据是

x <- 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 ="") 
+0

我很感谢谁下投票,如果他们可以发表评论来解释为什么,或者我应该在这个岗位提高。这在我看来是这里提供的最简单的解决方案,并且完美地工作! – dww