简体   繁体   中英

Display a function that is seperated in intervals and plot as piecewise constant graph

at first I am sorry, that I can't describe my problem that well, I hope you understand.

What I have is a mathematical function in a graph (picture one), what I want to describe is a process in which I have used that graph. First I divided the whole thing in intervals, as seen in the second picture. Than I wrote a program that iterated each interval and called the function at the beginning of each interval and returned a rough and a rounded value. The interval frequency is set for an experiment but can be easily adjusted. Now I got a set of rounded numbers equal the number of intervals that I want to display in an angular graph as seen in the third picture.

在此处输入图片说明

I am not sure if this three graphs describe my procedure or if this is a common problem with a simple solution or not. I use rstudio as a tool to describe that, and i have a bit experience with ggplot2, but I am open minded if you suggest me to use a different library or approach.

Here is some example data for the function (-0.06x^3)+(0.43x^2)-x+3:

myTable <- "ID Data Rounded
        1     2.973  3
        2     2.976  3
        3     2.970  3
        4     2.978  3
        5     2.976  3
        6     2.973  3
        7     2.630  2.6
        8     2.630  2.6
        9     2.633  2.6
       10     2.632  2.6
       11     2.630  2.6
       12     2.273  2.3
       13     2.273  2.3
       14     2.273  2.3
       15     2.273  2.3
       16     2.179  2.2
       17     2.179  2.2
       18     2.179  2.2
       19     2.179  2.2
       20     2.179  2.2
       21     2.179  2.2
       22     2.179  2.2
       23     2.179  2.2
       24     2.179  2.2
       25     2.179  2.2
       26     2.179  2.2
       27     2.179  2.2
       28     2.179  2.2
       29     2.179  2.2
       30     2.179  2.2
       31     2.073  2.1
       32     2.073  2.1
       33     2.073  2.1
       34     2.073  2.1
       35     2.073  2.1
       36     2.073  2.1
       37     2.076  2.1
       38     2.073  2.1
       39     2.073  2.1
       40     1.886  1.9
       41     1.886  1.9
       42     1.886  1.9
       43     1.886  1.9
       44     1.886  1.9
       45     1.628  1.6
       46     1.628  1.6
       47     1.631  1.6
       48     1.628  1.6
       49     1.630  1.6
       50     1.628  1.6
       51     1.631  1.6
       52     1.631  1.6
       53     1.631  1.6"
Data <- read.table(text=myTable, header = TRUE)

If my understanding is correct, what you want is to plot a piecewise constant function.

In this case, since you are familiar with ggplot2, you can achieve it using geom_step() :

ggplot(Data) + geom_step(aes(x = ID, y = Rounded))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM