ggplot2

modify lm or loess function to use it within ggplot2's geom_smooth

I need to modify the lm (or eventually loess) function so I can use it in ggplot2's geom_smooth (or stat_smooth). For example, this is how stat_smooth is used normally: > qplot(data=diamonds, carat, price, facets=~clarity) + stat_smooth(method='lm')` I would like to define a custom 'lm2' function to use as value for the 'method' para...

scatterplot: how to color points in a different color if a data attribute is not null

I have a scatter plot in R (with ggplot2). The data has a numeric column (let's call it "bin") which can contain various integer values or null. I would like to colour the points with non-null bin values differently from the others. I do not want to one colour per value of bin, that would be too noisy. Just simply, say, red for those wi...

Create qplots (with ggplot2) larger than the window?

I have a large chart with many data points. When I create the qplot in R, the chart is auto-fitted to the window. Even if I maximize the window, the chart is still too small and details are lost. I would like to save it as a large PNG and then look at certain areas at 1:1 resolution with an image viewer (as I cannot zoom in easily in R)....

How can a data ellipse be superimposed on a ggplot2 scatterplot?

Hi, I have an R function which produces 95% confidence ellipses for scatterplots. The output looks like this, having a default of 50 points for each ellipse (50 rows): [,1] [,2] [1,] 0.097733810 0.044957994 [2,] 0.084433494 0.050337990 [3,] 0.069746783 0.054891438 I would like to superimpose a number of suc...

Greek letters in ggplot strip text

I'm trying to override the text in some ggplot strips to incorporate Greek characters. Here's some sample data, and the base for the plot. dfr <- data.frame( x = rep(1:10, times = 6), y = runif(60), fx = rep(c("foo", "bar"), each = 30), fy = rep(c("alpha", "beta", "gamma"), each = 10, times = 2) ) p <- ggplot(dfr, aes(x, y...

How to adjust time scale axis for ggplot histogram

I am working with a data frame where one of the columns consists of POSIXct date-time values. I am trying to plot a histogram of these timestamps using ggplot2 but I'm having two issues: I don't know how to set the binwidth in geom_histogram(). I'd like to set each bin to a day or a week. I've tried providing a difftime object, but ...

How to nicely annotate a ggplot2 (manual)

Using ggplot I normally use geom_text and something like position=jitter to annotate my plots. However - for a nice plot I often finds it worthwhile to annotate manually. like below: data2 <- structure(list(type = structure(c(5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L), .Label = c("EDS", "KIU", "LA...

Manual annotate a ggplot with different labels, in different facets

JD Long helped me with this: question about manual annotation. But is it possible to do something similar on a facetted plot, such that the label style corresponds to the linestyle (aestetics) and in a way that I can annotate different facets individually? Some data funny <- structure(list(Institution = structure(c(1L, 1L, 1L, 1L, 2L, ...

How do I change the stacking order in a bar chart in ggplot2?

From the online bar chart guide: qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear)) How do I get 5 to sit on the bottom, 4 above that, and 3 on top? ...

R ggplot2: Arrange facet_grid by non-facet column (and labels using non-facet column)

I have a couple of questions regarding facetting in ggplot2... Let's say I have a query that returns data that looks like this: (note that it's ordered by Rank asc, Alarm asc and two Alarms have a Rank of 3 because their Totals = 1798 for Week 4, and Rank is set according to Total for Week 4) Rank Week Alarm To...

How does one overcome overlapping points without jitter or transparency in ggplot2

I am starting to use ggplot2. I have some small n (about 30 or so) granular data with lots of overlap. Neither jitter nor alpha (transparency) are suitable. Instead a stripchart with stack and offset do it best but I do not know how to do it in ggplot2. Do you know? To see what the end result should be click on this graphic. Here is th...

Annotating axis in ggplot2

I am looking for the way to annotate axis in ggplot2. The example of the problem can be found here: http://learnr.wordpress.com/2009/09/24/ggplot2-back-to-back-bar-charts. The y axis of the chart (example graph in the link) has an annotation: (million euro). Is there a way to create such types of annotations in ggplot2? Looking at the ...

How can one create a double horizontal axis for a plot in ggplot2?

I need to show the values on the horizontal axis (aka abscissa, aka x-axis) converted into other units. Hence the need for a second axis. ...

How can I superimpose modified loess lines on a ggplot2 qplot?

Background Right now, I'm creating a multiple-predictor linear model and generating diagnostic plots to assess regression assumptions. (It's for a multiple regression analysis stats class that I'm loving at the moment :-) My textbook (Cohen, Cohen, West, and Aiken 2003) recommends plotting each predictor against the residuals to make s...

how to define fill colours in ggplot histogram?

I have the following simple data data <- structure(list(status = c(9, 5, 9, 10, 11, 10, 8, 6, 6, 7, 10, 10, 7, 11, 11, 7, NA, 9, 11, 9, 10, 8, 9, 10, 7, 11, 9, 10, 9, 9, 8, 9, 11, 9, 11, 7, 8, 6, 11, 10, 9, 11, 11, 10, 11, 10, 9, 11, 7, 8, 8, 9, 4, 11, 11, 8, 7, 7, 11, 11, 11, 6, 7, 11, 6, 10, 10, 9, 10, 10, 8, 8, 10, 4, 8, 5, 8, 7)...

Generate multiple graphics from within an R function

I'd like to spawn several graphics windows from within a function in R using ggplot graphics... testf <- function(a, b) { devAskNewPage(TRUE) qplot(a, b); # grid.newpage(recording = TRUE) dev.new() qplot(a, a+a); # grid.newpage(recording = TRUE) dev.new() qplot(b, b+b); } library(ggplot2) x <- rnorm(50) y <- rnorm(50) ...

Show frequencies along with barplot in ggplot2

I'm trying to display frequencies within barplot ... well, I want them somewhere in the graph: under the bars, within bars, above bars or in the legend area. And I recall (I may be wrong) that it can be done in ggplot2. This is probably an easy one... at least it seems easy. Here's the code: p <- ggplot(mtcars) p + aes(factor(cyl)) + ge...

How can I collapse a dataframe by some variables, taking mean across others

I need to summarize a data frame by some variables, ignoring the others. This is sometimes referred to as collapsing. E.g. if I have a dataframe like this: Widget Type Energy egg 1 20 egg 2 30 jap 3 50 jap 1 60 Then collapsing by Widget, with Energy the dependent variable, Energy~Widget, would yield Widget Energy ...

for (i in xxx) ggplot problem

This is strange - I think? library(ggplot2) tf <- which(sapply(diamonds, is.factor)) diamonds.tf <- diamonds[,tf] So far so good. But next comes the trouble: pl.f <- ggplot(diamonds.tf, aes(x=diamonds.tf[,i]))+ geom_bar()+ xlab(names(diamonds.tf[i])) for (i in 1:ncol(diamonds.tf)) { ggsave(paste("plot.f",i,".png",sep=""), plot=pl.f,...

How to better create stacked bar graphs with multiple variables from ggplot2?

I often have to make stacked barplots to compare variables, and because I do all my stats in R, I prefer to do all my graphics in R with ggplot2. I would like to learn how to do two things: First, I would like to be able to add proper percentage tick marks for each variable rather than tick marks by count. Counts would be confusing, whi...