2014-03-06 100 views
0

我正在創建一個腳本,它允許我從數據庫中獲取數據,並在圖形中將其可視化。 enter image description hereX軸上每組點的標籤

正如您所看到的,有8組數據,在X軸上顯示。每個組總是包含90個值。

眼下,標籤的地方是硬編碼是這樣的:

axis(1, at = c(31.25, 93.75, 156.25, 218.75, 281.25, 343.75, 406.25, 468.75), 
    labels = c("ss oligo 1", "ss oligo 2", "ss oligo 3", "ss oligo 4", 
       "ss oligo 4", "ss oligo 5", "ss oligo 6", "ss oligo 7")) 

這工作完全正常,但我不知道是否有一種方法可以更動態地做到這一點,通過只是告訴[R爲每組90個值分配一個標籤。

例子:

# Generate data ################################################################################### 
x <- vector() 
y <- vector() 

# y[length(y)+1] <- sample(10:12, 1, replace = TRUE) 
oligo_1 <- runif(62, 10.5, 11.5) 
oligo_2 <- runif(62, 14, 15) 
oligo_3 <- runif(62, 17, 18) 
oligo_4a <- runif(64, 20.5, 22) 
oligo_4b <- runif(64, 20.5, 22) 
oligo_5 <- runif(62, 24, 25) 
oligo_6 <- runif(62, 27, 28) 
oligo_7 <- runif(62, 30, 31) 

y <- c(oligo_1, oligo_2, oligo_3, oligo_4a, oligo_4b, oligo_5, oligo_6, oligo_7) 
x <- c(1:500) 

example <- data.frame(x, y) 

# Define variables ################################################################################ 
xmin  <- 10 
xmax  <- 36 

# Generate graph ################################################################################### 
png(filename = "graph.png", width = 1500, height = 833) 

plot(x = example[,2], type="l", 
    xlim = c(0, nrow(example)), ylim = c(xmin, xmax), 
    xaxt="n", yaxt="n", 
    xlab = "", ylab = "") 

rect(xleft = par("usr")[1], ybottom = 9.8, xright = par("usr")[2], ytop = 12.2, border = "lightgrey", col = "lightgrey") 
rect(xleft = par("usr")[1], ybottom = 13.2, xright = par("usr")[2], ytop = 15.5, border = "lightgrey", col = "lightgrey") 
rect(xleft = par("usr")[1], ybottom = 16.5, xright = par("usr")[2], ytop = 18.9, border = "lightgrey", col = "lightgrey") 
rect(xleft = par("usr")[1], ybottom = 19.9, xright = par("usr")[2], ytop = 22.3, border = "lightgrey", col = "lightgrey") 
rect(xleft = par("usr")[1], ybottom = 23.3, xright = par("usr")[2], ytop = 25.5, border = "lightgrey", col = "lightgrey") 
rect(xleft = par("usr")[1], ybottom = 26.5, xright = par("usr")[2], ytop = 28.7, border = "lightgrey", col = "lightgrey") 
rect(xleft = par("usr")[1], ybottom = 29.7, xright = par("usr")[2], ytop = 32.1, border = "lightgrey", col = "lightgrey") 

axis(1, at = c(31.25, 93.75, 156.25, 218.75, 281.25, 343.75, 406.25, 468.75), 
    labels = c("ss oligo 1", "ss oligo 2", "ss oligo 3", "ss oligo 4", 
       "ss oligo 4", "ss oligo 5", "ss oligo 6", "ss oligo 7")) 
axis(2, at = c(11, 14.35, 17.7, 21.1, 24.4, 27.6, 30.9), las = 1) 

lines(x = example[,2]) 
box() 

mtext(paste("QC-check for", "TEST"), side = "3", line = 1, cex = 2, font = 2) 
mtext("Samples"      , side = "1", line = 3, cex = 1, font = 1) 

legend(x = par("usr")[1]+10, y = par("usr")[4]-1, legend = c("Cq", "Ccq"), cex=1.5, lwd = 2, col = c("black","red")) 

dev.off() 
+0

如果您提供一個[**最小的自包含示例**],它將更容易回答您的問題(http://stackoverflow.com/questions/5963269/how-to-make-a-大-R-重複性,例如/ 5963610#5963610)。 – Henrik

+0

@ Hendrik:添加了示例 – Fingashpitzzz

回答

0

好吧,我把它變得更加困難比它。因此,與@ user449060的答案組合在一起,我將代碼更改爲:

count <- nrow(data)/8 
axis(1, at = count/2+(0:7)*count, labels = paste("ss oligo",c(1:4, 4, 5:7))) 

這使它更具動態性!

1

爲什麼不:

axis(1, at=31.25+(0:7)*62.5, labels=paste("ss oligo",1:8)) 
+0

這使得它更簡單,謝謝! – Fingashpitzzz