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
294
votes
2 answers

Turning off some legends in a ggplot

Suppose I have a ggplot with more than one legend. mov <- subset(movies, length != "") (p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) + geom_point() ) I can turn off the display of all the legends like this: (p1 <- p0 +…
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
272
votes
6 answers

Increase number of axis ticks

I'm generating plots for some data, but the number of ticks is too small, I need more precision on the reading. Is there some way to increase the number of axis ticks in ggplot2? I know I can tell ggplot to use a vector as axis ticks, but what I…
João Daniel
  • 8,696
  • 11
  • 41
  • 65
263
votes
7 answers

Changing font size and direction of axes text in ggplot2

I am plotting a graph with a categorical variable on the x axis and a numerical variable on the y axis. For the x axis, given that there are many data points, the default text formatting causes the label for each tick mark to overlap with other…
Anjan
  • 2,858
  • 4
  • 17
  • 10
257
votes
5 answers

Emulate ggplot2 default color palette

What function can I use to emulate ggplot2's default color palette for a desired number of colors. For example, an input of 3 would produce a character vector of HEX colors with these colors:
Suraj
  • 35,905
  • 47
  • 139
  • 250
253
votes
6 answers

ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"

With this data frame ("df"): year pollution 1 1999 346.82000 2 2002 134.30882 3 2005 130.43038 4 2008 88.27546 I try to create a line chart like this: plot5 <- ggplot(df, aes(year, pollution)) + geom_point() + geom_line()…
megashigger
  • 8,695
  • 17
  • 47
  • 79
242
votes
5 answers

Change size of axes title and labels in ggplot2

I have a really simple question, which I am struggling to find the answer to. I hoped someone here might be able to help me. An example dataframe is presented below: a <- c(1:10) b <- c(10:1) df <- data.frame(a,b) library(ggplot2) g =…
KT_1
  • 8,194
  • 15
  • 56
  • 68
236
votes
3 answers

Label points in geom_point

The data I'm playing with comes from the internet source listed below nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv", sep=",") What I want to do, is create a 2D points graph comparing two metrics from this table, with each player…
Green Demon
  • 4,078
  • 6
  • 24
  • 32
220
votes
6 answers

Annotating text on individual facet in ggplot2

I want to annotate some text on last facet of the plot with the following code: library(ggplot2) p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() p <- p + facet_grid(. ~ cyl) p <- p + annotate("text", label = "Test", size = 4, x = 15, y =…
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
217
votes
5 answers

How to assign colors to categorical variables in ggplot2 that have stable mapping?

I've been getting up to speed with R in the last month. Here is my question: What is a good way to assign colors to categorical variables in ggplot2 that have stable mapping? I need consistent colors across a set of graphs that have different…
wintour
  • 2,295
  • 2
  • 14
  • 10
216
votes
8 answers

How can I handle R CMD check "no visible binding for global variable" notes when my ggplot2 syntax is sensible?

EDIT: Hadley Wickham points out that I misspoke. R CMD check is throwing NOTES, not Warnings. I'm terribly sorry for the confusion. It was my oversight. The short version R CMD check throws this note every time I use sensible plot-creation syntax in…
briandk
  • 6,749
  • 8
  • 36
  • 46
212
votes
2 answers

What do hjust and vjust do when making a plot using ggplot?

Every time I make a plot using ggplot, I spend a little while trying different values for hjust and vjust in a line like + opts(axis.text.x = theme_text(hjust = 0.5)) to get the axis labels to line up where the axis labels almost touch the axis,…
William Gunn
  • 2,925
  • 8
  • 26
  • 22
206
votes
9 answers

Show percent % instead of counts in charts of categorical variables

I'm plotting a categorical variable and instead of showing the counts for each category value. I'm looking for a way to get ggplot to display the percentage of values in that category. Of course, it is possible to create another variable with the…
wishihadabettername
  • 14,231
  • 21
  • 68
  • 85
204
votes
9 answers

Add a common Legend for combined ggplots

I have two ggplots which I align horizontally with grid.arrange. I have looked through a lot of forum posts, but everything I try seem to be commands that are now updated and named something else. My data looks like this; # Data plot 1 …
jO.
  • 3,384
  • 7
  • 28
  • 38
201
votes
4 answers

Force the origin to start at 0

How can I set the origin / interception of the y-axis and x-axis in ggplot2? The line of the x-axis should be exactly at y=Z. With Z=0 or another given value.
Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
200
votes
6 answers

How to use a variable to specify column name in ggplot

I have a ggplot command ggplot( rates.by.groups, aes(x=name, y=rate, colour=majr, group=majr) ) inside a function. But I would like to be able to use a parameter of the function to pick out the column to use as colour and group. I.e. I would like…
Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45