1

I'm currently working on a very simple data.frame, containing three columns:

x contains x-coordinates of a set of points,

y contains y-coordinates of the set of points, and

weight contains a value associated to each point;

Now, working in ggplot2 I seem to be able to plot contour levels for these data, but i can't manage to find a way to fill the plot according to the variable weight. Here's the code that I used:

ggplot(df, aes(x,y, fill=weight)) + 
  geom_density_2d() +
  coord_fixed(ratio = 1) 

You can see that there's no filling whatsoever, sadly. I've been trying for three days now, and I'm starting to get depressed.

Specifying fill=weight and/or color = weight in the general ggplot call, resulted in nothing. I've tried to use different geoms (tile, raster, polygon...), still nothing. Tried to specify the aes directly into the geom layer, also didn't work. Tried to convert the object as a ppp but ggplot can't handle them, and also using base-R plotting didn't work. I have honestly no idea of what's wrong! I'm attaching the first 10 points' data, which is spaced on an irregular grid:

x = c(-0.13397460,-0.31698730,-0.13397460,0.13397460,-0.28867513,-0.13397460,-0.31698730,-0.13397460,-0.28867513,-0.26794919)

y = c(-0.5000000,-0.6830127,-0.5000000,-0.2320508,-0.6547005,-0.5000000,-0.6830127,-0.5000000,-0.6547005,0.0000000)

weight = c(4.799250e-01,5.500250e-01,4.799250e-01,-2.130287e+12,5.798250e-01,4.799250e-01,5.500250e-01,4.799250e-01,5.798250e-01,6.618956e-01)

any advise? The desired output would be something along these lines:

click

Thank you in advance.

DeveauP
  • 1,217
  • 11
  • 21
superzucca
  • 103
  • 2
  • 6
  • 1
    It sounds like you are misusing a density plot. Density plots are meant to estimate underlying probability density functions (a function that integrates to 1 over the sample space). Maybe it sounds like you want some form of interpolation to spread function values out over a surface? But in that case you would need to choose some form of statistical modeling for the interpolation. – MrFlick May 05 '16 at 14:05
  • Have you tried looking at the `filled.contour` function? You would first need to interpolate your data with something like `library(akima)` `int.ds <- interp(df$x,df$y,df$weight,duplicate="strip")`. Then you could do something along the lines of `filled.contour(int.ds)` – Mike H. May 05 '16 at 18:40

2 Answers2

2

From your description geom_density doesn't sound right.

You could try geom_raster:

ggplot(df, aes(x,y, fill = weight)) + 
     geom_raster() +
     coord_fixed(ratio = 1)  + 
     scale_fill_gradientn(colours = rev(rainbow(7)) # colourmap
Scott Warchal
  • 1,028
  • 10
  • 15
0

Here is a second-best using fill=..level... There is a good explanation on ..level.. here.

# load libraries
  library(ggplot2)
  library(RColorBrewer)
  library(ggthemes)

# build your data.frame
 df <- data.frame(x=x, y=y, weight=weight)

# build color Palette
  myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")), space="Lab")


# Plot
  ggplot(df, aes(x,y, fill=..level..) ) + 
      stat_density_2d( bins=11, geom = "polygon") +
      scale_fill_gradientn(colours = myPalette(11)) +
      theme_minimal() +
      coord_fixed(ratio = 1)

enter image description here

Community
  • 1
  • 1
rafa.pereira
  • 13,251
  • 6
  • 71
  • 109
  • 1
    This doesn't use weights. – Axeman May 07 '16 at 06:40
  • Hi @Axeman, you're correct. My answer does not use `fill=weights`, that's why I said it's a 'second best'. As far as I'm aware, it's not possible to pass a data vector to `fill` in `stat_density_2d`. A bit of explanation in the [Link](http://stackoverflow.com/questions/32206623/what-does-level-mean-in-ggplotstat-density2d/32207207?noredirect=1#comment57518193_32207207) I posted with my answer. – rafa.pereira May 07 '16 at 09:43