two dependent variables with plot function

Hello everyone!

I am currently trying to reconstruct/imitate a plot that I have seen in a paper. I have managed it quite well. However, I have three problems that I cannot get resolved. I hope that someone of you can help me.

So, the final plot should look something like this:

The code that I have used is :

plot(if_uebe$i.infitMSQ,type ="b", xlab = "Items der Skala UEBE", ylab = "Infit MSQ", 
     ylim = c(0.2, 1.8), 
     pch =ifelse((if_uebe$i.infitZ < 0| if_uebe$i.infitZ > 2), 1, 16))
abline(h= 1.0, col=c("black"), lty=5, lwd=1)
abline ( h = 1.3, col = "grey", lty=5, lwd=1)
abline ( h = 0.7, col = "grey", lty=5, lwd=1)

My first question is: How can I include two dependent variables instead of just one as it has been visualised in the plot from the paper (grey and black lines)?

The second aspect that I can't figure out is how to add customized x labels instead of 1,2,3 ... as it is the case with my plot. My X variable is unordered categorical, so customized labels such as "item 3", "item 10" on the x axis would make sense.

The third and last issue is that I want to use the ifelse() function concerning the point character. I want to make pch = 16 if they are smaller than -2 or greater than +2. Concerning the last condition (+2) there is no problem. The problem emerges when I want to include the negative number. Does someone of you know how to include the minus sign ( - ). I have tried it out with | -2 | but it still does not work.

Any help would be highly appreciated. Thank you :slight_smile:

Take look at ?lines and ?points. These are lower level functions that allow you to add elements to an existing plot.

In this plot, I plot the same variable twice, but one is shifted up, and added using lines. In this case I make sure to control the ylim otherwise some points may be plotted outside the plotting area

plot(demand ~ Time, BOD, type = 'b', pch = 19, ylim = c(8, 22))
lines(demand + 1 ~ Time, BOD, type = 'b', col = 2, pch = 19)


Sometimes a good approach is to use type = 'n' to make a blank plot then add in everything you need afterwards

1 Like

Thank you very much @jmcvw. Your answer has helped me a lot :slight_smile:

Do you also know how I can include the minus (-) sign if I want to use for example the following:

pch =ifelse((if_uebe$i.infitZ < 0, 1, 16)) 

Instead of < 0, I would like to use - 2.

Thanks and all the best :slight_smile:

Hi @lupo,

I'm afraid it's not clear to me what you need. A reprex would be very helpful, please consider providing one if this doesn't help.

Help pages you should read if you don't already know them:
Armed with these, you should be able to build up the plot as desired. Alternatively, check out package ggplot2.

I have given a plot below which as far as I can tell comes close to your situation.

BOD$Time <- as.factor(BOD$Time)
BOD$demand2 <- BOD$demand + 1

plot(BOD$Time, type = 'n', ylim = c(8, 22), axes = FALSE)
#> Warning in plot.window(xlim, ylim, log = log, ...): graphical parameter "type"
#> is obsolete
#> Warning in title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...):
#> graphical parameter "type" is obsolete
lines(demand ~ Time, BOD, type = 'b', col = 1,
      pch = ifelse(demand < 16, 15, 0))
lines(demand + 1 ~ Time, BOD, type = 'b', col = 2,
      pch = ifelse(demand < 16, 16, 1))
axis(side = 1, at = c(1:6), labels = paste('label', 1:6))


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.