Questions tagged [ggplot2]

ggplot2 is an actively maintained open-source chart-drawing package for R, written by Hadley Wickham, based upon the principles of "Grammar of Graphics". It partially replaces R's basic plot and the lattice package, while providing a clean, powerful, orthogonal and fun API.

ggplot2 is a package for R, written by Hadley Wickham, based upon the principles of Grammar of Graphics by Leland Wilkinson. It partially replaces R's graphics and the lattice packages, while providing a clean, powerful and fun API.

Repositories

Vignettes

Frequently Asked Questions

Books

Other resources

R packages extending ggplot2:

  • cowplot. This provides support for arranging multiple ggplot plots using a robust grid system, as well as tools for annotating plots, and a theme optimized for academic publications.
  • GGally. Contains templates for different plots to be combined into a plot matrix, as well as a parallel coordinate plot function.
  • ggalt. Extra Coordinate Systems, Geoms, Statistical Transformations, Scales & Fonts for ‘ggplot2’.
  • ggcolpairs Combination of GGplot2 plots into a matrix based on data columns
  • ggdendro Tools for extracting dendrogram and tree diagram plot data for use with ggplot
  • granovaGG Graphical Analysis of Variance Using ggplot2
  • nvis Combination of visualization functions for nuclear data using ggplot2 and ggcolpairs
  • ggmap. Allows visualization of spatial data and models on top of Google Maps, OpenStreetMap, or Stamen Maps using ggplot2
  • ggsignif. Provides a single layer geom_signif which can calculate the significance of a difference between groups and add the annotation to the plot
  • ggstance. Provides horizontal versions of ggplot2's stats and geoms.
  • ggtern. An extension to ggplot2 for the creation of ternary diagrams
  • ggthemes. Contains extra geoms, scales, and themes for use with ggplot2
  • ggsubplot. Makes it easy to embed customized subplots within larger graphics
  • gridExtra. This is useful for arranging multiple ggplot objects together, as ggplot2 is based upon the grid graphics system. An alternative to cowplot.
  • ggpmisc. Makes it easy to add equations and parameters from model fits as text or labels, to label peaks and/or valleys, to use time series objects as data, to label or highlight observations in low density regions, to debug stats code.
  • ggrepel. Makes it easy to add repulsive text and labels (relocated to avoid overlap).
  • ggspectra. Makes it easy to plot and annotate light spectra.
  • ggridges. Partially overlapping line plots that create the impression of a mountain range, useful for visualizing changes in distributions over time or space.
  • ggtext. Improved text rendering support for ggplot2, enables complex formatted plot labels.

See also the ggplot2 extensions web site.

Related tags

54496 questions
98
votes
6 answers

Choosing between qplot() and ggplot() in ggplot2

I'm starting to use the great ggplot2 package for plotting in R, and one of the first things I ask myself before each plot is "well, will I use qplot or ggplot ?" I understand that qplot provides a simpler syntax while ggplot allows maximum features…
juba
  • 47,631
  • 14
  • 113
  • 118
98
votes
2 answers

How to set legend alpha with ggplot2

I have a graph of wind speeds against direction which has a huge numeber of points, and so am using alpha=I(1/20) in addition to color=month Here is a sample of code: library(RMySQL) library(ggplot2) con <- dbConnect(...) wind <- dbGetQuery(con,…
Chris
  • 1,888
  • 4
  • 21
  • 27
98
votes
6 answers

How do you add a general label to facets in ggplot2?

I often have numeric values for faceting. I wish to provide sufficient information to interpret these faceting values in a supplemental title, similar to the axis titles. The labeller options repeat much unnecessary text and are unusable for longer…
Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87
97
votes
13 answers

Plot correlation matrix into a graph

I have a matrix with some correlation values. Now I want to plot that in a graph that looks more or less like that: How can I achieve that?
anon
97
votes
4 answers

Shift legend into empty facets of a faceted plot in ggplot2

Consider the following plot: library(ggplot2) p <- ggplot(diamonds, aes(x = carat, fill = cut)) + geom_density(position = "stack") + facet_wrap(~ color) The facet_wrap function wraps a sequence of faceted panels into a roughly…
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
96
votes
2 answers

Is there a built-in way to do a logarithmic color scale in ggplot2?

Here's an example of a binned density plot: library(ggplot2) n <- 1e5 df <- data.frame(x = rexp(n), y = rexp(n)) p <- ggplot(df, aes(x = x, y = y)) + stat_binhex() print(p) It would be nice to adjust the color scale so that the breaks are…
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
96
votes
5 answers

ggplot2: Adjust the symbol size in legends

How should I change the size of symbols in legends? I checked the document of theme but found no answer. Here is an example: library(ggplot2);library(grid) set.seed(1000) x <- 1:6 mu <- sin(x) observed <- mu + rnorm(length(x), 0, 0.5*sd(mu)) data…
wush978
  • 3,114
  • 2
  • 19
  • 23
96
votes
5 answers

set only lower bound of a limit for ggplot

Is it possible to only set the lower bound of a limit for continuous scale? I want to make all my plots 0 based without needing to specify the upper limit bound. e.g. + scale_y_continuous(minlim=0)
Mark
  • 3,177
  • 4
  • 26
  • 37
95
votes
5 answers

How can I obtain an 'unbalanced' grid of ggplots?

With grid.arrange I can arrange multiple ggplot figures in a grid to achieve a multi-panel figure by using something like: library(ggplot2) library(grid) library(gridExtra) generate some ggplot2 plots , then plot5 <- grid.arrange(plot4, plot1,…
user441706
  • 1,370
  • 2
  • 16
  • 17
94
votes
3 answers

ggplot combining two plots from different data.frames

I want to combine two ggplots, from two different data.frames, into one plot. Below you will find the code. I want to combine plot 1&2 or plots 3&4. df1 <- data.frame(p=c(10,8,7,3,2,6,7,8), v=c(100,300,150,400,450,250,150,400)) df2 <-…
jeroen81
  • 2,305
  • 5
  • 29
  • 41
94
votes
7 answers

How to plot a function curve in R

What are the alternatives for drawing a simple curve for a function like eq = function(x){x*x} in R? It sounds such an obvious question, but I could only find these related questions on stackoverflow, but they are all more specific Plot line…
sjdh
  • 3,907
  • 8
  • 25
  • 32
93
votes
3 answers

How can I arrange an arbitrary number of ggplots using grid.arrange?

This is cross-posted on the ggplot2 google group My situation is that I'm working on a function that outputs an arbitrary number of plots (depending upon the input data supplied by the user). The function returns a list of n plots, and I'd like to…
briandk
  • 6,749
  • 8
  • 36
  • 46
92
votes
1 answer

Why does tmap render 80 times faster than ggplot2? [Plotting shapefiles in R with ggplot2::geom_sf(), using XQuartz/X11 graphics device on macOS]

Update/Edit/Reprex: Rendering the same spatial data with the same graphics device takes 1 second with tmap versus 80 seconds with ggplot2, even though the tmap plot's R object is 80x larger in size. Difference in internals and/or implementation btw.…
dad
  • 1,335
  • 9
  • 28
91
votes
10 answers

Using ggplot2, can I insert a break in the axis?

I want to make a bar plot where one of the values is much bigger than all other values. Is there a way of having a discontinuous y-axis? My data is as follows: df <- data.frame(a = c(1,2,3,500), b = c('a1', 'a2','a3', 'a4')) p <- ggplot(data = df,…
djq
  • 14,810
  • 45
  • 122
  • 157
91
votes
4 answers

What's the difference between facet_wrap() and facet_grid() in ggplot2?

I've been reading the ggplot2 documentation for both functions. I was wondering what were the differences and what would be right situation for using each function (facet_wrap() and facet_grid()). library(ggplot2) p <- qplot(displ, hwy, data =…
marbel
  • 7,560
  • 6
  • 49
  • 68