-1

I am representing the "nature" of an interaction amongst independent variables via a line graph, on this graph the nature of the interaction changes above and below the calculated value w0. Below this value, there is a buffering effect and above there is a magnification effect. I want to represent the magnification effect with a green line and the buffering effect with a red line.

When using a line, the line does not change colors according to the conditional. However, when I use points instead, the points change colors correctly but the points are so sparse that it is hard to measure the results.

enter image description here

enter image description here

I have played around with the ifelse() function trying to change the color of the line, but as stated previously, it does not work with the line only the points.

wLow = 0
w0   = 1.49
wHigh = 4


z = yaxis <- c(1,2,3)
t = xaxis <- c(wLow,w0,wHigh)

distPlot <- plot(t,z, type="b", lwd=5, pch=15, col = ifelse( t < 
w0,'red','green'), xlab="Moderator Range", ylab="Effect")

Optimally, I would like the line below w0 to be red and above to be green or if this is not possible, could we create a line of points of the correct color between the 3 given points?

M--
  • 25,431
  • 8
  • 61
  • 93
  • 1
    Duplicate of [Plot with conditional colors based on values in R](https://stackoverflow.com/questions/11838278/plot-with-conditional-colors-based-on-values-in-r) – M-- Jul 06 '19 at 16:37
  • I saw that post and the only solution provided there is using nested ifelse(), which I tried & explicitly stated did not work in my case. – Mateusz Gembarzewski Jul 06 '19 at 16:52
  • I'm still somewhat new to R, maybe I'm not understanding fully. In the link you provided, the plot() color is set using ifelse() while in d.b's example he is adding lines and points using the lines() & points() functions. I see no mention of those functions in the provided link? – Mateusz Gembarzewski Jul 06 '19 at 16:57
  • https://stackoverflow.com/questions/36775090/different-colors-for-lines-as-opposed-to-points-in-r – M-- Jul 06 '19 at 17:01
  • Sorry, I will try better in the future to make sure my issue wasn't solved already. I couldn't find the result with my search queries. – Mateusz Gembarzewski Jul 06 '19 at 17:06
  • No worries, finding dupes is not that easy. After a while you will know search for what. This is part of communities effort to point new users to the right thread. Cheers. – M-- Jul 06 '19 at 17:08

2 Answers2

1

One solution is to draw the line segments first, then add the points.
Note that a blank plot must be started first. This is done with plot(..., type = "n").

z0  <- c(1, 2)
t0  <- c(wLow, w0)
z1 <- c(2, 3)
t1 <- c(w0, wHigh)

plot(t, z,type = "n", xlim = range(t), ylim = range(z),
    xlab = "Moderator Range", ylab = "Effect")
segments(t0, z0, t1, z1, lwd = 5, col  = ifelse(t < w0, 'red', 'green'))
points(t, z, pch = 15, col  = ifelse(t < w0, 'red', 'green'))

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
1
graphics.off()
plot(t, z, type="n", xlab="Moderator Range", ylab="Effect")
lines(t[t <= w0], z[t <= w0], col = "red", lwd = 5)
lines(t[t >= w0], z[t >= w0], col = "green", lwd = 5)
points(t, z, pch = 19, cex = 2, col = "white")
points(t,z, pch=15, col = ifelse( t < w0,'red','green'))

enter image description here

d.b
  • 32,245
  • 6
  • 36
  • 77
  • 1
    Thank you d.b. Your example of using the lines() and points() functions was very helpful. I tried these functions previously, but was not able to obtain the correct results, thank you so much for your clarification and expertise. :) – Mateusz Gembarzewski Jul 06 '19 at 17:02