ggplot2

How to barplot frequencies with ggplot2?

Dear all, probably I just need a break :). I have a melted dataset containing a column "value" which represent an absolute number which varies with every row of the dataset. I want to display this number in a barchart by country. p <- ggplot(melted,aes(factor(country),y=as.numeric(value))) + geom_bar() +opts(axis.text.x = theme_text(...

ggplot2: overlay control group line on graph panel set

I have a stacked areaplot made with ggplot2: dists.med.areaplot<-qplot(starttime,value,fill=dists,facets=~groupname, geom='area',data=MDist.median, stat='identity') + labs(y='median distances', x='time(s)', fill='Distance Types')+ opts(title=subt) + scale_fill_brewer(type='seq') + facet_wrap(~groupname, ncol=2) + g...

scatterplot with equal axes

Hi, I have a data set like this one below: DataFrame <- data.frame(x=runif(25),y=runif(25), z=sample(letters[1:4],25,rep=TRUE)) and using the Lattice package, I can make a scatter plot with equal axes (with a 1:1 line going through the centre) with the following lines: xyplot(y ~ x | z, data=DataFrame, ...

Cashflow diagram in R?

A cashflow diagram is often used when explaining derivatives in financial engineering. It shows the payoffs at different times. I couldn't find a great example online, but it looks something like this: I would like to make something roughly equivalent using ggplot2. My thought was to use a stacked bar plot, where the zero axis is s...

how to set x-axis limits in ggplot2 R plots?

Say I plot the following in R: library(ggplot2) carrots <- data.frame(length = rnorm(500000, 10000, 10000)) cukes <- data.frame(length = rnorm(50000, 10000, 20000)) carrots$veg <- 'carrot' cukes$veg <- 'cuke' vegLengths <- rbind(carrots, cukes) ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha = 0.2) Now say I only ...

How to make ggplot2 plots prettier?

I have generated the following plot using the R code that follows it: ggplot(lengths, aes(length, fill = library)) + geom_density(alpha = 0.2) + coord_cartesian(xlim = c(0, 60000)) Now I would like to make the plot a bit prettier: Make the x-axis show length every 5000 units (instead of every 20000) Add x-values on top of the thre...

Highlighting regions of interest in ggplot2

In vanilla plotting, it is possible to use a polygon call in the panel.first argument to plot to highlight a background region. Is it possible to do the same in ggplot2? Can it be done while preserving the gridlines? eg: # plot hp and wt for mtcars data, highlighting region where hp/wt ratio < 35 with(mtcars,plot(hp,wt, panel.firs...

Stacked bar chart in R (ggplot2) with y axis and bars as percentage of counts

Hi, I'm a novice with ggplot2 and have a question about generating a stacked bar plot. I checked the book and the dedicated webpage, but can't solve the problem. I have two factors, one of which has 2 levels (presence-absence), the other 10 levels. Lets call these two "variable" and "fruit". I'd like to create a stacked bar plot where ...

ggplot2: line connecting the means of grouped data

I'm trying to group one variable of my data by another and then plot a line through the means. It works fine when both variables are numbers. However, I'm having a problem when the grouping variable is a factor. I have a mock up below. x <- sample(1:3, 40, replace=T) y <- rnorm(40) df1 <- data.frame(x, y) qplot(x, y, data=df1) + stat_...

ordered factors in ggplot2 bar chart

I have a data frame with (to simplify) judges, movies, and ratings (ratings are on a 1 star to 5 star scale): d = data.frame(judge=c("alice","bob","alice"), movie=c("toy story", "inception", "inception"), rating=c(1,3,5)) I want to create a bar chart where the x-axis is the number of stars and the height of each bar is the number of r...

[R] ggplot: specify aes by index

ggplot() + layer( data = diamonds, mapping = aes(x = carat, y = price), geom = "point", stat = "identity" ) In the above example, I am wondering if I can specify the parameters for the "aes" function by indexes. I know that carat and price correspond to the 1st and 8th elements in the names array of diamond. Can you advice why the ...

How to create side-by-side bar charts (for multiple series) with ggplot?

I have two sets of data (3 columns: x=categorical, y = numerical, l = location) and I would like to create a bar chart with the categories on the x axis and, for each value of the category, two vertical bars, coloured differently, with the y values for each location. By the default, Excel/OpenOffice produce this kind of chart. I tried ...

ggplot: recommended colour palettes also distinguishable for B&W printing?

I've started to produce the charts for a paper. For some of them which are bar charts I've used the "Pastel1" palette (as recommended in the book on ggplot2, pastel colours are better than saturated ones for fill areas, such as bars). The problem with Pastel1 at least is that when printed on a B&W laser printer, the colours are indistin...

ggplot: How to increase spacing between faceted plots?

I have several faceted histograms (obtained with the command below) which are nicely plotted one under the other. I would like to increase the spacing between them, however, they are tight. I looked at the doc but didn't find a parameter for this. Thanks in advance. qplot (Happiness.Level, Number.of.Answers, data=mydata, geom="histog...

ggplot: How to override the ylim interval?

I have a faceted plot (about which I had this other question). I would like to control the interval of ylim() to reduce the clutter because it looks like this: It's too detailed and I would like to display only 0 and 500, that is not even the maximum (the thin horizontal lines are enough). The reasons I want only those 2 values are: ...

[R, ggplot2] ggplot2, facet_grid, free scales?

In the following example, how do I get the y-axis limits to scale according to the data in each panel? mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() Neither of these will do it: mt + facet_grid(. ~ cyl, scales="free") mt + facet_grid(. ~ cyl, scales="free_y") ...

[r] ggplot how to control when geom_line connects points when data points missing

Hello - For ggplot line charts I would like to control when geom_line connects between two points, and when not (due to missing observations). In other words, I would like to be able to tell geom_line to still connect when e.g. one data point is missing, but not when more than one data point is missing. Or to tell it not to connect when ...

How to draw only a range of values in geom_point from the ggplot2 package?

Hello All, I have the following molten data: X variable value 1 StationA SAR11.cluster 0.001309292 2 StationB SAR11.cluster 0.002712237 3 StationC SAR11.cluster 0.002362708 4 StationD SAR11.cluster 0.002516751 5 StationE SAR11.cluster 0.004301075 6 StationF SAR11.cluster 0.0 . . . etc. etc. I used the following code t...

ggplot: showing % instead of counts in charts of categorical variables

I'm plotting a categorical variable and instead of showing the counts for each category value, I'm looking for a way to get ggplot to display the percentage of values in that category. Of course, it is possible to create another variable with the calculated percentage and plot that one, but I have to do it several dozens of times and I ...

Compute/plot statistics on a 2d grid

Suppose I have an R data frame with columns that specify location (lat/long), height, and gender of individuals: x <- data.frame( lat=c(39.5,39.51,38,38.1,38.2), long=c(86,86,87,87,87), gender=c("M","F","F","M","F"), height=c(72,60,61,70,80) ) I want to bin the data in two dimensions (e.g. into 1000m x 1000m squares) and compu...