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

Densityplot with ggplot

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) 

Levelplot with Latice

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() 

Plot wrong with ggplot

I don't wanna make a density plot. I just wanna make a plot that look like the density plot in figure 1.

2

1 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.

default ggplot geom_raster

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)) ) ) 

level plot example with discrete steps

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.