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
90
votes
6 answers

Avoid ggplot sorting the x-axis while plotting geom_bar()

I have the following data which I want to plot with ggplot: SC_LTSL_BM 16.8275 SC_STSL_BM 17.3914 proB_FrBC_FL 122.1580 preB_FrD_FL 18.5051 B_Fo_Sp 14.4693 B_GC_Sp 15.4986 What I want to do is to make a bar plot and maintain the…
neversaint
  • 60,904
  • 137
  • 310
  • 477
90
votes
2 answers

Subscript letters in ggplot axis label

I'm trying to work out how to have subscript letters in an axis label. dat <- data.frame(x = rnorm(100), y = rnorm(100)) ggplot(dat, aes(x=x,y=y)) + geom_point() + labs(y=expression(Blah[1])) dat <- data.frame(x = rnorm(100), y =…
nzcoops
  • 9,132
  • 8
  • 41
  • 52
90
votes
2 answers

Construct a manual legend for a complicated plot

I cannot figure out how to manually set up a legend for this plot. All I really want is a simple legend to the right that uses the three colors and has a name next to each. The current code looks like this: a…
Torvon
  • 1,113
  • 1
  • 10
  • 12
90
votes
6 answers

Plot multiple boxplot in one graph

I saved my data in as a .csv file with 12 columns. Columns two through 11 (labeled F1, F2, ..., F11) are features. Column one contains the label of these features either good or bad. I would like to plot a boxplot of all these 11 features against…
Samo Jerom
  • 2,361
  • 7
  • 32
  • 38
88
votes
2 answers

Controlling ggplot2 legend display order

Does anyone know how I can get control of the ordering of legends in ggplot2? From what I can see the order appears related to the actual scale labels rather than the scale declaration order. Changing the scale titles alters the ordering. I've made…
Alastair
  • 1,669
  • 3
  • 14
  • 27
87
votes
6 answers

ggplot2 reverse order of scale_brewer

Seemingly a very simple thing to do but it took me >30min without finding answer. How do I reverse the order of colors? By looking at documentation for scale_brewer, i figured it can be formatter= argument being suspicious. I passed 'rev' and then…
yosukesabai
  • 6,184
  • 4
  • 30
  • 42
87
votes
2 answers

Change bar plot colour in geom_bar with ggplot2 in r

I have the following in order to bar plot the data frame. c1 <- c(10, 20, 40) c2 <- c(3, 5, 7) c3 <- c(1, 1, 1) df <- data.frame(c1, c2, c3) ggplot(data=df, aes(x=c1+c2/2, y=c3)) + geom_bar(stat="identity", width=c2) + …
tuttifolies
  • 1,417
  • 2
  • 14
  • 20
87
votes
5 answers

ggplot2 - annotate outside of plot

I would like to associate sample size values with points on a plot. I can use geom_text to position the numbers near the points, but this is messy. It would be much cleaner to line them up along the outside edge of the plot. For instance, I…
jslefche
  • 4,379
  • 7
  • 39
  • 50
85
votes
5 answers

ggplot2, facet_grid, free scales?

In the following example, how do I get the y-axis limits to scale according to the data in each panel? mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() Neither of these will do it: mt + facet_grid(. ~ cyl, scales="free") mt…
hatmatrix
  • 42,883
  • 45
  • 137
  • 231
85
votes
13 answers

Error: package or namespace load failed for ggplot2 and for data.table

I am not able to open install the ggplot2 and data.table packages. It gives me the following error (example for ggplot2) > library(ggplot2) Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) : there is no…
elisahmendes
  • 2,149
  • 3
  • 13
  • 8
85
votes
6 answers

Putting x-axis at top of ggplot2 chart

I feel like this should be obvious... all I'm trying to do is to remove the x-axis from the bottom of my graph and add it to the top. Here is a reproducible example. Data plus code to make the following…
jalapic
  • 13,792
  • 8
  • 57
  • 87
85
votes
2 answers

How to save a plot made with ggplot2 as SVG

I want to save a stacked area plot (Plot example with code can be found here) made with ggplot2 as SVG. Tried it with the Cairo package but the outcome is bad. library(ggplot2) library(grid) library(Cairo) ... #png(output_file, width=800,…
Matthias Munz
  • 3,583
  • 4
  • 30
  • 47
83
votes
4 answers

How do I change the number of decimal places on axis labels in ggplot2?

Specifically, this is in a facet_grid. Have googled extensively for similar questions but not clear on the syntax or where it goes. What I want is for every number on the y-axes to have two digits after the decimal, even if the trailing one is 0. Is…
Peter Lewis
  • 975
  • 2
  • 8
  • 8
81
votes
5 answers

Create stacked barplot where each stack is scaled to sum to 100%

I have a data.frame like this: df <- read.csv(text = "ONE,TWO,THREE 23,234,324 34,534,12 56,324,124 34,234,124 123,534,654") I want…
Julio Diaz
  • 9,067
  • 19
  • 55
  • 70
81
votes
5 answers

Combine base and ggplot graphics in R figure window

I would like to generate a figure that has a combination of base and ggplot graphics. The following code shows my figure using the base plotting functions of R: t <- c(1:(24*14)) P <- 24 A <- 10 y <-…
KatyB
  • 3,920
  • 7
  • 42
  • 72