ggplot2

Per panel smoothing in ggplot2

I'm plotting a group of curves, using facet in ggplot2. I'd like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I'd like to stop the plot failing when one of the panels only has 1 or 2 points. Example: a <- data.frame( x=1:100, y=sin(seq(0.1,10,0.1) )) ...

ggplot2: using the geom_area() function

I have a data frame showing four classes for each year, along with their respective shares of the total for that year. > head(df) class year share 1 class1 1975 0.806 2 class2 1975 0.131 3 class3 1975 0.018 4 class4 1975 0.045 5 class1 1976 0.788 6 class2 1976 0.151 When I run ggplot with no fill specified, I ...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I'm plotting it in a bar plot with ggplot2 using the code below. I'd like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "Singular" come before the bars labeled "Plural". ggplot(df.all, aes(gram, V1, fill=number)) + geom_bar(stat="identity", position="dodg...

Creating a Pareto Chart with ggplot2 and R

I have been struggling with how to make a Pareto Chart in R using the ggplot2 package. In many cases when making a bar chart or histogram we want items sorted by the X axis. In a Pareto Chart we want the items ordered descending by the value in the Y axis. Is there a way to get ggplot to plot items ordered by the value in the Y axis? I t...

Adding a line to ggplot

Hi all, I am trying to add a line to a plot of points, and I can't figure it out. My y values are numbers from 0 to Inf, while my x values are from an ordered factor. Here is the plotting code, which only displays points: g = ggplot() + layer(data = ratesdf, mapping = aes(x = age, y = rates), geom = "point", stat="identity") + la...

[ggplot2] problem with legend while plotting data from two data.frame

Hi, I'm having a little trouble with getting ggplot2 to work as I want. Basically, I'd like to compare actual observations vs. approximated by putting them in one single plot. For example, > library(ggplot2) > df.actual <- data.frame(x = 1:100, y = (1:100) * 2) > df.approx <- data.frame(x = 1:150, y = (1:150) * 2 + 5 + rnorm(150, mea...

ggplot2 Scatter Plot Labels

I'm trying to use ggplot2 to create and label a scatterplot. The variables that I am plotting are both scaled such that the horizontal and the vertical axis are plotted in units of standard deviation (1,2,3,4,...ect from the mean). What I would like to be able to do is label ONLY those elements that are beyond a certain limit of standard...

Problem storing plot objects in a list in R

I asked this question yesterday about storing a plot within an object. I tried implementing the first approach (aware that I did not specify that I was using qplot() in my original question) and noticed that it did not work as expected. library(ggplot2) # add ggplot2 string = "C:/example.pdf" # Setup pdf pdf(string,h...

Vertical gridlines in ggplot with discrete x-axis

I have the following setup: emp <- structure(list(s = structure(c(1L, 2L, 2L, 2L, 7L, 7L, 3L, 4L, 4L, 4L, 4L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 5L, 5L, 6L), .Label = c("8", "24", "31", "78", "135", "142", "30", "98", "117", "123"), class = "factor", scores = structure(c(1, 2, 14, 3, 5, 17, 18, 20, 11, 13), .Dim = 10L, .Dimnames = list(c(...

Plotting functions in R

Is there a way of overlaying a mathematical function on top of data using ggplot? ## add ggplot2 library(ggplot2) # function eq = function(x){x*x} # Data x = (1:50) y = eq(x) # Make plot object p = qplot( x, y, xlab = "X-axis", ylab =...

qplot and anti-aliasing in R

I am using ggplot2 library and am working with the qplot command I know I can save my output as an anti-aliased image file by using the following command after my qplot ggsave(file="filename.png") But how about my LCD display? is there any way to see a plot on the monitor as anti-aliased grpah? ...

Repositioning scatter plot labels in ggplot2

Is it possible to reposition the labels such that in sector (-x,y) the label is on the left and sector (+x,y) the label is on the right? ...

Create Editable plots from R

Hello, I'm creating a series of plots in R (I'm using ggplot2, but that's not essential) and I want to be able to save my output so I can then edit it for furthur use, For instance, I might want to move legends about, or adjust colours etc. I have seen that ggplot2 has a save command but that seems to produce pdf's or bitmaps, neither ...

Creating a facet_wrap plot with ggplot2 with different annotations in each plot

I am using ggplot2 to explore the result of some testing on an agent-based model. The model can end in one of three rounds per realization, and as such I am interested in how player utilities differ in terms of what round the game ends and their relative position in 2D space. All this is to say that I have generated a facet_wrap plot t...

Do I always have to use data frames in ggplot2

Dear All, I'm running a monte-carlo simulation and the output is in the form: > d = data.frame(iter=seq(1, 2), k1 = c(0.2, 0.6), k2=c(0.3, 0.4)) > d iter k1 k2 1 0.2 0.3 2 0.6 0.4 The plots I want to generate are: plot(d$iter, d$k1) plot(density(d$k1)) I know how to do equivalent plots using ggplot2, convert to data ...

Most underused data visualization

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 you think is the most underused type of plot? Answers should: Not be very commonly used in practice. Be understandable without a great de...

R ggplot2 question - working with factors

I've got a dataset that looks like this... mine tonnes week AA 112 41 AA 114 41 AA 119 41 BB 108 41 BB 112 41 AA 110 42 AA 109 42 AA 102 43 AA 101 43 And I want to create a boxplot in ggplot2 to show the distribution of tonnes for each week. But I only want results from mine AA. I thoug...

Using ggplot2 and rpanel together

Has anybody used ggplot2 along with rpanel to produce interactive plots. Here is a piece of code that I adapted from rpanel to plot a Poisson distribution and have a slider to change the parameter value. However, the plot changes too slowly as I change the parameters using the slider. When I change the plot function to use Lattice, it i...

R ggplot2 - no background or grid lines in plot with RGui

I'm having a strange problem with the output window in RGui (under Win XP). I should see a plot like the one below... ... when I run this script: library(ggplot2) x <- rnorm(100,0,1) y <- rnorm(100,0,1) z <- data.frame(x,y) g <- ggplot(z, aes(x,y)) + geom_point() + theme_gray() Instead, in the plot window it shows a white backgrou...

R ggplot2 facet_grid question

I have a ggplot2 plot that looks like this: from the following R code: ggplot(newdata, aes(benefit, cost, colour = factor(opt), shape = factor(roster))) + facet_grid(. ~ location) It's exactly what I need, except that the graph is too wide to be clearly read. I'd like to be able to take the four rightmost locations and place the...