I'm completely helpless using a downloaded colour palette in R - r

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])

Related

How to Change X-Axis Label in ChromoMap?

I am new to R and I have been using chromoMap library in R to visualize annotation plots and visualizing the feature-associated data. It's a great library and produces great charts. However, I was not able to find any option for changing the x-axis label from length (bp) to anything else. Because of this, I am not able to use any of the produced charts. It sounds like a small issue, but it totally affects the usability of this great package. I followed this tutorial link and produced the below chart. In my chart and all the samples, the x-axis label is fixed and this is my problem and I am looking for a way to just change it.
library(chromoMap)
chr_file_1 = "chr_file_without_centromere.txt"
anno_file_1 = "annotation_pos.txt"
chromoMap(chr_file_1,anno_file_1)
I am wondering if anybody has the same experience ?? This package produces output as a htmlwidget object and therefore I could not change the x-axis lable. Is there any way to modify a htmlwidget object? or Any way to change this bp to something else??

Plot in R won't change color

This is probably going to sound like a really stupid question but my plot in R will not change color!
this is my line in R
plot(MasterModCfs$V3,MasterModCfs$V5,col="blue")
I have tried it with spaces and without, with different colors, reloading, everything I could think of. If it's important, MasterModCfs contains literally thousands of data values.
I did one of the examples just to check
cars <- c(1, 3, 6, 4, 9)
plot(cars)
plot(cars, col="blue")
and it's blue. So that works.
Why won't my plot change colors?
Googling the same problem, I came across this present page - but subsequently found the source of my own issue through trial and error so posting here in case it helps. My x axis values were set up as factors - when I re-converted them to a normal string using as.character(), I was able to re-apply my own colours finally.
Without knowing what your par() settings are and what the classes of your columns, it's impossible to say.
You might try adding 'pch=21, bg="red"' to your plot command and see if that changes anything. It might give you a hint.
I suggest using the aes command in ggplot2. You can try
ggplot(data = MasterModCfs) + geom_point (mapping =aes (x=V3, y=V5), color="blue")
That should work!
Using plot directly I am not sure how to do it. I try your simple example (with cars) but it also fails if you add another vector for the "y" axis (as you wish to do in the end). To be honest, I have no idea why it works using only "cars" and the color.

Heatmap in R, representation of colors and removal of x-axis

I made a heatmap. The code I used is:
heatmap(t(data.matrix(survey)))
I don't need anything on x-axis. In plots, the following command would delete the numbers in x-axis:
xaxt='n'
Also, if I want to add a chart at the top (which tells about the representation of colors - like yellow means lower values and red means higher), how can I do that? I have no idea so I didn't even try. The only thing I can think of is 'scale' but that didn't work.
Lastly, I tried to change the color (green and red) and for that I used:
mycol = c("green","red")
heatmap(t(data.matrix(zscoreplus)),col=mycol)
Unlike 1st pic, there are no colors in between. (1st one had a lot more variety.) What I was trying to get was red, light red, reddish-green, green, dark green, etc...
p.s. For some reason, gplots and heatmap.2 are not installed and R can not find those packages.
Instead of using the basic heatmap() function, you could load the gplots package and use heatmap.2() - in your case same syntax - to get a color key. Let me know if you have any further questions about the heatmap.2() package.
EDIT:
Sorry, didn't read that you cannot install gplots. Is it because of limited admin rights?
Unfortunately, heatmap() is kind of limited regarding the color key.
But for the red -> green problem I have a solution for you. To create your own color palette, try
my_palette <- colorRampPalette(c("red", "green"))(n =
1000)
and then use it as color in your heat map:
heatmap(..., col = my_palette, ...)
How important is clustering in your case? If you don't need clustering, you can use the levelplot() function (comes with R), which has a nice color key representation.
EDIT2:
Regarding the color "scale" problem. I assumed that you mean something like legend according to the description in your first post. So is something like in the screenshot below that you want?
EDIT3
Regarding the x-labels:
Unfortunately, there is no direct option in heatmap.2() to turn those off. THose x-labels are the colnames for your matrix that you read in. By xlabel you would just control the general description of the axis (it is turned off by default). Here is a Screenshot that shows what I mean when xlabels is used:
Maybe you could just give your matrix empty ( " " ) colnames. That should help.
On the other hand, I am sorry to ask you this, but this doesn't make sense if you are using clustering. How would you know which is which?
An alternative solution is to simply crop the region or code from the pdf, or svg once you saved the heat map. Shouldn't take more than 5 seconds.
Regarding your problems to install gplots: You forgot the quotes.
require(gplots) Loading required package: gplots Warning message: In
library(package, lib.loc = lib.loc, character.only = TRUE,
logical.return = TRUE, : there is no package called ‘gplots’ >
install.packages(gplots) Error in install.packages : object 'gplots'
not found – ayesha malik 8 mins ago
Try
install.packages("gplots", dependencies = TRUE)

Are there good predefined color sequences for different data in one plot?

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.

Color schemes in R?

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.

Resources