1

I want to output two plots in a grid using the same function but with different input for x. I am using ggplot2 with stat_function as per this post and I have combined the two plots as per this post and this post.

f01 <- function(x) {1 - abs(x)}

ggplot() +
    stat_function(data = data.frame(x=c(-1, 1)), aes(x = x, color = "red"), fun = f01) +
    stat_function(data = data.frame(x=c(-2, 2)), aes(x = x, color = "black"), fun = f01)

With the following outputs:

Plot:

my output

Message:

`mapping` is not used by stat_function()`data` is not used by stat_function()`mapping` is not used by stat_function()`data` is not used by stat_function()

I don't understand why stat_function() won't use neither of the arguments. I would expect to plot two graphs one with x between -1:1 and the second with x between -2:2. Furthermore it takes the colors as labels, which I also don't understand why. I must be missing something obvious.

stuyka
  • 35
  • 5

2 Answers2

2

To be honest, I'm not really sure what happens here with ggplot and its inner workings. It seems that the functions are always applied to the complete range, here -2 to 2. Also, there is an issue on github regarding a wrong error message for stat_function.

However, you can use the xlim argument for your stat_function to limit the range on which a function is drawn. Also, if you don't specify the colour argument by a variable, but by a manual label, you need to tell which colours should be used for which label with scale_colour_manual (easiest with a named vector). I also adjusted the line width to show the function better:

library(ggplot2)

f01 <- function(x) {1 - abs(x)}

cols <- c("red" = "red", "black" = "black")

ggplot() +
  stat_function(data = data.frame(x=c(-1, 1)), aes(x = x, colour = "red"), fun = f01, size = 1.5, xlim = c(-1, 1)) +
  stat_function(data = data.frame(x=c(-2, 2)), aes(x = x, colour = "black"), fun = f01) +
  scale_colour_manual(values = cols)


enter image description here

starja
  • 9,887
  • 1
  • 13
  • 28
2

The issue is that according to the docs the data argument is

Ignored by stat_function(), do not use.

Hence, at least in the second call to stat_function the data is ignored.

Second, the

The function is called with a grid of evenly spaced values along the x axis, and the results are drawn (by default) with a line.

Therefore both functions are plotted over the same range of x values.

If you simply want to draw functions this can be achievd without data and mappings like so:

library(ggplot2)

f01 <- function(x) {1 - abs(x)}

ggplot() +
  stat_function(color = "black", fun = f01, xlim = c(-2, 2)) +
  stat_function(color = "red", fun = f01, xlim = c(-1, 1))

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Where in the docs can I find the part that `data` is ignored? On the page for [`stat_function`](https://ggplot2.tidyverse.org/reference/stat_function.html) I couldn't find it – starja Jun 23 '20 at 20:06
  • Type ?stat_function in the console. Then scroll down to the arguments. – stefan Jun 23 '20 at 20:07
  • ... or https://ggplot2.tidyverse.org/reference/geom_function.html?q=stat%20_%20function#arguments – stefan Jun 23 '20 at 20:08
  • Thanks, apparently there was a change between 3.3.0 and 3.3.2 in the docs and you still find the old reference for `stat_function` (see my link above) – starja Jun 23 '20 at 20:12
  • Yep. Just had a look in the change log. See https://ggplot2.tidyverse.org/news/index.html. There have been some changes. In particular there is now a geom_function. – stefan Jun 23 '20 at 20:21