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
164
votes
3 answers

Setting individual axis limits with facet_wrap and scales = "free" in ggplot2

I'm creating a facetted plot to view predicted vs. actual values side by side with a plot of predicted value vs. residuals. I'll be using shiny to help explore the results of modeling efforts using different training parameters. I train the model…
Hendy
  • 10,182
  • 15
  • 65
  • 71
163
votes
14 answers

Scatterplot with marginal histograms in ggplot2

Is there a way of creating scatterplots with marginal histograms just like in the sample below in ggplot2? In Matlab it is the scatterhist() function and there exist equivalents for R as well. However, I haven't seen it for ggplot2. I started an…
Seb
  • 5,417
  • 7
  • 31
  • 50
163
votes
1 answer

ggplot does not work if it is inside a for loop although it works outside of it

I'm using a simple ggplot function which works fine outside a loop but not inside even if the iterative value does not interfere with the ggplot function. Why is it so ? Here is my code x=1:7 y=1:7 df =…
Remi.b
  • 17,389
  • 28
  • 87
  • 168
163
votes
5 answers

How do I change the formatting of numbers on an axis with ggplot?

I'm using R and ggplot to draw a scatterplot of some data, all is fine except that the numbers on the y-axis are coming out with computer style exponent formatting, i.e. 4e+05, 5e+05, etc. This is unacceptable to me, so I want to get it to display…
Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
161
votes
1 answer

facet label font size

Is there a way to change the font size of facet labels in ggplot? I googled and found that the issue was yet on Hadley's to-do list. I wonder if there is a workaround or any news on this issue?
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
160
votes
4 answers

How can I change the Y-axis figures into percentages in a barplot?

How can we change y axis to percent like the figure? I can change y axis range but I can't make it to percent.
STAT
157
votes
6 answers

How to make graphics with transparent background in R using ggplot2?

I need to output ggplot2 graphics from R to PNG files with transparent background. Everything is ok with basic R graphics, but no transparency with ggplot2: d <- rnorm(100) #generating random data #this returns transparent…
Yuriy Petrovskiy
  • 7,888
  • 10
  • 30
  • 34
155
votes
7 answers

Order discrete x scale by frequency/value

I am making a dodged bar chart using ggplot with discrete x scale, the x axis are now arranged in alphabetical order, but I need to rearrange it so that it is ordered by the value of the y-axis (i.e., the tallest bar will be positioned on the…
lokheart
  • 23,743
  • 39
  • 98
  • 169
154
votes
3 answers

Overlaying histograms with ggplot2 in R

I am new to R and am trying to plot 3 histograms onto the same graph. Everything worked fine, but my problem is that you don't see where 2 histograms overlap - they look rather cut off. When I make density plots, it looks perfect: each curve is…
Bloomy
  • 2,103
  • 4
  • 17
  • 9
154
votes
4 answers

How to use Greek symbols in ggplot2?

My categories need to be named with Greek letters. I am using ggplot2, and it works beautifully with the data. Unfortunately I cannot figure out how to put those greek symbols on the x axis (at the tick marks) and also make them appear in the…
Sam
  • 7,922
  • 16
  • 47
  • 62
154
votes
7 answers

Fixing the order of facets in ggplot

Data: df <- data.frame( type = c("T", "F", "P", "T", "F", "P", "T", "F", "P", "T", "F", "P"), size = c("50%", "50%", "50%", "100%", "100%", "100%", "150%", "150%", "150%", "200%", "200%", "200%"), amount = c(48.4, 48.1, 46.8, 25.9,…
samarasa
  • 2,025
  • 2
  • 16
  • 22
152
votes
8 answers

Persistent invalid graphics state error when using ggplot2

I believe my dataframe is okay and my code is okay. In fact, I have eliminated parts of the dataframe and most of the graphing code to make things as basic as possible. But still, I get: Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL))…
Nazer
  • 3,654
  • 8
  • 33
  • 47
150
votes
7 answers

Changing fonts in ggplot2

Once upon a time, I changed my ggplot2 font using windowsFonts(Times=windowsFont("TT Times New Roman")). Now, I can't get it off of this. In trying to set family="" in ggplot2 theme(), I can't seem to generate a change in fonts as I compile the MWE…
EngBIRD
  • 1,915
  • 3
  • 18
  • 22
150
votes
5 answers

Remove legend title in ggplot

I'm trying to remove the title of a legend in ggplot2: df <- data.frame( g = rep(letters[1:2], 5), x = rnorm(10), y = rnorm(10) ) library(ggplot2) ggplot(df, aes(x, y, colour=g)) + geom_line(stat="identity") + …
smillig
  • 5,073
  • 6
  • 36
  • 46
149
votes
2 answers

Limit ggplot2 axes without removing data (outside limits): zoom

If you specify axis limits in ggplot the outlying points are removed. This is fine for points, but you might want to plot lines that intersect with the specified range, but ggplot's range or xlim/ylim methods removes these. Is there another way to…
geotheory
  • 22,624
  • 29
  • 119
  • 196