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
147
votes
4 answers

Showing data values on stacked bar chart in ggplot2

I'd like to show data values on stacked bar chart in ggplot2. Here is my attempted code library(ggplot2) Data <- data.frame( Year = c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4)), Category = c(rep(c("A", "B", "C", "D"), times =…
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
144
votes
4 answers

Create a matrix of scatterplots (pairs() equivalent) in ggplot2

Is it possible to plot a matrix of scatter plots with ggplot2, using ggplot's nice features like mapping additional factors to color, shape etc. and adding smoother? I am thinking about something similar to the base function pairs.
Karsten W.
  • 17,826
  • 11
  • 69
  • 103
142
votes
8 answers

Remove grid, background color, and top and right borders from ggplot2

I would like to reproduce the plot immediately below by using ggplot2. I can come close, but cannot remove the top and right borders. Below I present several attempts using ggplot2, including several suggestions found on or via Stackoverflow. …
Mark Miller
  • 12,483
  • 23
  • 78
  • 132
140
votes
3 answers

ggplot geom_text font size control

I tried to change the font to 10 for the labels of my bar plot in ggplot2 by doing something like this: ggplot(data=file,aes(x=V1,y=V3,fill=V2)) + geom_bar(stat="identity",position="dodge",colour="white") + …
olala
  • 4,146
  • 9
  • 34
  • 44
140
votes
4 answers

Increase distance between text and title on the y-axis

The y-axis title appears too close to the axis text. ggplot(mpg, aes(cty, hwy)) + geom_point() I have tried changing the value of many parameters with theme() but none seems to help.
Ernest A
  • 7,526
  • 8
  • 34
  • 40
139
votes
2 answers

adding x and y axis labels in ggplot2

How do I change the x and y labels on this graph please? library(Sleuth2) library(ggplot2) discharge<-ex1221new$Discharge area<-ex1221new$Area nitrogen<-ex1221new$NO3 p <- ggplot(ex1221new, aes(discharge, area), main="Point") p +…
Brett Michael
  • 1,403
  • 2
  • 10
  • 8
135
votes
4 answers

How to get a vertical geom_vline to an x-axis of class date?

Even though I found Hadley's post in the google group on POSIXct and geom_vline, I could not get it done. I have a time series from and would like to draw a vertical line for years 1998, 2005 and 2010 for example. I tried with ggplot and qplot…
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
133
votes
2 answers

ggplot2 legend to bottom and horizontal

How can I move a ggplot2 legend to the bottom of the plot and turn it horizontally? Sample code: library(reshape2) # for melt df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2")) p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value)) p1 +…
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
132
votes
1 answer

Position geom_text on dodged barplot

I tried to make the title self-explanatory, but here goes - data first: dtf <- structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", "fla"), class = "factor"), ustanova =…
aL3xa
  • 35,415
  • 18
  • 79
  • 112
132
votes
11 answers

How to draw an empty plot?

I need to make an empty plot. This is the best could I come up with. plot(0, xaxt = 'n', yaxt = 'n', bty = 'n', pch = '', ylab = '', xlab = '') Any simpler solutions? P.S.: completely empty, no axis etc.
Adam SO
  • 9,821
  • 8
  • 28
  • 27
126
votes
3 answers

How do I change the background color of a plot made with ggplot2

By default, ggplot2 produces plots with a grey background. How do I change the color of the background of the plot? For example, a plot produced by the following code: library(ggplot2) myplot<-ggplot(data=data.frame(a=c(1,2,3), b=c(2,3,4)),…
Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
126
votes
4 answers

Keep unused levels in bar plot

I want to plot unused levels (that is, levels where the count is 0) in my bar-plot, however, unused levels are dropped and I cannot figure out how to keep them df <- data.frame(type=c("A", "A", "A", "B", "B"), group=rep("group1", 5)) df$type <-…
Ulrik
  • 1,575
  • 2
  • 10
  • 10
125
votes
13 answers

How to display only integer values on an axis using ggplot2

I have the following plot: library(reshape) library(ggplot2) library(gridExtra) require(ggplot2) data2<-structure(list(IR = structure(c(4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L ), .Label = c("0.13-0.16", "0.17-0.23", "0.24-0.27", "0.28-1" ), class =…
Atticus29
  • 4,190
  • 18
  • 47
  • 84
125
votes
4 answers

Remove extra legends in ggplot2

I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this: df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20), group=c(rep("a",10),rep("b",10))) And I'm trying to…
Wilduck
  • 13,822
  • 10
  • 58
  • 90
122
votes
2 answers

How do you specifically order ggplot2 x axis instead of alphabetical order?

I'm trying to make a heatmap using ggplot2 using the geom_tiles function here is my code below: p<-ggplot(data,aes(Treatment,organisms))+geom_tile(aes(fill=S))+ scale_fill_gradient(low = "black",high = "red") + scale_x_discrete(expand = c(0,…
Lisa Ta
  • 1,231
  • 2
  • 9
  • 4