简体   繁体   中英

Change x-axis values and background colour of a plot by group

I have to make a plot of the value of the last column of the following randomly generated dataset:

l <- seq(from=0.2, to=0.3, by=0.02)
output <- as.data.frame(matrix(NA,length(l)*4, 3))
colnames(output) <- c("G", "l", "Value")
output$G <- rep(2:5, each=length(l))
output$l <- rep(l, 4)
output$Value <- rnorm(nrow(output))
plot(output$Value, type="l")  

The plot looks like this now

在此处输入图像描述

But I would like, if it is possible, to have on the x-axis the values of the column "l", which is just a vector repeated four times. Regarding the backgroung I was wondering if it is possible to divide the plot into four bins corresponding to the four different values of the column G (eg, by alternating the colours of the background and adding a legend explaining the corresponding value of G).

You may plot an empty plot first with no axes, then build it along a seq of the index values. We need mtext() to get a smaller cex= . With help of the par()$usr we can define the borders accurately which we may finally put in rect() .

plot(output$Value, type="l", axes=F)  
axis(1, seq(nrow(output)), labels=F)
mtext(output$l, 1, .5, at=seq(nrow(output)), cex=.75)
axis(2)
box()
p <- par()$usr
v <- c(0, with(output, seq(G)[diff(G) == 1]), p[2])
sapply(seq(v)[1:length(v) - 1], with(output, function(f) {
  rect(v[f], p[3], v[f + 1], p[4], col=f + 1)}))
lines(output$Value)  

在此处输入图像描述

However, that's a rather unusual approach, and I reccomend to use a single panel for each G, something like that below. This also eliminates the need for tinkering with the axes.

rg <- range(output$Value)

op <- par(mfrow=c(1, 4))
lapply(unique(output$G), function(x) {
  with(output[output$G == x, ], 
       plot(l, Value, type='l', main=paste0("G = ", x), ylim=rg))
  p <- par()$usr
  rect(p[1], p[3], p[2], p[4], col=x)
  with(output[output$G == x, ], lines(l, Value))
})
par(op)

在此处输入图像描述


Data:

output <- structure(list(G = c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L), l = c(0.2, 
0.22, 0.24, 0.26, 0.28, 0.3, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 
0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.2, 0.22, 0.24, 0.26, 0.28, 
0.3), Value = c(1.37095844714667, -0.564698171396089, 0.363128411337339, 
0.63286260496104, 0.404268323140999, -0.106124516091484, 1.51152199743894, 
-0.0946590384130976, 2.01842371387704, -0.062714099052421, 1.30486965422349, 
2.28664539270111, -1.38886070111234, -0.278788766817371, -0.133321336393658, 
0.635950398070074, -0.284252921416072, -2.65645542090478, -2.44046692857552, 
1.32011334573019, -0.306638594078475, -1.78130843398, -0.171917355759621, 
1.2146746991726)), row.names = c(NA, -24L), class = "data.frame")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM