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.
36,738
questions
703
votes
7answers
81k 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)? ...
699
votes
8answers
799k 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 ...
589
votes
16answers
1.4m views
Plot two graphs in same plot in R
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 ...
366
votes
2answers
582k 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$...
348
votes
13answers
404k 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 ...
311
votes
5answers
483k 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 = ...
311
votes
12answers
544k 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 = c(rnorm(200), ...
306
votes
14answers
344k 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 ...
306
votes
11answers
363k views
How to save a plot as 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 ...
267
votes
3answers
354k views
Center Plot title in ggplot2
Hi this simple code (and all my scripts from this morning) has started giving me a off center title in ggplot2
Ubuntu version: 16.04
R studio version: Version 0.99.896
R version: 3.3.2
GGPLOT2 ...
260
votes
4answers
370k 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....
238
votes
15answers
315k 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) ...
236
votes
20answers
243k 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, ...
231
votes
9answers
270k 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 = ...
226
votes
2answers
180k 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 ...
224
votes
1answer
398k 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 ...
199
votes
4answers
131k 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:
195
votes
7answers
484k 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 ...
194
votes
5answers
250k 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 ...
183
votes
3answers
285k 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 ...
182
votes
6answers
25k 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 ...
179
votes
5answers
209k 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 ...
178
votes
15answers
26k 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 ...
176
votes
8answers
223k 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 ...
175
votes
5answers
219k 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))...
164
votes
2answers
128k 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 ...
152
votes
4answers
396k 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)
...
151
votes
6answers
83k 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 + ...
145
votes
3answers
274k 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)
require(...
144
votes
3answers
123k 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.
143
votes
6answers
169k 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'.
142
votes
9answers
115k 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.
...
141
votes
6answers
92k 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 ...
138
votes
5answers
248k views
Order discrete x scale by frequency/value
I am making a dodged bar chart using ggplot with discrete x scale, the x axis are now arranged in alphabetical order, but I need to rearrange it so that it is ordered by the value of the y-axis (i.e., ...
137
votes
14answers
58k 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,...
134
votes
5answers
131k views
How do I change the formatting of numbers on an axis with ggplot?
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 ...
133
votes
7answers
147k 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 ...
132
votes
4answers
181k 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, ...
131
votes
5answers
317k 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=...
126
votes
1answer
112k 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?
126
votes
6answers
29k 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 = ...
125
votes
3answers
148k views
Overlaying histograms with ggplot2 in R
I am new to R and am trying to plot 3 histograms onto the same graph.
Everything worked fine, but my problem is that you don't see where 2 histograms overlap - they look rather cut off.
When I make ...
123
votes
3answers
93k views
How to make graphics with transparent background in R using ggplot2?
I need to output ggplot2 graphics from R to PNG files with transparent background. Everything is ok with basic R graphics, but no transparency with ggplot2:
d <- rnorm(100) #generating random data
...
122
votes
5answers
322k 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))
...
122
votes
1answer
77k 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
...
122
votes
1answer
144k views
Reorder bars in geom_bar ggplot2
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 ...
122
votes
2answers
335k 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 ...
121
votes
8answers
94k 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 ...
120
votes
6answers
125k views
Persistent invalid graphics state error when using ggplot2
I believe my dataframe is okay and my code is okay. In fact, I have eliminated parts of the dataframe and most of the graphing code to make things as basic as possible. But still, I get:
Error in ....
119
votes
1answer
245k views
adding x and y axis labels in ggplot2
How do I change the x and y labels on this graph please?
library(Sleuth2)
library(ggplot2)
discharge<-ex1221new$Discharge
area<-ex1221new$Area
nitrogen<-ex1221new$NO3
p <- ggplot(...