Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think there are several options, I show in order of my preference.</p> <pre><code>## setup require(ggplot2) set.seed(10) # make reproducible dat &lt;- data.frame(x=1:10,y=runif(10)) </code></pre> <p>My favorite option is to create a simple function wrapper to your code. Then whenever you need to change the data, just pass new data to your function, and it will give it to <code>ggplot</code> and create the new graph. This is flexible and fairly robust to problems. It is also extensible, in that if later you decide you would also like to be able to change the title, you can just add a title argument to your function too.</p> <pre><code>## my favorite option myplot &lt;- function(data) { ggplot(data, aes(x, y)) + geom_point() } ## use it myplot(data = dat) </code></pre> <p><img src="https://i.stack.imgur.com/Y43R1.png" alt="first plot"></p> <pre><code>## change it dat &lt;- data.frame(x = 11:20, y = runif(10)) myplot(data = dat) </code></pre> <p><img src="https://i.stack.imgur.com/K2eto.png" alt="second plot"></p> <p>Another approach is to save your call to <code>ggplot</code> as an expression, which is unevaluated. Then you just evaluate it whenever you want. It is almost like typing the code each time (it is different in some ways but that is the best analogy I can think of).</p> <pre><code>## not wild about this one myplotcall &lt;- expression(ggplot(dat, aes(x,y)) + geom_point()) ## look at it (literally just the input) myplotcall expression(ggplot(dat, aes(x, y)) + geom_point()) ## use it eval(myplotcall) </code></pre> <p><img src="https://i.stack.imgur.com/G0vO3.png" alt="third plot"></p> <pre><code>## change it dat &lt;- data.frame(x = 21:30, y = runif(10)) eval(myplotcall) </code></pre> <p><img src="https://i.stack.imgur.com/BHGSe.png" alt="fourth plot"></p> <p>You can change the data in the <code>ggplot</code> object itself. I think that this approach would be the most prone to problems as you are mucking with internals of an object that was not really intended to be changed by the user (i.e., just because we can does not mean we should). This is more appropriately done with the <code>%+%</code> operator (see joran's answer)</p> <pre><code>## not wild about this either g &lt;- ggplot(dat, aes(x,y)) + geom_point() g ## use it </code></pre> <p><img src="https://i.stack.imgur.com/u7cS9.png" alt="fifth plot"></p> <pre><code>## change it dat &lt;- data.frame(x = 31:40, y = runif(10)) g$data &lt;- dat g </code></pre> <p><img src="https://i.stack.imgur.com/4WOe2.png" alt="sixth plot"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload