1

I have a data table, data_dt. I would like to plot contours of densities in 2 dimension. I cannot make the filling colors to be different.

Any suggestions is appreciated.

data looks like

data_dt
DD    precip    model 
3148   302    observed
2904   262    observed
3345   322    observed
2937   420    observed
3659   140    BNU 
2661   208    BNU 
3314   364    BNU 
3088   222    BNU 

Which can be created by

data_dtt = data.table(DD   = c(3148, 2904, 3345, 3088, 3314, 2937, 2661, 3659),
                      precip = c(302, 262, 322, 420, 310, 292, 372, 440),
                      model = c("observed", "observed", "observed", "observed", "BNU", "BNU", "BNU", "BNU"))

I have tried the following code:

ggplot(data_dtt, aes(x = DD, y = precip)) + 
ylab(y_lab) + xlab(x_lab) + 
stat_density_2d(aes(fill = stat(level), colour = model), 
                alpha = .4, contour = TRUE, geom = "polygon") + 
scale_fill_viridis_c(guide = FALSE, aesthetics = "fill") 

The output I get looks like:

enter image description here

You can notice the level sets are in different colors, red and blue-ish, but the filling color, shade, are not.

How can I make them different?

Perhaps the following image, from a full data set is a better one.

You can see the borders of observed and BNU are in differnet colors, but the filling color inside each closed curves are identical.

enter image description here

You see, the curve at the center of both BNU and observe is green, as they move away from their mean, they both go to kind of purple. I want BNU be red at center and as it goes out, the intensity decrease, and I want the center of observed be blue.

Or, in the following image, you can see the filling colors for different sexes are different, I want this, but in 2D contours. In my plots, only the color of borders, or curves, or level sets are different: enter image description here

OverFlow Police
  • 861
  • 6
  • 23
  • I didn't understand your question, you want to change fill or color ? – pogibas Jun 08 '19 at 08:21
  • yes, the color of filling, you see, at the center where the intensity is high, it is green, and as it goes out, it fades away, but for both models, "observed" and "BNU", the filling color is the same. I want to make it different – OverFlow Police Jun 08 '19 at 08:22
  • So you want to change a legend? – pogibas Jun 08 '19 at 08:24
  • not the legend! if the filling color was different, then the legend would automatically be different. I want to change the filling color. I want the color of BNU be blue, the observed one, to be red! I put the arrows towards the center of legend to show the color of fillings for both models is the same! – OverFlow Police Jun 08 '19 at 08:25
  • 4
    Related: [Using different scales as fill based on factor](https://stackoverflow.com/questions/20474465/using-different-scales-as-fill-based-on-factor). Main message: one scale per aesthetic. I suspect the grob digging in my answer needs to be adjusted to more recent versions of ggplot. Also, I think there are now some convenience functions which may make the code a bit cleaner. – Henrik Jun 08 '19 at 08:38
  • FYI: Just checked the code in my answer above: it works with `ggplot2_3.1.1`. – Henrik Jun 08 '19 at 09:25

0 Answers0