Questions tagged [ggplot2]

ggplot2 is an enhanced plotting library for R based upon the principles of "The Grammar of Graphics". Use this tag for *on topic* questions that (a) involve `ggplot2` as a critical part of the question &/or expected answer, & (b) are not just about how to use `ggplot2`.

ggplot2 is an actively maintained, open-source chart-drawing library for , written by Hadley Wickham, based upon the principles of "The Grammar of Graphics" by Leland Wilkinson. It can stand as an alternative to R's basic plot and the lattice package.

Resources:

There are several related packages on CRAN that are based upon, or extend ggplot2's functionality:

  • GGally: Package containing templates for different plots to be combined into a plot matrix, as well as a parallel coordinate plot function
  • 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 ggplot2
  • 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, OpenStreetMaps, Stamen Maps, or CloudMade Maps using ggplot2
  • Finally, the package gridExtra is often useful, as ggplot2 is based upon the grid graphics system.
233 questions
59
votes
2 answers

How can I change the title of a legend in ggplot2?

I have a plot I'm making in ggplot2 to summarize data that are from a 2 x 4 x 3 celled dataset. I have been able to make panels for the 2-leveled variable using facet_grid(. ~ Age) and to set the x and y axes using aes(x=4leveledVariable, y=DV). I…
russellpierce
  • 17,079
  • 16
  • 67
  • 98
55
votes
6 answers

How to determine best cutoff point and its confidence interval using ROC curve in R?

I have the data of a test that could be used to distinguish normal and tumor cells. According to ROC curve it looks good for this purpose (area under curve is 0.9): My questions are: How to determine cutoff point for this test and its confidence…
Yuriy Petrovskiy
  • 4,081
  • 7
  • 25
  • 30
36
votes
3 answers

How to draw neat polygons around scatterplot regions in ggplot2

How do I add a neat polygon around a group of points on a scatterplot? I am using ggplot2 but am disappointed with the results of geom_polygon. The dataset is over there, as a tab-delimited text file. The graph below shows two measures of attitudes…
Fr.
  • 1,343
  • 3
  • 11
  • 22
29
votes
5 answers

Are gridlines and grey backgrounds chartjunk and should they be used only on an exception basis?

It seems that most authorities agree that dark or otherwise prominent gridlines in plots are "chartjunk" by any reasonable definition and distract the viewer from the message in the main body of the chart. So I won't bother to give references on…
Peter Ellis
  • 16,522
  • 1
  • 44
  • 82
28
votes
1 answer

Dropping unused levels in facets with ggplot2

Is it possible to drop levels that are not used in ggplot2s facets? This is my code: tab = as.data.frame(cbind(groups = mtcars$cyl, names = row.names(mtcars), val = mtcars$mpg, N = mtcars$disp)) tab$N = as.numeric(tab$N) ggplot(tab, aes(names,val))…
mrrrau
  • 395
  • 1
  • 3
  • 4
20
votes
2 answers

How to predict or extend regression lines in ggplot2?

I have a data frame that contains two time series: the dates and version numbers of Emacs and Firefox releases. Using one ggplot2 command it's easy to make a chart that uses loess (in a way that looks a bit amusing, which I don't mind) to turn the…
William Denton
  • 303
  • 1
  • 2
  • 6
19
votes
2 answers

R: geom_density values in y-axis

Why geom_density is showing me values higher than 1 in the density plot? How to change it into fraction? And my code used to generate the plot ggplot(data = input2, aes(x = r.close)) + geom_density(aes(y = ..density.., fill = `Próba`), alpha =…
kodi1911
  • 440
  • 2
  • 4
  • 10
19
votes
1 answer

How can I 'dodge' the position of geom_point in ggplot2?

I am using ggplot2 in R to make plots like the following ones: The errorbars overlap with each other which look really messy. How can I separate the errorbars for different indices? I have used position="dodge" but it seems to be not working. Here…
ycc
  • 411
  • 1
  • 3
  • 6
17
votes
2 answers

Ink to data ratio and plot backgrounds

I have noticed that many "state of the art" plotting packages and themes and many distinguished data folks use grey background for their plots. Here are several examples: ggplot2: Nate Silver's fivethirtyeight.com: While in the first example above…
David D
  • 717
  • 5
  • 13
16
votes
1 answer

How does ggplot compute confidence intervals for regressions?

The R plotting package ggplot2 has an awesome function called stat_smooth for plotting a regression line (or curve) with the associated confidence band. However I am having a hard time figuring out exactly how this confidence band is generated, for…
static_rtti
  • 745
  • 1
  • 11
  • 24
15
votes
1 answer

How to interpret notched box plots

While doing some EDA I decided to use a box plot to illustrate the difference between two levels of a factor. The way ggplot rendered the box plot was satisfactory, but slightly simplistic (first plot below). Whilst researching the characteristics…
RDJ
  • 485
  • 1
  • 4
  • 14
15
votes
1 answer

How to plot a stair steps function with ggplot?

I have the graph like this: R code for generating it is: DF <- data.frame(date = as.Date(runif(100, 0, 800),origin="2005-01-01"), outcome = rbinom(100, 1, 0.1)) DF <- DF[order(DF$DateVariable),] #Sort by date DF$x <-…
Yuriy Petrovskiy
  • 4,081
  • 7
  • 25
  • 30
14
votes
4 answers

Boxplot equivalent for heavy-tailed distributions?

For approximately normally distributed data, boxplots are a great way to quickly visualize the median and spread of the data, as well as the presence of any outliers. However for more heavy-tailed distributions, a lot of points are shown as…
14
votes
2 answers

How to draw fitted graph and actual graph of gamma distribution in one plot?

Load the package needed. library(ggplot2) library(MASS) Generate 10,000 numbers fitted to gamma distribution. x <- round(rgamma(100000,shape = 2,rate = 0.2),1) x <- x[which(x>0)] Draw the probability density function, supposed we don't know which…
13
votes
4 answers

How to draw funnel plot using ggplot2 in R?

As title, I need to draw something like this: Can ggplot, or other packages if ggplot is not capable, be used to draw something like this?
lokheart
  • 2,999
  • 7
  • 37
  • 47
1
2 3
15 16