I like the look that the density plot of the ggplot view (Below). I wanna know how make a similar graph using a set of data interpoled (Data: ). In other words, i wanna a graph with discrete contour and shaded colors.
I will appreciate your help
For example:
The same plot is possible make with lattice package:
library(ggplot2) library(lattice) library(latticeExtra) data=read.table("data.txt",header = T,sep = ";") #discrete field levelplot(var~long+lat,data = data,contour=T) But, in ggplot i get this result when i apply the density plot:
ggplot(data,aes(x=long,y=lat))+geom_density2d(aes(fill=var))+theme_bw() I don't wanna make a density plot. I just wanna make a plot that look like the density plot in figure 1.
21 Answer
With ggplot I believe you require something like ggplot(dat, aes(long, lat, fill = var)) + geom_raster() and not the geom_density2d function. The default fill function is continuous as shown below. Others can point you to the functions to use discrete colors.
For lattice, you have to provide the colors and the breakpoints to the levelplot function and to the colorkey argument list and to the labels argument list within the colorkey argument. It's not so obvious...but here is an example.
# dat <- read.delim(f, sep = ";") myCol <- brewer.pal(11, "RdYlBu")[c(6,8:11)] breaks <- seq(min(dat$var), max(dat$var), len = 6) levelplot(var ~ long + lat, data = dat, at = breaks, col.regions = myCol, colorkey = list(col = myCol, at = breaks, labels = list(at = breaks, labels = round(breaks)) ) )




