0

I made a graph in which a barplot and a line plot are combined. The problem is that the scale of my secondary y-axis isn't how it should be.

This is the code I used:

barplot <- barplot(covpatient[[1]]$cov, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = gray.colors(length(unique(covpatient[[1]]$exon)))[as.factor(covpatient[[1]]$exon)])
par(new = TRUE)
lines(x = barplot, y = covpatient[[1]]$amplicon, bty = "n")
axis(side = 4, at = pretty(range(covpatient[[1]]$amplicon)))

And this is how my plot looks like: plot

The values of the lines plot are OK, but you see that the y-axis is not fully expanded. I want it to look the same as the y-axis on the left

Can someone help me with this?

user1987607
  • 2,057
  • 6
  • 26
  • 53
  • Think it is not possible to adapt the axes with the lines command independently. Have a look at the answer you got on your earlier (but closed) question, that will help you forward. – thijs van den bergh Feb 06 '13 at 10:54
  • i tried to do it with twoord.plot() function from the plotrix package. However I definitly need different colors for certain groups in the barplot. I saw that you can specify a color with the lcol function. But when I made a normal barplot, i specified the colors using this code: col = rainbow(length(unique(covpatient[[i]]$exon)))[as.factor(covpatient[[i]]$exon)] I tried to put this in the twoord.plot function, but I just get the standard black color ... – user1987607 Feb 06 '13 at 15:25
  • read the documentation! also read: [link](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and as an answer to your question: use `lcol=` instead of `col=` – thijs van den bergh Feb 06 '13 at 18:35
  • I in fact used lcol & that didn't work. If my answer would be in the documentation, I would not ask it here ... – user1987607 Feb 07 '13 at 10:53

1 Answers1

0

Without a reproducible example and a clear question all answers will at best be gueswork but have a look at the following:

x <- 1:10
y <- c(1, 3, 5, 6, 2, 7, 11, 3, 2, 13)
z <- runif(10, min=1000, max=10000) 

par(mar = c(5, 4, 4, 4) + 0.3) 
barplot(y, col=rainbow(length(x)))
par(new = TRUE)
plot(x, z, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "")
axis(side=4, at = pretty(range(z)))
mtext("z", side=4, line=3)

library(plotrix)
twoord.plot(x,y,x,z,
    xlab="x",
    ylab="y",
    rylab="z",
    main="Main",
    type=c("bar","l"),lcol=rainbow(length(x)),rcol=4)
thijs van den bergh
  • 608
  • 1
  • 9
  • 17