Hello,
Sorry for not included any example data for my problem. I couldn’t find a way to easily produce an example shape file. Hopefully, experienced users of ggplot can see what I’d like to do from the description below.
I’ve got:
A data frame X with information about sample plots (plotid, var1, var2, var3, var4,…)
A polygon shapefile Y with spatial information for the sample plots
Importation of the shapefile Y (with maptools) and fortifying as data frame Z (ggplot2) works fine. Melting X to X_melted works equally fine. Merging Z and X_melted to mapdf works as well.
That means that now we have a dataframe in long form with spatial information and var1, var2, var3, … Now I want to plot this data frame like this:
pl1 <- ggplot(mapdf,aes(long,lat),group=group)
pl1 <- pl1 + geom_polygon(aes(group=group,fill=value),colour="black")
pl1 <- pl1 + facet_grid(variable ~ .)
pl1 <- pl1 + coord_equal(ratio = 1)
pl1
The result is a nice plot with one panel for each variable. The maps of the panels are identical, but fill colour varies with the values of the variables. Up to now, everything works like a charm… with one problem:
The variables have different min and max values. For example var1 goes from 0 to 5, var2 from 0 to 400, var3 from 5 to 10, etc. In that example, the legend for the fill colour goes from 0 to 400. Var2 is nicely drawn, but var1 and var3 are basically in the same colour.
Is there a way I could use a different legend for each panel of the facet? Or is this simply not (yet) possible with facet_wrap or facet_grid in ggplot?
I could make individual plots for each variable and join them with viewports, but there a plenty of variables and this would be a lot of work.
Or is there maybe another package or method I could use to accomplish what I’d like to do?
And help would be very much appreciated. :)
Edit: With the help of the ggplot2-package description, I constructed an example that illustrates my problem:
ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
values <- data.frame(
id = ids,
val1 = cumsum(runif(6, max = 0.5)),
val2 = cumsum(runif(6, max = 50))
)
positions <- data.frame(
id = rep(ids, each = 4),
x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
)
values <- melt(values)
datapoly <- merge(values, positions, by=c("id"))
p <- ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=value, group=id),colour="black")
p <- p + facet_wrap(~ variable)
p
The panel on the right illustrates different values for var2 on the map. On the panel on the left however, all polygons have the same colour. This is logical, because only one colour gradient is used for all panels. Could I use a different colour gradient for each panel?