86121

In ggplot, how can I get two legends (“gradient” type) for stat_bin2d?

Question:

Here I have two 'clusters', and just one legend.

How can I get two "density" legends with two different color gradients?

I have tried group but it does not work.

<a href="https://i.stack.imgur.com/OT3xR.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/OT3xR.png" data-original="https://i.stack.imgur.com/OT3xR.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

The following code generated the above graph:

library(ggplot2) df <- data.frame(x=c(rnorm(1000,1,.1),rnorm(1000,3,.1)), y=c(rnorm(1000,1,1),rnorm(1000,3,1)), type=c(rep('a',1000),rep('b',1000))) plot( ggplot(df) + stat_bin2d(aes(x,y,fill=..density..,group='type')))

Answer1:

I'm not aware of a way to specify more than one fill gradient. But here's a work around that uses different transparency levels to simulate the gradient, leaving fill available to be mapped with type:

ggplot(df, aes(x, y, fill = type)) + stat_bin2d(aes(alpha = ..density..)) + scale_alpha(range = c(1, 0.1)) + theme_bw()

<a href="https://i.stack.imgur.com/0UBfW.png" rel="nofollow"><img alt="different colours" class="b-lazy" data-src="https://i.stack.imgur.com/0UBfW.png" data-original="https://i.stack.imgur.com/0UBfW.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer2:

Using alpha = ..density.. does the trick:

ggplot(df, aes(x = x, y = y) ) + stat_bin2d(mapping= aes(alpha = ..density.., fill = type))

<a href="https://i.stack.imgur.com/G7wQv.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/G7wQv.jpg" data-original="https://i.stack.imgur.com/G7wQv.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

A bit more aesthetically using stat_density2d e.g.:

ggplot(df, aes(x=x, y=y) ) + stat_density2d(mapping= aes(alpha = ..level.., color= type), geom="contour", bins=6, size= 2)

<a href="https://i.stack.imgur.com/uTERq.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/uTERq.jpg" data-original="https://i.stack.imgur.com/uTERq.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Recommend

  • Why ggplot2 pie-chart facet confuses the facet labelling
  • How to plot stacked proportional graph?
  • Multiple plots on one page using ggplot
  • How do I use ggplot2 to create a border around a group of US counties?
  • boxplot won't display with ggvis
  • R ggvis interactive slider for calculating y values (e.g. for background correction)
  • How to deal with SpiderWebPlot in JFreeChart?
  • Error while installing ggplot in python
  • Specify regression line intercept (R & ggplot2)
  • How to model a mixture of finite components from different parametric families with JAGS?
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • Using : for multiple slicing in list or numpy array
  • iOS 6 dateFromString returns wrong date
  • Geom_jitter colour based on values
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • For loop with if condition on multiple R functions
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • Breaking out column by groups in Pandas
  • wxPython: displaying multiple widgets in same frame
  • R - Combining Columns to String Based on Logical Match
  • Knitr HTML Loop - Some HTML output, some R output
  • Importing jscolor library in angular 2
  • QuartzCore.framework for Mono Develop
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Arrays break string types in Julia
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • embed rChart in Markdown
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal