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
81
votes
5 answers

Any way to make plot points in scatterplot more transparent in R?

I have a 3 column matrix; plots are made by points based on column 1 and column 2 values, but colored based on column 2 (6 different groups). I can successfully plot all points, however, the last plot group (group 6) which was assigned the color…
Steve
  • 1,047
  • 1
  • 9
  • 13
80
votes
5 answers

if else condition in ggplot to add an extra layer

say I want to plot two layers in ggplot, one containing points and another one containing lines if a certain criteria is fulfilled. The code without the criteria could look like this: library("ggplot2") # Summarise number of movie ratings by year…
joaoal
  • 1,892
  • 4
  • 19
  • 29
80
votes
4 answers

Boxplot show the value of mean

In this boxplot we can see the mean but how can we have also the number value on the plot for every mean of every box plot? ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() + stat_summary(fun.y=mean,…
user2354885
80
votes
4 answers

ggplot2 two-line label with expression

I'd like to write an axis label over two lines with an expression() statement. However, plotmath and expression won't allow this (e.g. subscripted text appears on the far right). I found this discussion circa 2005 of a similar issue but the work…
metasequoia
  • 7,014
  • 5
  • 41
  • 54
80
votes
2 answers

Formatting dates on X axis in ggplot2

I'm having a very, very tough time getting the x-axis to look correct for my graphs. Here is my data: df <- data.frame( Month = factor(c( "2011-07-31", "2011-08-31", "2011-09-30", "2011-10-31", "2011-11-30", "2011-12-31", "2012-01-31",…
mikebmassey
  • 8,354
  • 26
  • 70
  • 95
79
votes
4 answers

Wrap long axis labels via labeller=label_wrap in ggplot2

I would like to automatically wrap my labels in ggplot2, i.e. insert line breaks of long labels. Here is written how to write a function (1) for it, but sadly I do not know where to put labeller=label_wrap in my code (2). (1) function by…
Til Hund
  • 1,543
  • 5
  • 21
  • 37
79
votes
1 answer

Creating multi column legend in ggplot

I have the following script, which suppose to create a plot with two column legend. #!/usr/bin/Rscript library(ggplot2) library(plyr) library(reshape2) library(scales) file <- "http://dpaste.com/1354089/plain/"; dat…
pdubois
  • 7,640
  • 21
  • 70
  • 99
78
votes
5 answers

How to merge color, line style and shape legends in ggplot

Suppose I have the following plot in ggplot: It was generated using the code below: x <- seq(0, 10, by = 0.2) y1 <- sin(x) y2 <- cos(x) y3 <- cos(x + pi / 4) y4 <- sin(x + pi / 4) df1 <- data.frame(x, y = y1, Type = as.factor("sin"), Method =…
Ben
  • 6,321
  • 9
  • 40
  • 76
78
votes
2 answers

ggplot2, change title size

I would like to have my main title and axis title have the same font size as the annotated text in my plot. i used theme_get() and found that text size is 12, so I did that in my theme statement - this did not work. I also tried to send the relative…
user1375871
  • 1,199
  • 1
  • 12
  • 23
78
votes
6 answers

Bar plot, no space between bottom of geom and x axis keep space above

When I plot a bar graph in ggplot2 I would like to reduce the space between the bottom of the bars and the x-axis to 0, yet keep the space above the bars and the plot box. I have a hack to do it below. It's dirty and I want to be clean again. Is…
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
78
votes
1 answer

Custom legend for multiple layer ggplot

I'm trying to get a custom legend for a ggplot with data coming from two separate data frames. See below for a minimum reproducible example. What I'm trying to accomplish is to have a legend describing the ribbon fill, the black line, and the red…
scs217
  • 2,218
  • 2
  • 17
  • 18
78
votes
1 answer

Error: could not find function "unit"

While trying to modify theme settings this simple code gives the following error: library(ggplot2) theme_nogrid <- theme_set(theme_update( plot.margin=unit(c(.25, .25, .25, .25), "in"),)) Error in do.call(theme, list(...)) :…
opv
  • 881
  • 1
  • 6
  • 3
78
votes
3 answers

How to suppress warnings when plotting with ggplot

When passing missing values to ggplot, it's very kind, and warns us that they are present. This is acceptable in an interactive session, but when writing reports, you do not the output get cluttered with warnings, especially if there's many of them.…
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
77
votes
2 answers

Plot a function with ggplot, equivalent of curve()

Is there an equivalent method for plotting functions using ggplot to the curve() command employed in base graphics? I guess that the alternative would be to just create a vector of values of the function and plot a connected line, but I was hoping…
Charlie
  • 2,801
  • 3
  • 26
  • 27
77
votes
3 answers

Plotting with ggplot2: "Error: Discrete value supplied to continuous scale" on categorical y-axis

The plotting code below gives Error: Discrete value supplied to continuous scale What's wrong with this code? It works fine until I try to change the scale so the error is there... I tried to figure out solutions from similar problem but…
Rechlay
  • 1,457
  • 2
  • 12
  • 19