Dear all,
I have a continuos variable (a ratio ranging [0;4]) and I want to color different region of a map based on the ratio of each given region. So far so good.
ggplot(data=region,aes(x=long,y=lat, group=group, fill=ratio))+geom_polygon(color="gray60", size=0.15)+scale_fill_gradient(low="yellow", high="purple")
R plot the legend as a rectangle from yellow to purple. I would like to have a legend that shows only few values, as it they were discrete (i.e. a yellow box for ratio=0, a orangish box for ratio=2 and a purple box for ratio=4).
How should I do?
Thank you in advance,
F.
mara
April 19, 2019, 3:43pm
2
There's an open issue to implement what I think you're referring to in ggplot2, as well as a couple of possible implementations in the thread (e.g. see Claus Wilke's here ).
opened 12:42PM - 30 May 18 UTC
closed 06:39AM - 25 Jun 20 UTC
feature
guides
This is a feature request.
`ggplot2` does not provide a way to use discrete col… or scales in which the labels are staggered compared to the colors. In my opinion, this kind of color bars is more intuitive than color bars where each color is assigned an explicit interval (usually in a form like `[2,5)`).
Using only base `ggplot`, this is the best that I could come up with ([SO question link](https://stackoverflow.com/questions/26802272/generating-a-color-legend-with-shifted-labels-using-ggplot2)), but it's very hackish, involving shifting the labels and using a dummy empty label at the end:
![](https://i.stack.imgur.com/7g4mf.png)
While this works, it is a bit hackish, the labels often are a bit shifted depending on plot size, and the white spaces between the color boxes do not look very good.
Most other plotting systems allow for this kind of color bar: here are some examples of the kind of color bar that I'd like in `ggplot`, implemented in other plotting systems:
- Lattice ([source](https://stackoverflow.com/questions/21641186/r-levelplot-colorkey-label-and-color-range)):
![](https://i.stack.imgur.com/3j2lZ.png)
- Base graphics ([source](https://stackoverflow.com/questions/49490246/plot-continuous-data-with-discrete-colors)):
![](https://i.stack.imgur.com/oEteR.png)
- Matplotlib ([source](https://stackoverflow.com/questions/14777066/matplotlib-discrete-colorbar)):
![](https://i.stack.imgur.com/6OvdM.png)
- MATLAB ([source](https://www.mathworks.com/matlabcentral/answers/203750-define-a-sequential-discrete-color-map-with-first-color-white)):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/29144/fig.bmp)
I have written a small function ([SO link with examples](https://stackoverflow.com/a/50540633/4124334)) to be able to plot this kind of color bar as a `ggplot` object, to be added to other objects using `patchwork` or similar. It's not a great solution however: a built-in guide would be much much better.
Example result:
![](https://i.stack.imgur.com/RXBhr.png)
Note that in this case the last "bin" in the color bar indicates that the last color represents values going up to infinity; this is a useful feature in many cases.
Could something like this be implemented as a built-in guide?
You can see Claus has implemented something similar-ish (discretized gradient scale) for value-suppressing uncertainty palettes in the multiscales package, but for a more complex legend
Could you please turn your example into a self-contained reprex (short for repr oducible ex ample)? It will help us help you if we can be sure we're all working with/looking at the same stuff.
install.packages("reprex")
If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page . The reprex dos and don'ts are also useful.
There's also a nice FAQ on how to do a minimal reprex for beginners, below:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
What to do if you run into clipboard problems
If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.
reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")
For pointers specific to the community site, check out the reprex FAQ .
system
Closed
May 10, 2019, 3:43pm
3
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.