Reanimation
Reanimation

Reputation: 3346

Plotting a complex function in R

I'm self learning R using a book called "Discovering Statistics using R" and I'm playing around with making graphs as there's quite a lot to learn regarding programming the aesthetics.

The book doesn't really say more than this and I can't find an example on google of a function like the one below. So I'm wondering how to plot this function which is a bit more complex. The example I've got is this:

g(x)

I've looked through a few plotting questions on here but they don't cover a function like this.

I know how to create a simple plot and a legend etc, but how can this function be plotted in R?

Thanks in advance.

Upvotes: 0

Views: 530

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81753

# create a function
myfun <- function(x) 
  ifelse((0 <= x & x <= pi) | ((-2 * pi) <= x & x <= -pi), sin(x), -pi / 4)

# plot a curve
curve(myfun, from = -10, to = 10)

enter image description here

Upvotes: 3

Related Questions