Does R have color palettes?
In other words, I am looking for an array of 6 or so color names that go well together in a graph or plot; maybe there are some predefined schemes like that?
RColorBrewer, as mentioned by deinst, is very useful -- even though it was designed for maps rather than line charts.
A number of other packages offer help with palettes:
gplots has colorpanel(), rich.colors(), ...
hcl-colors
colorRamps
colorspace
caTools
caTools
...
as can be seen from a quick query on 'palette' at rseek.org.
The easiest way to generate a palette is using generic functions from the basic grDevices package:
rainbow()
topo.colors()
terrain.colors()
heat.colors()
These are useful if the desired number of colors doesn't exceed 7-8. The only necessary argument is the number of colors in palette.
There is also gray() function which can be used to generate various schades of gray.
Or you could do something like:
pal <- colorRampPalette(c("red", "blue", "plum"))
barplot(t(as.matrix(mydf)), beside=TRUE, col=pal(3))
Look at the RColorBrewer package. The colors are not named, but I think that they are close to what you are looking for.
Visit this page before using RColorBrewer. Select the number of your data classes in the top and then define the nature of your data. You may also find this page useful.
No one mention this but look at palette function (?palette) which define default pallet.
palette()[1:6] gives you first six default colours.
Related
today I downloaded the ggsci colour palette in R because I want a diagramme to be displayed in, let's say, the NEJM colours.
Thing is, after installing the package and adding it to the library, I have not the slightest clue how to use it. No matter what I try, the programme always indicates different kinds of errors which I don't know how to fix. Could someone who's familiar with using these palettes just explain what I have to do in general, i.e. what parameters I have to insert in here:
barplot(Z,beside=TRUE,names=c("a","b","c"),col = ??????? )
You can get the colours like this:
library(ggsci)
COLS = pal_nejm()(8)
[1] "#BC3C29FF" "#0072B5FF" "#E18727FF" "#20854EFF" "#7876B1FF" "#6F99ADFF"
[7] "#FFDC91FF" "#EE4C97FF"
If you want to colour your bars sequentially, do:
barplot(1:8,col=COLS)
If it's like stacked, and you wanna use 2 colours for top and bottom:
barplot(rbind(1:8,8:1),col=COLS[1:2])
I am using R to create a plot that contains 20 distinct groups, and I would like to color each of them differently. I am also familiar with Matlab and when working with that program I have found that "distinguishable_colors" matlab file worked best for distinguishing different colors 1. I have looked at the "rainbow", "rainbow_hcl", and "brewer" palettes, but none of them look as good as "distinguishable_colors.mat". I am wondering if anyone knows of a function in R that will create the same palette as the "distinguishable_colors.mat" matlab function?
That function in MatLab seems to iteratively search over RBG space for sets of color which are maximally different from each other. I don't know of such a thing in R, but we can come pretty close.
We can find a color palette of a few colors which suits our needs (perhaps using http://colorbrewer2.org/) and use those seed color to create a colorRampPalette for any number of colors.
pal<-colorRampPalette(c('#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00'))
N=10
plot(rnorm(N),rnorm(N),pch=16,col=pal(N),cex=3)
The R package Polychrome provides tools for qualitative palettes with many (20 or more) colors. It comes with two vignettes giving the provided palettes and tools for creating palettes.
A while ago, I asked How to change Lattice graphics default groups colors?, and got a helpful response from BenBarnes. This allowed me to define more than 7 cycling colors for different data in the same plot in R's Lattice package, which I did. However, I found that it's difficult to define more than 9, maybe 10 colors are not (a) hard to see on a white background, or (b) include pairs of colors that look very similar. (That might be why seven colors is Lattice's default, obviously.) It occurs to me, though, that there are people out there who are much better at managing colors in information display than I am, and that maybe someone had already defined a good list of 10, 12, maybe even 15 colors for display of data in the same plot. Anybody know of such a list? Any color specification that I can convert into a Lattice format would work. If it's already been done in Lattice, even better! (Is there a better place to ask this question??)
There's a large body of work on choosing colors. Check out the RColorBrewer and colorspace packages as a starting point. In the documentation for colorspace there is a link to an excellent paper (and the vignette summarizes much of the paper). And think about your color blind colleagues, with dichromat.
In general, I think it is very difficult to pick a large set of colors that don't end up being hard to distinguish from one another. When I am looking for a large number (>8) of colors that I want to be noticeably distinct and aesthetically pleasing, I usually use the rich.colors palette in the gplots package. I find it more useful than the similar rainbow palette, because the colors don't wrap around on each other.
I am trying to illustrate a histogram of 33 different variables. Due to the number of variables I think "beside" different Colors I need to label each bar in a clear way, even using an arrow, if its doable.
I was wondering about
1) How can I define 33 distinct color in R
2) How can I label them, say vertical below X axis with a certain distance from each other to make my figure more clear.
I am using multhist function from Plotrix package, and for data you can image just 33 random vector with different length !
Thanks
As Chris mentioned, trying to distinguish 33 colours doesn't work for humans. You need to find a different plot type that doesn't rely on only colour.
Without a reproducible example, it is not possible to say what this plot should be, but here's some generic colour advice.
Use HCL colours rather the RGB or HSV. Read Escaping RGBland by Achim Zeileis for an explanation. There are some useful functions for generating palettes in the colorspace package.
If your variables are unordered categories (i.e., encoded as factors) then your colours should have different hues. (Use rainbow_hcl.)
If your variables are in some sort of order (ranges or ordered factors) then your colours should have different lightness or chroma. (Use sequential_hcl.) A variation on this is if they differ about some midpoint, in which case you need diverge_hcl.
You can define colors in R in any number of ways; try ?rainbow or ?greyscale for some suggestions
You could also look at all the colors here and just create a vector of your desired colors that you call inside your plot function.
Your problem though is that the human eye and the printing process has trouble distinguishing and reproducing that many distinct colors. See the documentation at the colorbrewer site for more information (and advice on picking colors).
Not sure I understand what your trying to do with the labels, but you can re-label an axis with a call to axis. See the documentation in ?axis.
I am wondering if in R there is a per-existing package that can colorate sets inside graph or a package that can generate a list of colors that are not close,
Because I have a graph that have many clusters and I want to color but I don't want to colors to be close.
I have found a nice answer here but I am wondering if there is a per-existing package for
You may also want to check out the package RColorBrewer for other built in color palettes. However, you may run into issues if you need large numbers of colours. There is a nice post on CrossValidated which addresses the large n issue and offers a few nice solutions as well. Specifically, would it make sense to facet your plot based on some large groupings? Do you need to plot all of the items at once? ggplot2 makes it easy to facet based on a column in your data. I'm sure there are equivalent functions in base graphics and lattice, but I'm not as familiar with them.
See the functions rainbow, heat.colors, terrain.colors etc, described in the help pages (?rainbow). These are part of the grDevices package, which is installed by default.