0

I know how to plot multiple functions on one graph,

  > var1 <- function(x) x^2
  > var2 <- function(x) x*(log(x)^2)
  > var3 <- function(x) x*log(x)
  > plot(var1,var2,var3, type="l",col="blue",xlim=c(0,40),
  + xlab="X-axis",ylab="Y-axis", add=TRUE)

How to plot these together with points when x = 2, 4, 8, 16, and 32 on each functions?

3 Answers 3

1

Here's an approach:

xs <- c(2, 4, 8, 16, 32)
curve(var1, col = "blue", xlim = c(0, 40), xlab = "X-axis", ylab = "Y-axis")
curve(var2, col = "blue", add = TRUE)
curve(var3, col = "blue", add = TRUE)
points(xs, var1(xs), col = "blue")
points(xs, var2(xs), col = "blue")
points(xs, var3(xs), col = "blue")

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

neat! this gives the points only. i guess i can use lines if i want points with functions all together?
1

Here's a ggplot2 approach.

library(ggplot2)
x <- c(2, 4, 8, 16, 32)
var1 <- x^2
var2 <- x * log(x) ^ 2
var3 <- x * log(x)
df <- data.frame(x = rep(x, times = 3), y = c(var1, var2, var3), var = rep(c("var1", "var2", "var3"), each = length(x)))
ggplot(df, aes(x = x, y = y, color = var)) + geom_line()

Replace geom_line() with geom_point() if you want points instead of lines, and add + geom_point() at the end if you want points and lines.

enter image description here

1 Comment

TO get points as well add + geom_point() to @blakeoft's answer: ggplot(df, aes(x = x, y = y, color = var)) + geom_line() + geom_point()
1
var1 = function(x) x^2
var2 = function(x) x*(log(x)^2)
var3 = function(x) x*log(x)

curve(var1,0,40)
curve(var2,add = T, col="red")
curve(var3,add = T, col="blue")

x=c(2, 4, 8, 16, 32)

points(x,var1(x))
points(x,var2(x), col="red")
points(x,var3(x), col="blue")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.