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

ggplot2: facet_wrap strip color based on variable in data set

Is there a way to fill the strips of facets created with facet_wrap based on a variable supplied with the data frame? Example data: MYdata <- data.frame(fruit = rep(c("apple", "orange", "plum", "banana", "pear", "grape")), farm =…
Dalmuti71
  • 1,509
  • 3
  • 15
  • 19
77
votes
1 answer

Manually setting group colors for ggplot2

I have a data set from which I have to make several dozen plots. The data consists of 30 Groups and several measurements within each group. Most plots will not use all the groups at once. My goal is to have one single color pallete for the set of…
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
77
votes
1 answer

Control ggplot2 legend look without affecting the plot

I'm plotting lines with ggplot2 like this: ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + theme_bw() . I find legend marks to be small so I want them to be bigger. If I change the size, lines on the plot change…
baltazar
  • 1,659
  • 2
  • 18
  • 23
77
votes
4 answers

Controlling the order of points in ggplot2?

I'm plotting a dense scatter plot in ggplot2 where each point might be labeled by a different color: df <- data.frame(x=rnorm(500)) df$y = rnorm(500)*0.1 + df$x df$label <- c("a") df$label[50] <- "point" df$size <- 2 ggplot(df) +…
user248237
76
votes
4 answers

How to put labels over geom_bar in R with ggplot2

I'd like to have some labels stacked on top of a geom_bar graph. Here's an example: df <- data.frame(x=factor(c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE))) ggplot(df) + geom_bar(aes(x,fill=x)) +…
angerman
  • 4,099
  • 5
  • 24
  • 25
76
votes
4 answers

geom_rect and alpha - does this work with hard coded values?

Same title, completely reworded the question though. Why does the alpha work in the first plot but not the second? I'm struggling to see why with hardcoded values the rect is drawn in the right place but not made transparent but when in a data.frame…
nzcoops
  • 9,132
  • 8
  • 41
  • 52
76
votes
5 answers

R knitr Markdown: Output Plots within For Loop

I would like to create an automated knitr report that will produce histograms for each numeric field within my dataframe. My goal is to do this without having to specify the actual fields (this dataset contains over 70 and I would also like to reuse…
bnjmn
  • 4,508
  • 4
  • 37
  • 52
75
votes
2 answers

Change facet label text and background colour

How can I change the grey facet labels (A and B) into say red background with white text? library(data.table) A = data.table(x = 1:4, y = 1:4, z = c('A','A','B','B')) ggplot(A) + geom_point(aes(x = x, y = y)) + facet_wrap(~z) + theme_bw()
jf328
  • 6,841
  • 10
  • 58
  • 82
75
votes
3 answers

Adding custom image to geom_polygon fill in ggplot

I was asked by a student if it was possible to recreate a plot similar to the one below using R: This is from this paper.... This sort of stuff isn't my specialty, but using the following code I was able to create 95% CI ellipses and to plot…
jalapic
  • 13,792
  • 8
  • 57
  • 87
75
votes
2 answers

Formatting dates with scale_x_date in ggplot2

In a previous version of ggplot2, I was able to use one of the two following commands to format my x dates: Either scale_x_date(breaks = "1 month", minor_breaks = "1 week", labels=(date_format="%B")) + or scale_x_date(major="months", minor="weeks",…
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
75
votes
5 answers

Place a border around points

I'd like to place a black border around points on a scatterplot that are filled based on data, using ggplot2. Also, I would like to avoid having a legend entry for the black border since it will be on each point. Basically I'm looking for this plot,…
Drew Steen
  • 16,045
  • 12
  • 62
  • 90
74
votes
3 answers

How to italicize part (one or two words) of an axis title

Is there any way to change the style of part of an axis title while keep the rest part unchanged? In my case, How could I italicize "bacteria X" in the y-axis title? To my knowledge, the command theme(axis.title.y=element_text(face="italic")) can…
eze
  • 741
  • 1
  • 5
  • 3
74
votes
3 answers

Add panel border to ggplot2

I have been asked to place a full border around my plot below: Using panel.border = element_rect(colour = "black") results in losing in the plot becoming blank. I can't use theme_bw() as it does not have the same functionality as the usual theme,…
Methexis
  • 2,739
  • 5
  • 24
  • 34
74
votes
4 answers

Legend placement, ggplot, relative to plotting region

Problem here is a bit obvious I think. I'd like the legend placed (locked) in the top left hand corner of the 'plotting region'. Using c(0.1,0.13) etc is not an option for a number of reasons. Is there a way to change the reference point for the…
nzcoops
  • 9,132
  • 8
  • 41
  • 52
73
votes
4 answers

Multi-row x-axis labels in ggplot line chart

Edit: This question has been marked as duplicated, but the responses here have been tried and did not work because the case in question is a line chart, not a bar chart. Applying those methods produces a chart with 5 lines, 1 for each year - not…
jlhoward
  • 58,004
  • 7
  • 97
  • 140