Draw Plot with Two Y-Axes in R (Example) | axis, plot, par, mtext, range & pretty Functions

preview_player
Показать описание
R code of this video:

x <- rnorm(30)
y1 <- x + rnorm(30)
y2 <- x + rnorm(30, 5)

par(mar = c(5, 4, 4, 4) + 0.3) # Additional space for second y-axis
plot(x, y1, pch = 16, col = 2) # Create first plot
par(new = TRUE) # Add new plot
plot(x, y2, pch = 17, col = 3, # Create second plot without axes
axes = FALSE, xlab = "", ylab = "")
axis(side = 4, at = pretty(range(y2))) # Add second axis
mtext("y2", side = 4, line = 3) # Add second axis label
Рекомендации по теме
Комментарии
Автор

Easy to listen to you, easy to understand, good plain english. Congratulations

jorgemanuelazevedosantos
Автор

My go to place for R-codes when I get stuck. Thanks for the help!

WaTrBuFFaL
Автор

Hi, is there a way to add a lowess line (or similar to join all the points up) on the second graph on the second y axis please?

luciekneller
Автор

super heroes don't wear capes thank you for your help

mjt
Автор

But, if i want the same scale values in both y-axes, how can i do it?

ssstriplekids
welcome to shbcf.ru