Can I change labels with custom Geom?



Hi everyone,
I have been working on small ggplot extension to speed-up some common analysis I need to run repeatedly. I have completed the code for my custom geom, but I was wondering if I can also set a default axis label. Do you guys know a solution?


Choosing between this site and StackOverflow for posting a question

So this might depend on what your extension is doing, but as far as setting up a default axis label, the StackOverflow post (below) might help:

You can also modify multiple components using a list:
which is also nicely described in @hadley’s short and useful Programming with ggplot2 RPub.


Sorry for being a ****, but isn’t this more like a Stack Overflow question?
Sorry again! :slight_smile:


Sorry, I didn’t mean to disturb the forum. But thanks for the help.


@taraas has a good point. This would be an excellent question for Stack Overflow if you could add some reproducible code to illustrate precisely what you are looking for.

The benefit of posting specific code questions at Stack Overflow is that more people are likely to benefit from the answers when you post there.


I realize this is tilting into the realm of #meta, but might be helpful to have a guide/post (maybe R Views or RStudio Blog?) on which kinds of questions might fit SO, etc. That, or just a visible link to rOpenSci’s SO/reprex community call

Choosing between this site and StackOverflow for posting a question

@andremrsantos What sort of axis did you have in mind? I think that, with the way ggplot2 works, the axis type is going to be determined by the type of data going in, rather than by the geom (or geoms) you’re plotting. In other words, they’re kind of independent. So you could include a custom scale in your extension package, but I’m not sure if you can set a particular scale to be used whenever you use the geom (unless there’s some way to specify what types of data a geom can ingest and therefore what scales are available to it). Does that make sense?

That said, @mara’s idea of defining a function that returns multiple ggplot2 element is a good one! So you have a geom defined, a scale defined, and then a function that returns them both in a list and can basically be used in place of the geom.


It isn’t clear to me that this isn’t an appropriate post for here.

It would certainly fall into the realm of questions asked on the ggplot mailing list, and that group were specifically asked to migrate to here. I assumed to carry on providing help to one another, but maybe I’ve got that wrong.

Choosing between this site and StackOverflow for posting a question

Hi @rensa,
I’m trying to define a stat that given a sample of p-values, it generates a qqplot for diagnostics (see code bellow). I was able to change the axis label to expected and observed using the code, but I was looking to define it as “Expected log[10] P” and “Observed log[10] P” to reduce my later presentation work.

  • P.S. I’ve also made a custom geom (which I was referencing) that plots not only the dots, but also a regression line and confidence interval lines.
  • P.S.S Its a fringe use case, but I need it quite frequently
StatQQPval <- ggproto("StatQQPval", Stat,
  required_aes = c("sample"),
  default_aes  = aes(x = ..expected.., y = ..observed..),
  compute_group = function(data, scales) {
      n <- nrow(data)
        observed = -log10(sort(sample)),
        expected = -log10(1:n/n))

@mara idea was great but not quite what I was looking for, because I need to group my sample (e.g. by color).

  • P.S.S.S. Also, I can move any further discussion to stack overflow if it would make community more comfortable.