Skip to main content

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.

Filter by
Sorted by
Tagged with
912 votes
8 answers
1.6m views

Rotating and spacing axis labels in ggplot2

I have a plot where the x-axis is a factor whose labels are long. While probably not an ideal visualization, for now I'd like to simply rotate these labels to be vertical. I've figured this part out ...
Christopher DuBois's user avatar
768 votes
7 answers
93k views

How can we make xkcd style graphs?

Apparently, folk have figured out how to make xkcd style graphs in Mathematica and in LaTeX. Can we do it in R? Ggplot2-ers? A geom_xkcd and/or theme_xkcd? I guess in base graphics, par(xkcd=TRUE)? ...
jebyrnes's user avatar
  • 9,222
690 votes
17 answers
1.9m views

Plot two graphs in a same plot

I would like to plot y1 and y2 in the same plot. x <- seq(-2, 2, 0.05) y1 <- pnorm(x) y2 <- pnorm(x, 1, 1) plot(x, y1, type = "l", col = "red") plot(x, y2, type = "l", col = "green") But ...
Sandra Schlichting's user avatar
532 votes
14 answers
1.4m views

How to change legend title in ggplot

I have the following plot like below. It was created with this command: library(ggplot2) df <- data.frame(cond = factor(rep(c("A", "B"), each = 200)), rating =...
neversaint's user avatar
  • 63.1k
450 votes
14 answers
693k views

Side-by-side plots with ggplot2

I would like to place two plots side by side using the ggplot2 package, i.e. do the equivalent of par(mfrow=c(1,2)). For example, I would like to have the following two plots show side-by-side with ...
Christopher DuBois's user avatar
440 votes
2 answers
767k views

How to set limits for axes in ggplot2 R plots?

I plot the following: library(ggplot2) carrots <- data.frame(length = rnorm(500000, 10000, 10000)) cukes <- data.frame(length = rnorm(50000, 10000, 20000)) carrots$veg <- 'carrot' cukes$...
David B's user avatar
  • 29.7k
399 votes
5 answers
719k views

Center Plot title in ggplot2

This simple code (and all my scripts from this morning) has started giving me an off center title in ggplot2: Ubuntu version: 16.04 R studio version: Version 0.99.896 R version: 3.3.2 GGPLOT2 ...
Christopher John's user avatar
393 votes
16 answers
583k views

Order Bars in ggplot2 bar graph

I am trying to make a bar graph where the largest bar would be nearest to the y axis and the shortest bar would be furthest. So this is kind of like the Table I have Name Position 1 James ...
Julio Diaz's user avatar
  • 9,297
376 votes
4 answers
601k views

Remove legend ggplot 2.2

I'm trying to keep the legend of one layer (smooth) and remove the legend of the other (point). I have tried shutting off the legends with guides(colour = FALSE) and geom_point(aes(color = vs), show....
Guy's user avatar
  • 3,785
371 votes
5 answers
642k views

Plotting two variables as lines using ggplot2 on the same graph

A very newbish question, but say I have data like this: test_data <- data.frame( var0 = 100 + c(0, cumsum(runif(49, -20, 20))), var1 = 150 + c(0, cumsum(runif(49, -10, 10))), date = ...
fmark's user avatar
  • 58.1k
357 votes
11 answers
462k views

How can I save a plot as an image on the disk?

I plot a simple linear regression using R. I would like to save that image as PNG or JPEG. Is it possible to do it automatically? (via code) There are two different questions: First, I am already ...
blakc05's user avatar
  • 3,739
349 votes
19 answers
571k views

ggplot with 2 y axes on each side and different scales

I need to plot a bar chart showing counts and a line chart showing rate all in one chart, I can do both of them separately, but when I put them together, I scale of the first layer (i.e. the geom_bar) ...
lokheart's user avatar
  • 24.4k
339 votes
1 answer
768k views

Remove all of x axis labels in ggplot [duplicate]

I need to remove everything on the x-axis including the labels and tick marks so that only the y-axis is labeled. How would I do this? In the image below I would like 'clarity' and all of the tick ...
Vedda's user avatar
  • 7,316
329 votes
10 answers
493k views

Add regression line equation and R^2 on graph

I wonder how to add regression line equation and R^2 on the ggplot. My code is: library(ggplot2) df <- data.frame(x = c(1:100)) df$y <- 2 + 3 * df$x + rnorm(100, sd = 40) p <- ggplot(data = ...
MYaseen208's user avatar
  • 23.6k
311 votes
24 answers
432k views

How to change facet labels?

I have used the following ggplot command: ggplot(survey, aes(x = age)) + stat_bin(aes(n = nrow(h3), y = ..count.. / n), binwidth = 10) + scale_y_continuous(formatter = "percent", breaks = c(0, 0.1, ...
wishihadabettername's user avatar
306 votes
2 answers
267k views

Turning off some legends in a ggplot

Suppose I have a ggplot with more than one legend. mov <- subset(movies, length != "") (p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) + geom_point() ) I can turn off ...
Richie Cotton's user avatar
284 votes
6 answers
436k views

Increase number of axis ticks

I'm generating plots for some data, but the number of ticks is too small, I need more precision on the reading. Is there some way to increase the number of axis ticks in ggplot2? I know I can tell ...
João Daniel's user avatar
  • 8,886
271 votes
7 answers
781k views

Changing font size and direction of axes text in ggplot2

I am plotting a graph with a categorical variable on the x axis and a numerical variable on the y axis. For the x axis, given that there are many data points, the default text formatting causes the ...
Anjan's user avatar
  • 2,938
267 votes
5 answers
228k views

Emulate ggplot2 default color palette

What function can I use to emulate ggplot2's default color palette for a desired number of colors. For example, an input of 3 would produce a character vector of HEX colors with these colors:
Suraj's user avatar
  • 36.2k
258 votes
6 answers
435k views

ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"

With this data frame ("df"): year pollution 1 1999 346.82000 2 2002 134.30882 3 2005 130.43038 4 2008 88.27546 I try to create a line chart like this: plot5 <- ggplot(df, aes(year, pollution))...
megashigger's user avatar
  • 8,893
252 votes
6 answers
768k views

Change size of axes title and labels in ggplot2

I have a really simple question, which I am struggling to find the answer to. I hoped someone here might be able to help me. An example dataframe is presented below: a <- c(1:10) b <- c(10:1) ...
KT_1's user avatar
  • 8,424
244 votes
3 answers
527k views

Label points in geom_point

The data I'm playing with comes from the internet source listed below nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv", sep=",") What I want to do, is create a 2D points graph ...
Green Demon's user avatar
  • 4,158
234 votes
6 answers
170k views

Annotating text on individual facet in ggplot2

I want to annotate some text on last facet of the plot with the following code: library(ggplot2) p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() p <- p + facet_grid(. ~ cyl) p <- p + ...
MYaseen208's user avatar
  • 23.6k
220 votes
5 answers
315k views

How to assign colors to categorical variables in ggplot2 that have stable mapping?

I've been getting up to speed with R in the last month. Here is my question: What is a good way to assign colors to categorical variables in ggplot2 that have stable mapping? I need consistent ...
wintour's user avatar
  • 2,325
219 votes
8 answers
31k views

How can I handle R CMD check "no visible binding for global variable" notes when my ggplot2 syntax is sensible?

EDIT: Hadley Wickham points out that I misspoke. R CMD check is throwing NOTES, not Warnings. I'm terribly sorry for the confusion. It was my oversight. The short version R CMD check throws this note ...
briandk's user avatar
  • 6,889
216 votes
2 answers
240k views

What do hjust and vjust do when making a plot using ggplot?

Every time I make a plot using ggplot, I spend a little while trying different values for hjust and vjust in a line like + opts(axis.text.x = theme_text(hjust = 0.5)) to get the axis labels to line ...
William Gunn's user avatar
  • 2,965
212 votes
9 answers
271k views

Add a common Legend for combined ggplots

I have two ggplots which I align horizontally with grid.arrange. I have looked through a lot of forum posts, but everything I try seem to be commands that are now updated and named something else. ...
jO.'s user avatar
  • 3,472
211 votes
6 answers
127k views

How to use a variable to specify column name in ggplot

I have a ggplot command ggplot( rates.by.groups, aes(x=name, y=rate, colour=majr, group=majr) ) inside a function. But I would like to be able to use a parameter of the function to pick out the ...
Theodore Norvell's user avatar
208 votes
9 answers
291k views

Show percent % instead of counts in charts of categorical variables

I'm plotting a categorical variable and instead of showing the counts for each category value. I'm looking for a way to get ggplot to display the percentage of values in that category. Of course, it ...
wishihadabettername's user avatar
205 votes
4 answers
266k views

Force the origin to start at 0

How can I set the origin / interception of the y-axis and x-axis in ggplot2? The line of the x-axis should be exactly at y=Z. With Z=0 or another given value.
Jonas Stein's user avatar
  • 6,953
200 votes
9 answers
280k views

Ignore outliers in ggplot2 boxplot

How would I ignore outliers in ggplot2 boxplot? I don't simply want them to disappear (i.e. outlier.size=0), but I want them to be ignored such that the y axis scales to show 1st/3rd percentile. My ...
Suraj's user avatar
  • 36.2k
200 votes
8 answers
73k views

Remove 'a' from legend when using aesthetics and geom_text

How can I can remove the letter 'a' from the legend generated by this code? If I remove the geom_text, then the 'a' letter will not show in the legend. I want to keep geom_text, though. ggplot(data = ...
user2700207's user avatar
  • 2,043
195 votes
7 answers
637k views

Adding a regression line on a ggplot

I'm trying hard to add a regression line on a ggplot. I first tried with abline but I didn't manage to make it work. Then I tried this... data = data.frame(x.plot=rep(seq(1,5),10),y.plot=rnorm(50)) ...
Remi.b's user avatar
  • 18k
194 votes
2 answers
686k views

Editing legend (text) labels in ggplot

I have spent hours looking in the documentation and on StackOverflow, but no solution seems to solve my problem. When using ggplot I can't get the right text in the legend, even though it's in my ...
user3633161's user avatar
  • 2,043
193 votes
4 answers
316k views

increase legend font size ggplot2

Is there a way to increase the font size in ggplot2? I think I need to specify something like legend.key.width = unit(2, "line") in the theme function, but that is used to adjust the keys in legends, ...
alittleboy's user avatar
  • 10.8k
192 votes
7 answers
601k views

How to change line width in ggplot?

Datalink: the data used My code: ccfsisims <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_NewAggDatabase/NewFiles/GTAP_ConsIndex.csv", header=...
iouraich's user avatar
  • 3,014
188 votes
9 answers
235k views

ggplot2 plot without axes, legends, etc

I want to use bioconductor's hexbin (which I can do) to generate a plot that fills the entire (png) display region - no axes, no labels, no background, no nuthin'.
user1320487's user avatar
  • 2,119
187 votes
9 answers
193k views

Is there a way to change the spacing between legend items in ggplot2?

Is there a way to change the spacing between legend items in ggplot2? I currently have legend.position ="top" which automatically produces a horizontal legend. However, the spacing of the items is ...
overflowname's user avatar
  • 2,153
186 votes
3 answers
278k views

Reorder bars in geom_bar ggplot2 by value

I am trying to make a bar-plot where the plot is ordered from the miRNA with the highest value to the miRNA with the lowest. Why does my code not work? > head(corr.m) miRNA ...
user3741035's user avatar
  • 2,525
184 votes
10 answers
179k views

Force R to stop plotting abbreviated axis labels (scientific notation) - e.g. 1e+00

In ggplot2 how can I stop axis labels being abbreviated - e.g. 1e+00, 1e+01 along the x axis once plotted? Ideally, I want to force R to display the actual values which in this case would be 1,10. ...
JPD's user avatar
  • 2,631
184 votes
4 answers
406k views

Add legend to ggplot2 line plot

I have a question about legends in ggplot2. I managed to plot three lines in the same graph and want to add a legend with the three colors used. This is the code used library(ggplot2) ## edit from ...
pacomet's user avatar
  • 5,111
183 votes
15 answers
27k views

Most underused data visualization [closed]

Histograms and scatterplots are great methods of visualizing data and the relationship between variables, but recently I have been wondering about what visualization techniques I am missing. What do ...
181 votes
2 answers
127k views

ggplot: How to increase spacing between faceted plots?

I have several faceted histograms (obtained with the command below) which are nicely plotted one under the other. I would like to increase the spacing between them, however, they are tight. I looked ...
wishihadabettername's user avatar
177 votes
3 answers
175k views

Remove space between plotted data and the axes

I have the following dataframe: uniq <- structure(list(year = c(1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, ...
Jaap's user avatar
  • 82.6k
171 votes
4 answers
302k views

Setting individual axis limits with facet_wrap and scales = "free" in ggplot2

I'm creating a facetted plot to view predicted vs. actual values side by side with a plot of predicted value vs. residuals. I'll be using shiny to help explore the results of modeling efforts using ...
Hendy's user avatar
  • 10.5k
170 votes
7 answers
142k views

Saving grid.arrange() plot to file

I am trying to plot multiple plots using ggplot2, arranging them using grid.arrange(). Since I managed to find someone describing the exact problem I have, I have quoted from the problem description ...
I Like to Code's user avatar
168 votes
1 answer
136k views

ggplot does not work if it is inside a for loop although it works outside of it [duplicate]

I'm using a simple ggplot function which works fine outside a loop but not inside even if the iterative value does not interfere with the ggplot function. Why is it so ? Here is my code x=1:7 y=1:7 ...
Remi.b's user avatar
  • 18k
166 votes
14 answers
93k views

Scatterplot with marginal histograms in ggplot2

Is there a way of creating scatterplots with marginal histograms just like in the sample below in ggplot2? In Matlab it is the scatterhist() function and there exist equivalents for R as well. However,...
Seb's user avatar
  • 5,487
164 votes
5 answers
212k views

How do I change the formatting of numbers on an axis with ggplot? [duplicate]

I'm using R and ggplot to draw a scatterplot of some data, all is fine except that the numbers on the y-axis are coming out with computer style exponent formatting, i.e. 4e+05, 5e+05, etc. This is ...
Jack Aidley's user avatar
  • 19.9k
164 votes
1 answer
172k views

facet label font size [duplicate]

Is there a way to change the font size of facet labels in ggplot? I googled and found that the issue was yet on Hadley's to-do list. I wonder if there is a workaround or any news on this issue?
Matt Bannert's user avatar
  • 28.1k

1
2 3 4 5
1137