Questions tagged [ggproto]

ggproto is at the heart of the R package ggplot2. It implements a protype based OO system which blurs the lines between classes and instances. It is inspired by the proto package, but it has some important differences. Notably, it cleanly supports cross-package inheritance, and has faster performance. Knowledge of ggproto is essential if you want to add new primitives, like geoms or stats to ggplot2.

Questions tagged with may often involve adding new functionality involved in plotting, such as making new scales, geoms, stats, coords and facet systems, through the ggproto extension mechanism.

The ggplot2 package introduced an official extension mechanism for Stats, Geoms and Positions in other packages in version 2.0.0. Since then, Facets (2.2.0) and Coords (3.0.0) were added. At the core of this mechanism is the prototype based ggproto class that define the parameters and methods with which different structures in ggplot2 are build.

Important introductions to the topic can be found here:

96 questions
11
votes
1 answer

How to make a custom ggplot2 geom with multiple geometries

I've been reading the vignette on extending ggplot2, but I'm a bit stuck on how I can make a single geom that can add multiple geometries to the plot. Multiple geometries already exist in ggplot2 geoms, for example, we have things like geom_contour…
Ben
  • 41,615
  • 18
  • 132
  • 227
10
votes
3 answers

How to determine the geom type of each layer of a ggplot2 object?

As part of an effort to remove a specific geom from a plot I've already created (SO link here), I'd like to dynamically determine the geom type of each layer of a ggplot2 object. Assuming I don't know the order in which I added layers, is there a…
Erik Shilts
  • 4,389
  • 2
  • 26
  • 51
9
votes
1 answer

How to automate legends for a new geom in ggplot2?

I've built this new ggplot2 geom layer I'm calling geom_triangles (see https://github.com/ctesta01/ggtriangles/) that plots isosceles triangles given aesthetics including x, y, z where z is the height of the triangle and the base of the isosceles…
ctesta01
  • 909
  • 8
  • 19
9
votes
1 answer

Creating geom / stat from scratch

I just started working with R not long ago, and I am currently trying to strengthen my visualization skills. What I want to do is to create boxplots with mean diamonds as a layer on top (see picture in the link below). I did not find any functions…
MagKvis
  • 91
  • 3
8
votes
4 answers

ggplot2: How to conditionally change geom_text's vjust when low bars make text exceed bar's bottom

When plotting a bar chart, I often add labels to bars to signify the y-value for each bar. However, I run into trouble when the bar becomes too low, making the label unreadable or simply ugly. Example library(ggplot2) df_blood <-…
Emman
  • 3,695
  • 2
  • 20
  • 44
8
votes
1 answer

Split violin plot with ggplot2 with quantiles

In order to plot half densities, I am using the function described in this post: Split violin plot with ggplot2 However, when I want to draw the quantiles on the densities, like on a normal geom_violin() or geom_boxplot(), I obtain an error…
P. Denelle
  • 790
  • 10
  • 24
8
votes
1 answer

Can you make geom_ribbon leave a gap for missing values?

I am following up a discussion started at: How can I make geom_area() leave a gap for missing values?. It seems like geom_ribbon is not longer leaving gaps for missing values. Please try to execute the reproducible example in the attached link. I…
eFF
  • 267
  • 3
  • 17
8
votes
2 answers

R: adding alpha bags to a 2d or 3d scatterplot

I know that in ggplot2 one can add the convex hull to a scatterplot by group as in library(ggplot2) library(plyr) data(iris) df<-iris find_hull <- function(df) df[chull(df$Sepal.Length, df$Sepal.Width), ] hulls <- ddply(df, "Species",…
Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103
7
votes
1 answer

ggplot2 custom stat not shown when facetting

I'm trying to write a custom stat_* for ggplot2, where I would like to color a 2D loess surface using tiles. When I start from the extension guide, I can write a stat_chull like they do: stat_chull = function(mapping = NULL, data = NULL, geom =…
Michael Schubert
  • 2,726
  • 4
  • 27
  • 49
7
votes
1 answer

Extending ggplot2 with a custom geometry for sf objects

I am trying to create a new geometry for ggplot as described here, while adapting it to deal with Simple Features objects. As an example, let's take the same exercise of plotting the convex hull of a set of points. Thus, I wrote a new…
ƒacu.-
  • 507
  • 3
  • 9
7
votes
0 answers

Can we access all data columns in a custom ggplot2's stat?

I would like to implement diagnostics for Cox proportional hazards model with ggplot2, by creating new stats functions and ggproto objects. The idea is to benefit from grouping (by color, facet_grid, etc.) for conditional computation of desired…
mjktfw
  • 840
  • 6
  • 14
7
votes
1 answer

Add unit labels to radar plot and remove outer ring ggplot2 (spider web plot, coord_radar)

I've been working on a radar plot recently and found a great bit of code on from Erwan Le Pennec. I've made a few edits, however there are some parts I couldn't work out how to remove the outermost circle as it doesn't define a value. Currently the…
pr1g114413085
  • 155
  • 1
  • 11
7
votes
1 answer

How to extend ggplot2 boxplot with ggproto?

I'm often using boxplots in my work and like ggplot2 aesthetics. But standard geom_boxplot lacks two things important for me: ends of whiskers and median labels. Thanks to information from here I've written a function: gBoxplot <- function(formula =…
UlvHare
  • 151
  • 7
6
votes
1 answer

Escape 'discrete aesthetic implies group' in custom stat

I'm trying to build a custom stat function with ggplot2 wherein I would like to access a discrete variable to compute a statistic with per group. However, the default behaviour of ggplot layers is to automatically assign implicit groups to any…
teunbrand
  • 33,645
  • 4
  • 37
  • 63
6
votes
1 answer

ggplot2 - where are the scales being built?

I wanted to see where factor values are turned into numeric ones. I tried to achieve this by simply adding print statements everywhere... geom_tile2 <- function(mapping = NULL, data = NULL, stat = "identity2", position =…
eok
  • 151
  • 8