2

I am working on this graph in R:

graph

However as you see I am getting these solid dashes instead of nice lines. Here is the code I used to make this graph:

par(mar = c(5,5,2,5))
    with(bedtimes, plot(trap, funestus, type="l", col="red3", 
                 ylab=expression(italic(p))),
                 ylim=c(0,3))  

    par(new = T)
    with(bedtimes, plot(trap, bed, pch=16, axes=F, xlab=NA, ylab=NA, cex=1.2))
    axis(side = 4)
    mtext(side = 4, line = 3, 'Proportion in bed')
    polygon(bedtimes$bed,col=transp("gray", 0.3), border = NA)

And here is the dput of the data I am using:

(removed)

I realise that this is occurring because my x axis is a factor and not numeric. However, trying to change this (e.g. using as.POSIXct(paste0("2016-07-12",bedtimes$trap)) is causing me all sorts of problems, as I had to ake sure R plotted these factors in the correct order originally by using bedtimes$trap <- factor(bedtimes$trap, levels = bedtimes$trap)

How can I produce this same graph but with lines instead of these dashes? I eventually want a graph that looks similar to this, to give you an idea (though not exactly the same):

graph

Thank you!

Samuel Harper
  • 399
  • 1
  • 2
  • 13

1 Answers1

3

As I said in comments, you can try to use the levels of your factor variable:

par(mar = c(5,5,2,5))
with(bedtimes, plot(as.numeric(trap), funestus, type="l", col="red3", 
                    ylab=expression(italic(p))),
                    ylim=c(0,3))  
par(new = T)
with(bedtimes, plot(as.numeric(trap), bed, type="l", axes=F, xlab="", ylab=NA, cex=1.2))
axis(4)
axis(side = 1, at=1:length(levels(bedtimes$trap)), levels(bedtimes$trap))
mtext(side = 4, line = 3, 'Proportion in bed')
polygon(bedtimes$bed,col="gray", border = NA)

enter image description here

Cath
  • 23,906
  • 5
  • 52
  • 86
  • 2
    Thanks, but this removes the y axis on the left hand side and the line I had for funestus. I'm looking to create a plot like the one in the example picture with the two y axis and the separate plots – Samuel Harper Jul 13 '16 at 14:59
  • 1
    @SamuelHarper sorry, I just didn't take the first part of your code. The image is not exactly the one you'll have using your parameter of transparancy – Cath Jul 13 '16 at 15:16
  • 1
    That is great thank you! If I could ask you one more thing (and this is completely optional since you have already answered my question!) How do I rotate my axis labels so they are easier to read? As they are a bit too long to fit horizontally. – Samuel Harper Jul 13 '16 at 15:24
  • 1
    With parameter las ;-) – Cath Jul 13 '16 at 15:41