Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define (and plot) a non-continuous function in R?

Tags:

r

ggplot2

Suppose we have the function y = x^2, we could plot this continuous function from x=0 to x=100 like so:

library("ggplot2")
eq = function(x){x^2}
ggplot(data.frame(x=c(1, 100)), aes(x=x)) + 
  stat_function(fun=eq) +
  theme_void()

ggplot2 plot of a continuous function

Question

But suppose we wish to define a non continuous function, such that, say, it's the same as y=x^2 but there are no values for x between 20 and 30, and 50 and 70. How could we define it?

Context

I'm trying to replace the second line below (eq = function(x){x*x}) with whatever is necessary in order to plot noncontinuous function (i.e. keeping all other code the same).

library("ggplot2")
eq = function(x){x*x} # CHANGE ONLY THIS LINE (IF POSSIBLE)
ggplot(data.frame(x=c(1, 100)), aes(x=x)) + 
  stat_function(fun=eq) +
  theme_void()

I'll be trying to plot many non continuous functions on the same plot, so I suspect any hacks on the plot itself (e.g. adding elements over the top of the continuous function) would not scale well..

like image 509
stevec Avatar asked Dec 29 '21 10:12

stevec


People also ask

How do you define a discontinuous function?

A discontinuous function is a function that has a discontinuity at one or more values mainly because of the denominator of a function is being zero at that points. For example, if the denominator is (x-1), the function will have a discontinuity at x=1.

How do you make a function discontinuous?

To draw a graph of a function that is discontinuous, once we put the pen down to draw the graph, we must pick it up at least once before the graph is complete and then continue to draw again. A discontinuous function has breaks or gaps on its curve. Hence, the range of a discontinuous function has at least one gap.

How to plot a function in R?

How to plot a function in R? Plotting a function in R is not a difficult task. We can do it simply with curve function but if the function is very complex then it inside curve function might be difficult.

How to calculate cosine value in R plot?

To create a line plot, pass the parameter type = “l” inside the plot function. And we get the line chart of the y = x^3 function. If we don’t pass the type = “l” in the argument, it will return the points plot. By default, the plot () function returns point plot. To calculate the cosine value in R, use the cos () function.

Is it difficult to plot a function inside a curve function?

We can do it simply with curve function but if the function is very complex then it inside curve function might be difficult. It totally depends on the understand of the person who wants to plot the function, if he or she is well versed with the function then it won’t take much time, otherwise it becomes tedious.

What is the basic R function syntax?

The basic R function syntax is as follows: function_name <- function(arg1, arg2, ... ) { # Code } arg1, arg2, ... are the input arguments. # Code represents the code to be executed within the function to calculate the desired output. The output of the function can be a number, a list, a data.frame, a plot, a message or any object you want.


1 Answers

You can just replace the output of your function with NAs for values of x that aren't part of your domain.

library(ggplot2)

eq <- function(x) {
  ans <- x * x
  ans[x >= 20 & x <= 30] <- NA
  ans[x >= 50 & x <= 70] <- NA
  ans
}

ggplot(data.frame(x = c(1, 100)), aes(x)) +
  stat_function(fun = eq) +
  theme_void()

Created on 2021-12-29 by the reprex package (v2.0.1)

like image 127
teunbrand Avatar answered Nov 12 '22 19:11

teunbrand