ggplot2

Add subgroup labels to a jitter plot in ggplot2

I have a nearly-boxplot like jitter-plot: dt <- rbind(se,cb,cb.se) qplot(ds, size, data=dt, geom="jitter", colour=root, facets = test ~ .) I'd love to put a summary label for each group in the middle of the plot - for example the size totals here: aggregate(list(size=dt$size), list(dt$ds, dt$test), sum) Group.1 Group.2 size...

ggplot2: mean in the function on time

If I have data like this DF <- data.frame( date = seq(Sys.Date()-1000, len=1000, by="1 day")[sample(1000, 500)], price = runif(500) ) How do I plot e.g. mean of price in the function of time, e.g. in months, using ggplot2? ...

How to use an image as a point in ggplot?

Is there some way to use a specific small image as a point in a scatterplot with ggplot2. Ideally I will want to resize the images based on an variable. Here's an example: library(ggplot2) p <- ggplot(mtcars, aes(wt, mpg)) p + geom_point(aes(size = qsec, shape = factor(cyl))) So I basically want to know if there is a way to supply a...

Information Dashboards in R with ggplot2

I'm looking to create a static dashboard viewable in a web browser. And I'd like to create something like what Stephen Few does in his book Information Dashboard Design. (see example at bottom) Ggplot2: Shouldn't be any issue producing the graphs below, right? Dashboard Layout: Is grid suitable? Or should I lay things out in html/css? ...

Using ggplot, how to have the x-axis of time series plots set up automatically?

Is there a way of plotting a univariate time series of class "ts" using ggplot that sets up the time axis automatically? I want something similar to plot.ts() of base graphics. Also it seems to me that the coarsest time granularity is a day. Is that right? In my work I have to work with monthly and quarterly data and assigning each obse...

How can I plot multiple functions in R?

Using ggplot, is there a way of graphing several functions on the same plot? I want to use parameters from a text file as arguments for my functions and overlay these on the same plot. I understand this but I do not know how to add the visualized function together if I loop through. ...

Stacked Area Histogram in R

I ran a Pig job on a Hadoop cluster that crunched a bunch of data down into something R can handle to do a cohort analysis. I have the following script, and as of the second to last line I have the data in the format: > names(data) [1] "VisitWeek" "ThingAge" "MyMetric" VisitWeek is a Date. ThingAge and MyMetric are integers. The...

Is there a way to remove the border of the legend in ggplot2?

I'm using qplot to plot a function and I want to position the legend within the plot. I've used opts( legend.position = c(0.7,0.7) ) to move the legend where I want it to be. However there is a white border around the legend and that shows up on the gray background. For example: library(ggplot2) x = c(1:20) y = c(1:20) p <- qplo...

Using ggplot2 how can I represent a dot and a line in the legend

Using ggplot2 I am plotting several functions and a series of points. I cannot figure out how to represent the points on the legend. I realize I need to use an aes() function, but I don't fully understand how to do this. I apologize that the example is so long, but I don't know how else to illustrate it. ## add ggplot2 library(ggplot2) ...

List of ggplot2 options?

After some research I found the way to prevent an uninformative legend from displaying ... + opts(legend.position = "none") Where can I find all the available "opts" for ggplot2? ...

How to get geom_vline and facet_wrap from ggplot2 to work inside a function

I'm using ggplot2 to explore the effects of different military operations on murder rates. To show the effect I draw a vertical line when the operation occurred and a smoothed line of the murder rate before and after the operation. I've written a facet_wrap plot to show this for a whole bunch of counties. It works beautifully, but when...

bar width in ggplot2 geom_bar

I am trying to produce plots with a loop. l1<-factor(rep(letters,4)) n1<-abs(rnorm(104))*10000 b1<-rep(c("1","2","3","4","5","6","7","8"),c(2,2,11,24,11,20,33,1)) k1<-rep((rep(c("A","B","C","D"),c(2,3,4,4))),8) my.df<-data.frame(l1,b1,k1,n1) #make a dataframe names(my.df)<-c("letter","branch","ltrtype","numbe...

How can I add another layer / new series to a ggplot?

In ggplot I can add a series to a plot with: ggplot(diamonds, aes(x = carat, y = price)) + geom_point() How do I simply add another series, e.g. plotting the cost of rubies against diamonds. Assuming rubies was also in the diamonds dataset. I have tried to lay over the top another layer with the rubies data, but it just plots the ru...

R & ggplot2: Sparklines from dynamic variables (based on query results)

I've written an SQL query that tells me the names of the previous week's top 10 most frequent Alarms. And I've written a query that takes those top 10 alarms and provides a YTD weekly totals for each of those alarms. Now I'm looking to create a panel of sparklines showing the YTD trend for each of the week's top 10 alarms. I got somet...

Why do I get an error when I run some examples from the online ggplot2 reference manual?

Trying the ggplot2 examples in the online reference manual, and particularly in this page, I fail to produce all but the first of the second example's plots. > d + stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) Error in `[<-.data.frame`(`*tmp*`, var, value = list(`NA` = NULL)) : missing values are not allowed ...

How do I produce a boxplot in ggplot using a matrix

In R it is easy to turn a matrix into a boxplot > myMatrix [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 27 32 31 28 20 28 10 29 15 29 [2,] 31 33 20 28 21 9 14 21 34 33 [3,] 27 33 28 23 26 33 19 11 26 30 [4,] 33 17 10 31 10 32 10 29 31 ...

How to add custom series labels to a legend in R's ggplot?

I have a plot (sample code pasted below) that I am trying to add by own labels for the series information. Instead of plotting "p1s1" "p1s2" "p3s4", I would like "treatment 1" "treatment 2" "treatment 3". I have used levels(series_id) to get the unique series names and used a lookup table to get the descriptions. (I think this gets th...

How to add a title to a ggplot when the title is a variable name?

At the end of a ggplot, this works fine: + opts(title = expression("Chart chart_title...")) But this does not: chart_title = "foo" + opts(title = expression(chart_title)) nor this: chart_title = "foo" + opts(title = chart_title) How can I add a title to a ggplot when the title is a variable name? ...

How to add a condition to the geom_point size?

I am trying to add a condition to geom_point size and I've pasted my example below. I would like geom_point size to be 2 when n_in_stat is 4 or more, and size = 5 when n_in_stat is less than 4. I tried putting an ifelse statement inside the geom_point, but this failed. Perhaps I can't include logical operators here and I have to crea...

ggplot2 geom_area overlapping instead of stacking

I'm trying to generate a stacked area plot, but instead, ggplot makes overlapping areas. I've tried other examples that seems analogous to me, but they work and mine doesn't. > cx date type visitors 1 2009-11-23 A 2 2 2010-01-07 A 4 3 2010-01-09 A 6 4 2010-02-07 A 8 5 2009-12-02 B...