0

I'm am creating a script which allows me to get data from a database, and visualises it in a graph. enter image description here

As you can see, there are 8 groups of data, indicated on the X-axis. Each group always contains 90 values.

Right now, the place of the labels is hard-coded like this:

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

It works perfectly fine, but I was wondering if there is a way to do this more dynamically, by just telling R to assign a label to each set of 90 values.

Example:

# 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()
Fingashpitzzz
  • 169
  • 4
  • 20
  • It will be much easier to answer your question if you please provide a [**minimal, self-contained example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – Henrik Mar 06 '14 at 08:20
  • @ Hendrik: example added – Fingashpitzzz Mar 06 '14 at 09:16

2 Answers2

1

Why not:

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

Alright, I made it much more difficult than it was. So in combo with the answer from @user449060, I changed the code to:

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

which makes it a lot more dynamic!

Fingashpitzzz
  • 169
  • 4
  • 20