ggplot2

Side-by-side plots with ggplot2 in R

I would like to place two plots side by side using the ggplot2 package (ie. 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 the same scale. x <- rnorm(100) eps <- rnorm(100,0,.2) qplot(x,3*x+eps) qplot(x,2*x+eps) Do I need to put them in the same data.frame lik...

plotting two vectors of data on a GGPLOT2 scatter plot using R

I've been experimenting with both GGPLOT2 and lattice to graph panels of data. I'm having a little trouble wrapping my mind around the GGPLOT2 model. In particular, how do I plot a scatter plot with two sets of data on each panel: in Lattice I could do this: xyplot(Predicted_value + Actual_value ~ x_value | State_CD, data=dd) and tha...

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 with the code below, but as you can see, the labels aren't totally visible. data(diamonds) diamonds$cut <- paste("Super Dee-Duper",as.cha...

Getting foreach() and ggplot2 to get along

I have a set of survey data, and I'd like to generate plots of a particular variable, grouped by the respondent's country. The code I have written to generate the plots so far is: countries <- isplit(drones, drones$v3) foreach(country = countries) %dopar% { png(file = paste(output.exp, "/Histogram of Job Satisfaction in ", country$ke...

using stat_function and facet_wrap together in GGPLOT2 in R

I am trying to plot lattice type data with GGPLOT2 and then superimpose a normal distribution over the sample data to illustrate how far off normal the underlying data is. I would like to have the normal dist on top to have the same mean and stdev as the panel. here's an example: library(ggplot2) #make some example data dd<-data.fram...

How do I make (ugh) bull's-eye charts in R?

A colleague of mine needs to plot 101 bull's-eye charts. This is not her idea. Rather than have her slave away in Excel or God knows what making these things, I offered to do them in R; mapping a bar plot to polar coordinates to make a bull's-eye is a breeze in ggplot2. I'm running into a problem, however: the data is already aggregat...

More bullseye plotting in R (I'm so sorry)

I'm using ggplot2 to make some bullseye charts in R. They look delightful, and everyone is very pleased - except that they'd like to have the values of the bullseye layers plotted on the chart. I'd be happy just to put them in the lower-right corner of the plot, or even in the plot margins, but I'm having some difficulty doing this. H...

Boxplot schmoxplot: How to plot means and standard errors conditioned by a factor in R?

We all love robust measures like medians and interquartile ranges, but lets face it, in many fields, boxplots almost never show up in published articles, while means and standard errors do so all the time. It's simple in lattice, ggplot2, etc to draw boxplots and the galleries are full of them. Is there an equally straightforward way to...

How to overlay a line for an lm object on a ggplot2 scatterplot

I have some data, calvarbyruno.1<-structure(list(Nominal = c(1, 3, 6, 10, 30, 50, 150, 250), Run = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3"), class = "factor"), PAR = c(1.25000000000000e-05, 0.000960333333333333, 0.00205833333333334, 0.00423333333333333, 0.0322333333333334, 0.614433333333334, ...

Changing shapes used for scale_shape() in ggplot2

Suppose I have the following y <- rnorm(10) b <- as.factor(sample(1:4,10,replace=T)) qplot(1:10, y, shape=b) How do I change the shapes that are used using ggplot2? ...

Adding summary statistics (or even raw data points) to dodged position boxplots

Say you have the following dataset: trt <- ifelse(runif(100)<0.5,"drug","placebo") inj.site <- ifelse(runif(100)<0.5,"ankle","wrist") relief <- 20 + 0.5*(inj.site=="ankle") + 0.5*(trt=="drug") + rnorm(100) to.analyze <- data.frame(trt,inj.site,relief) Now, the idea is to make a boxplot with injury site on the x-axis and boxes by trea...

Count by factor in ggplot2 chart

Given the following ggplot2 chart: ggplot(my_data, aes(colour=my_factor) + geom_point(aes(x=prior, y=current)) + facet_grid(gender ~ age) I would like to make the size of the points be proportional to the count of my_factor for that prior/current combination. ggplot(my_data, aes(colour=my_factor,...

Plotting predefined density functions using ggplot and R

I have three data sets of different lengths and I would like to plot density functions of all three on the same plot. This is straight forward with base graphics: n <- c(rnorm(10000), rnorm(10000)) a <- c(rnorm(10001), rnorm(10001, 0, 2)) p <- c(rnorm(10002), rnorm(10002, 2, .5)) plot(density(n)) lines(density(a)) lines(density(p)) W...

Showing multiple axis labels using ggplot2 with facet_wrap in R

I've got a nice facet_wrap density plot that I have created with ggplo2. I would like for each panel to have x and y axis labels instead of only having the y axis labels along the left side and the x labels along the bottom. What I have right now looks like this: library(ggplot2) myGroups <- sample(c("Mo", "Larry", "Curly"), 100, replac...

melt to two variable columns

I have the following variables in a data frame [1] "Type" "I.alt" "idx06" "idx07" "idx08" "farve1" "farve2" If I do dm <- melt(d, id=c("Type","I.alt")) i get theese variables "Type" "I.alt" "variable" "value" where "idx06", "idx07", "idx08", "farve1", "farve2" is represented in "variable". But what I really want is...

Multiple data points in one R ggplot2 plot

I have two sets of data points that both relate to the same primary axis, but who differ in secondary axis. Is there some way to plot them on top of each other in R using ggplot2? What I am looking for is basically something that looks like this: 4+ | | x . + 220 3+ . . | | x | 2+ . + 210 | ...

how to make a (ggplot) line plot with different color segments conditional on direction

As per Shanes excellent solution of another question, I now realise that I do not know how to do this. My original approach was to use melt the data (thanks again shane): dm1 <- melt(d[,c("Type","I.alt","idx06","idx07","idx08")], id=c("Type","I.alt")) dm2 <- melt(d[,c("Type","I.alt","farve1","farve2")], id=c("Type","I.alt")) colnames(d...

Placement of axis labels at minor breaks with ggplot2

I am using ggplot2 to do some plotting of genomic data, so the basic format is that there is a chromosome and a position along it. I convert the positions to be on a continuous scale, then put the breaks at the boundaries of the chromosomes with: scale_x_continuous("Genome Position", breaks = c(0, cumsum(chromosome_length))) That look...

Adding stat_smooth in to only 1 facet in ggplot2

I have some data for which, at one level of a factor, there is a significant correlation. At the other level, there is none. Plotting these side-by-side is simple. Adding a line to both of them with stat_smooth, also straightforward. However, I do not want the line or its fill displayed in one of the two facets. Is there a simple wa...

Add a vertical line with different intercept for each panel in ggplot2

I'm using ggplot2 to create panels of histograms, and I'd like to be able to add a vertical line at the mean of each group. But geom_vline() uses the same intercept for each panel (i.e. the global mean): require("ggplot2") # setup some sample data N <- 1000 cat1 <- sample(c("a","b","c"), N, replace=T) cat2 <- sample(c("x","y","z"), N, ...