Questions tagged [grob]

stands for **gr**aphical **ob**ject, the generic drawing component in the grid framework of R graphics. Grobs can be very primitive, such as text, rectangles, lines, or complex objects such as full plots built up from many lower-level components.

The recommended grid package is included in every distribution of the R software. As such, it provides a low-level framework for building up all kinds of static graphics. The core component of grid-based graphics is a grob, which in its most basic form is a generic class of objects that describe a graphical component. The most basic grobs are simple shapes such as pointsGrob, polygonGrob, etc., but new objects can be derived from those primitive objects, and are often assembled into a grob tree, or gTree, which holds together multiple shapes together with a specific layout.

Examples of high-level, complex grobs are the complete plots generated by the ggplot2 and lattice packages.

146 questions
56
votes
3 answers

Inverse of ggplotGrob?

I have a function which manipulates a ggplot object, by converting it to a grob and then modifying the layers. I would like the function to return a ggplot object not a grob. Is there a simple way to convert a grob back to gg? The documentation on…
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
24
votes
2 answers

How to place grobs with annotation_custom() at precise areas of the plot region?

I am trying to reproduce the following [base R] plot with ggplot2 I have managed most of this, but what is currently baffling me is the placement of the line segments that join the marginal rug plot on the right of the plot with the respective…
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
17
votes
2 answers

Seeking workaround for gtable_add_grob code broken by ggplot 2.2.0

In plots with multiple facet variables, ggplot2 repeats the facet label for the "outer" variable, rather than having a single spanning facet strip across all the levels of the "inner" variable. I have some code that I've been using to cover the…
eipi10
  • 91,525
  • 24
  • 209
  • 285
12
votes
1 answer

force a regular plot object into a Grob for use in grid.arrange

b <- ggplot(cars,aes(x=speed,y=dist))+geom_line() grid.arrange( b, plot(cars), ncol=1 ) gives me the following error Error in gList(list(grobs = list(list(x = 0.5, y = 0.5, width = 1, height = 1, : only 'grobs' allowed in…
Chapo
  • 2,563
  • 3
  • 30
  • 60
11
votes
1 answer

npc coordinates of geom_point in ggplot2

How can I get the x, y coordinates of a geom_point in a ggplot, where the reference frame is the entire plotted image? I can create a ggplot with some geom_points using: library(ggplot2) my.plot <- ggplot(data.frame(x = c(0, 0.456, 1), y = c(0,…
LBogaardt
  • 402
  • 1
  • 4
  • 25
10
votes
1 answer

Using a pheatmap in arrangeGrob

I'm attempting to plate two plots in the same .jpg using arrangeGrob(). I've only just started learning about grids and grobs and I think I know what the problem is: pheatmap is a grid object and containing grob objects, not allowing me to put it in…
Xizam
  • 699
  • 7
  • 21
10
votes
2 answers

Remove white space between plots and table in grid.arrange

I would like to remove the large spacing that is inserted by default between the plots and the table in a grid.arrange, as shown in the MWE hereafter: require(ggplot2) require(gridExtra) list1=data.frame(mtcars[1:3, ]) # Dummy data p1 =…
JohnBee
  • 1,720
  • 1
  • 15
  • 19
10
votes
1 answer

How to use empty space produced by facet_wrap?

I have a faceted plot that forms an n x m grid. By design, the last (bottom-right) cell is always empty, so I'd like to utilize the extra space by adding another ggplot object. My current solution relies on low-level viewport approach, which is not…
tonytonov
  • 25,060
  • 16
  • 82
  • 98
9
votes
3 answers

Is it possible to draw the axis line first, before the data?

This is a follow up to my previous question where I was looking for a solution to get the axis drawn first, then the data. The answer works for that specific question and example, but it opened a more general question how to change the plotting…
tjebo
  • 21,977
  • 7
  • 58
  • 94
9
votes
2 answers

How to apply a clipping mask to geom in a ggplot?

I am trying to apply a clipping mask to a geom from a ggplot to mask part of the data, but keep the axis, the grid, other geoms and the legend visible. I do not want to create a specific plot, and therefore I am not looking for a work-around with…
Hobo Sheep
  • 389
  • 2
  • 11
9
votes
2 answers

How can ggplot2 keep jittered locations within a map boundary, such as a US state?

Is there a way to keep points that are jittered on a map within a boundary of that map? In the example below, where jittered locations in southwestern Connecticut end up in the water or in an adjoining state, is there a way to have R jitter the…
lawyeR
  • 7,488
  • 5
  • 33
  • 63
8
votes
3 answers

R ggplot2: change colour of font and background in facet strip?

I am trying to customize a ggplot2 plot containing facets, and would like to change both the colour of the facet strip, as well as the colour of the font. I found some code to change the strip.background colour, but was not able to modify it to…
Matifou
  • 7,968
  • 3
  • 47
  • 52
8
votes
3 answers

R return corrplot as object

corrplot plots a correlation matrix, but it does not return a graphical object (grob) I would like to plot several correlation matrices on a single page. For normal plots, I would use grid.arrange from the gridExtra package. However since corrplot…
Jeff
  • 12,147
  • 10
  • 51
  • 87
7
votes
1 answer

Estimate percentage of plot taken up by legend in ggplot2

I have some ggplot functionality, but I want to give users a warning in case they abuse it heavily. The problem is this: if there are many groups and the group names are long, the legend becomes so big it takes up the entire plot: But if I were to…
Vandenman
  • 3,046
  • 20
  • 33
7
votes
1 answer

Add grid.text to arrange.grob for export as PNG

I am attempting to create and export, as a PNG file, with several plots arranged in a 3 X 2 matrix. Each row (containing two plots) has its own X axis. I can add the additional axes via a grid.text but this grid.text is not exported with the PNG…
tcacek
  • 71
  • 1
  • 2
1
2 3
9 10