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

ggplot plots in scripts do not display in Rstudio

I have a strange issue with Rstudio: If a script calls ggplot2 functions to display a plot, then using Source to run the script does not produce the plots. If I select the whole script with Ctrl+A, then Run the current line or selection…
Superbest
  • 25,318
  • 14
  • 62
  • 134
110
votes
5 answers

How to fix the aspect ratio in ggplot?

I'm trying to resize a plot to fit into my document, but I'm having difficulties getting the plotted diagram do be a square. Example: pdf(file = "./out.pdf", width = 5, height = 5) p <- ggplot(mydata, aes(x = col1, y = col2)) print(p) aux <-…
htorque
  • 1,818
  • 4
  • 19
  • 22
109
votes
2 answers

Legend on bottom, two rows wrapped in ggplot2 in r

library(ggplot2) library(scales) library(reshape2) Rdates <- c("2007-01-31","2007-02-28","2007-03-30","2007-04-30","2007-05-31","2007-06-29","2007-07-31","2007-08-31","2007-09-28","2007-10-31") Rdates <- as.Date(Rdates) Cnames <- c("Column 1…
Trevor Nederlof
  • 2,546
  • 5
  • 22
  • 40
109
votes
1 answer

How to increase the size of points in legend of ggplot2?

I have thousands of points in one figure and set size = 1. However, the point size in the legend is reduced too. How to increase the point size in the legend? For example. num <- 10000 set.seed(1) df <- data.frame(x = seq(1, num), y = runif(num), z…
Bangyou
  • 9,462
  • 16
  • 62
  • 94
109
votes
7 answers

How to add a ggplot2 subtitle with different size and colour?

I'm using ggplot2 to improve precipitation barplots. Here's a reproducible example of what I want to achieve: library(ggplot2) library(gridExtra) secu <- seq(1, 16, by=2) melt.d <- data.frame(y=secu, x=LETTERS[1:8]) m <- ggplot(melt.d, aes(x=x,…
Migue
  • 1,401
  • 2
  • 15
  • 19
108
votes
3 answers

Control point border thickness in ggplot

When using ggplot, I can set shape to 21-25 to get shapes that have independent setting for the internal (fill) and border (col) colors, like so: df <- data.frame(id=runif(12), x=1:12, y=runif(12)) ggplot(df, aes(x=x, y=y)) + …
Noam Ross
  • 5,969
  • 5
  • 24
  • 40
106
votes
4 answers

How to move or position a legend in ggplot2

I'm trying to create a ggplot2 plot with the legend beneath the plot. The ggplot2 book says on p 112 "The position and justification of legends are controlled by the theme setting legend.position, and the value can be right, left, top, bottom, none…
Dan Goldstein
  • 24,229
  • 18
  • 37
  • 41
103
votes
2 answers

Changing line colors with ggplot()

I don't use ggplot2 that much, but today I thought I'd give it a go on some graphs. But I can't figure out how to manually control colors in geom_line() I'm sure I'm overlooking something simple, but here's my test code: x <- c(1:20, 1:20) variable…
JD Long
  • 59,675
  • 58
  • 202
  • 294
102
votes
2 answers

Saving a high resolution image in R

I'm creating a scatterplot using ggplot in R (R version 3.2.1). I want to save the graph as a tiff image in 300 DPI in order to publish it in a journal. However, my code using ggsave or tiff() with dev.off doesn't seem to work and only saves it in…
Dana
  • 1,053
  • 2
  • 8
  • 7
101
votes
3 answers

How to deal with "data of class uneval" error from ggplot2?

While trying to overlay a new line to a existing ggplot, I am getting the following error: Error: ggplot2 doesn't know how to deal with data of class uneval The first part of my code works fine. Below is an image of "recent" hourly wind generation…
MikeTP
  • 7,716
  • 16
  • 44
  • 57
100
votes
5 answers

How can I suppress the vertical gridlines in a ggplot2 plot?

I am building a bar chart for which bars suffice as indications of horizontal (x) placement, so I'd like to avoid drawing the superfluous vertical gridlines. I understand how to style the minor and major gridlines in opts(), but I can't for the life…
Tarek
  • 2,041
  • 3
  • 15
  • 20
99
votes
3 answers

geom_smooth() what are the methods available?

I'm using geom_smooth() from ggplot2. In Hadley Wickham's book ("ggplot2 - Elegant Graphics for Data Analysis") there is an example (page 51), where method="lm" is used. In the online manual there is no talk of the method argument. I see other…
nzcoops
  • 9,132
  • 8
  • 41
  • 52
99
votes
4 answers

R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph

I am getting this error while plotting a bar graph and I am not able to get rid of it, I have tried both qplot and ggplot but still the same error. Following is my code: library(dplyr) library(ggplot2) #Investigate data further to build a…
Uasthana
  • 1,645
  • 5
  • 16
  • 24
99
votes
6 answers

Reverse order of discrete y axis in ggplot2

Situation & data I have a dataframe df of athlete positions in a race: df <- structure(list(athlete = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "A", "B", "C", "D", "E", "F", "G", "H", "I",…
tospig
  • 7,762
  • 14
  • 40
  • 79
99
votes
4 answers

How to save plots that are made in a shiny app

I'm trying to figure out how to use downloadButton to save a plot with shiny. The example in the package demonstrates downloadButton/downloadHandler to save a .csv. I'm going to make a reproducible example based on that. For…
alexwhan
  • 15,636
  • 5
  • 52
  • 66