ggplot2

How to manually add a legend to a ggplot object

I have this data frame: structure(list(month_num = 1:24, founded_month = c(4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L), founded_year = c(2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L,...

R: How to remove outliers from a smoother in ggplot2?

I have the following data set that I am trying to plot with ggplot2, it is a time series of three experiments A1, B1 and C1 and each experiment had three replicates. I am trying to add a stat which detects and removes outliers before returning a smoother (mean and variance?). I have written my own outlier function (not shown) but I e...

Error when trying to create a faceted plot in ggplot2

I am trying to make a faceted plot in ggplot2 of the coefficients on the regressors from two linear models with the same predictors. The data frame I constructed is this: r.together> reg coef se y 1 (Intercept) 5.068608671 0.6990873 Labels 2 goodTRUE 0.310575129 0.5228815 Labels 3 indiaTRUE -1....

R: ggplot2, why does my legend show faded colors?

Why is my legend faded in these examples below? Notice how the colours in the legend are not as vivid as the colours in the plot: library(ggplot2) r <- ggplot(data = diamonds, aes(x = carat, y = price, color = cut, group = cut)) r + geom_smooth() #(left) r + geom_smooth(size = 2) #(right) EDIT: added a close-up ...

R: ggplot2, how to add a number of layers to a plot at once to reduce code

library(ggplot2) This code produces a nice looking plot: qplot(cty, hwy, data = mpg, colour = displ) + scale_y_log2() + labs(x="x axis") + labs(y="y axis") + opts(title = "my title") But I want to setup variables to try and to reduce code repetition: log_scale <- scale_y_log2() xscale <- labs(x="x axis") yscale <- labs(y="y a...

Changing ylim (axis limits) drops data falling outside range. How can this be prevented?

df <- data.frame(age=c(10,10,20,20,25,25,25),veg=c(0,1,0,1,1,0,1)) g=ggplot(data=df,aes(x=age,y=veg)) g=g+stat_summary(fun.y=mean,geom="point") Points reflect mean of veg at each age, which is what I expected and want to preserve after changing axis limits with the command below. g=g+ylim(0.2,1) Changing axis limits with the above c...

R: ggplot2, can I set the plot title to wrap around and shrink the text to fit the plot?

library(ggplot2) my_title = "This is a really long title of a plot that I want to nicely wrap \n and fit onto the plot without having to manually add the backslash n, but at the moment it does not" r <- ggplot(data = cars, aes(x = speed, y = dist)) r + geom_smooth() + #(left) opts(title = my_title) can I set the plot title to wrap a...

Creating a facet_wrap plot with ggplot2 with less annotations than plots

I am using ggplot2 to plot a figure that contains nine facets. Each facet represents the relationship between two variables and I would like to annotate the facets that display statistically significant results with a star ''. This would result in only two of the nine facets with a ''. However, I end up with all nine facets displaying th...

R: ca plotting text attributes

Does anyone know of a way to control the font size/color/weight of the row and column names when plotting a correspondence plot with the ca package? The following code will produce a very nice looking chart, though if there were more attributes (very heavy, super heavy, something more than super heavy) or more classes of workers (peons,...

How safe am I signing into Google Spreadsheets with yeroon.net/ggplot2

I am impressed by what I have seen of yeroon.net/ggplot2 which is a web interface for Hadley Wickham's R package ggplot2. I want to try it out on my own data. The part that has me very excited is that one can use data stored in one's own Google spreadsheet as the data. One just signs into their Google Account so that yeroon.net/ggplot2 c...

R: How to update a package and keep it from reverting to the original?

I want to upgrade the package ggplot2: library(ggplot2) packageDescription("ggplot2")["Version"] > 0.8.3 But the current version is 0.8.7. I tried update.packages(), which seemed to work OK. But it still returned older version 0.8.3. So I downloaded and installed the package source from Cran, which says 0.8.7 in the download page. ...

Can one extract model fit parameters after a ggplot stat_smooth call?

Using stat_smooth, I can fit models to data. E.g. g=ggplot(tips,aes(x=tip,y=as.numeric(unclass(factor(tips$sex))-1))) +facet_grid(time~.) g=g+ stat_summary(fun.y=mean,geom="point") g=g+ stat_smooth(method="glm", family="binomial") I would like to know the coefficients of the glm binomial fits. I could re-do the fit with dlply and ge...

How can I suppress the vertical gridlines in a ggplot2 plot?

I am building a bar chart for which bars suffice as indications of horizontal (x) placement, so I'd like to avoid drawing the superfluous vertical gridlines. I understand how to style the minor and major gridlines in opts(), but I can't for the life of me figure out how to suppress just the vertical gridlines. library(ggplot2) data <-...

How can I suppress the vertical gridlines in a ggplot2 plot while retaining the x-axis labels?

This is a follow-on from this question, in which I was trying to suppress the vertical gridlines. The solution, as provided by learnr, was to add scale_x_continuous(breaks = NA), but this had the side effect of also suppressing the x-axis labels, as well. I am totally happy to write the labels back in by hand, but it's not clear to me ...

Trying to keep filled bars in a faceted plot

Not sure what I'm doing wrong here. I have this plot: ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge") which produces: Then I want to facet by a factor, creating two stacked plots w/ dodged, colored bars ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge") + facet_grid(~gr...

ggplot geom_bar - bars too wide

I am sorry for the non-informative title. > y=read.csv(textConnection(scan("",sep="\n",what="raw"))) "","org","art","type","length" "191","gk","Finish","short",4 "147","ik","Attending","short",7 "175","gl","Finish","long",11 "192","il","Attending","long",95 "144","gm","Finish","between",5 "161","im","Attending","between",15 "164","tu",...

Can ggplot2 work with R's canvas backend

Having installed canvas from here http://www.rforge.net/canvas/files/ I try to plot: > canvas('test.js') > qplot(rnorm(100), geom='histogram') stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this. Error in grid.Call.graphics("L_setviewport", pvp, TRUE) : Non-finite location and/or size for viewport > ...

R: ggplot2, how to get the parameters from a plotted linear model smoother?

I have a data.frame with 3 time series in it, shown below. When I plot them with a smoother time series, I want to be able to get the parameters of the linear model that I plot, but I can't see how to do that? > data day od series_id 1 1 0.10 A1 2 3 1.00 A1 3 5 0.50 A1 4 7 0.7...

How can I manipulate the strip text of facet plots in ggplot2?

I'm wondering how I can manipulate the size of strip text in facetted plots. My question is similar to a question on plot titles, but I'm specifically concerned with manipulating not the plot title but the text that appears in facet titles (strip_h). As an example, consider the mpg dataset. library(ggplot2) qplot(hwy, cty, dat...

What is the simplest method to fill the area under a geom_freqpoly line?

The x-axis is time broken up into time intervals. There is an interval column in the data frame that specifies the time for each row. The column is a factor, where each interval is a different factor level. Plotting a histogram or line using geom_histogram and geom_freqpoly works great, but I'd like to have a line, like that provided ...