I have a file that contains the cluster results. I created a heatmap in R by the following code.How to change the color of heatmap to red and green
nba <- read.csv("E:/clus.arff", sep=",")
nba_heatmap <- heatmap(nba_matrix, Rowv=NA, Colv=NA, col = cm.colors(256), scale="column", margins=c(5,10))
You are using the cm.color palette which is a cyan-purple palette.
You can create your own palette for instance using RColorBrewer.
RColorBrewer provides a red-yellow-green palette you can use
library(RColorBrewer)
pal <- colorRampPalette(brewer.pal(11, "RdYlGn"))(100)
brewer.pal loads the palette (the palette has 11 colors, we're gonna use them all) and then colorRampPalette interpolates it to 100 colors.
Alternatively you can define your own colors:
redgreen <- c("red", "green")
pal <- colorRampPalette(redgreen)(100)
After creating the palette just use it as the col parameter to heatmap.
Related
I would like to add a legend to a pie chart in R
The default color palette is fine for me, but how can I use it in the legend (to match the colors of the plot)?
pie(table(iris$Species))
legend('right',c('A','B','C'), fill = colors(3))
(I know that I could create a vector of colors to use in the chart in the legend in advance like in my_colors <- colors(3), but this would then not be the default colors.
The official documentation only says that the pie() function uses "pastel" colors if the col argument is omitted, but doesn't specify the exact values.)
If you look at the body of the pie function, you will see that the colors are hard-coded into it in the following lines:
if (is.null(col))
col <- if (is.null(density))
c("white", "lightblue", "mistyrose",
"lightcyan", "lavender", "cornsilk")
else par("fg")
So, you can use the same colors yourself in the call to legend
pie(table(iris$Species))
legend('right', c('A','B','C'), fill = c("white", "lightblue", "mistyrose"))
How can one change this viridis color platte as shown below to other types of viridis color palette??The code used and the map displayed below is shown for reference:
The col.regions option does that. You can give it a different color palette as an argument:
library(mapview)
data(franconia)
mapview(franconia,
zcol = "district",
col.region=colorRampPalette(c("blue", "red")))
Created on 2022-06-03 by the reprex package (v2.0.1)
Since the question asked specifically about the viridis color palette, code would be :
library(mapview)
data(franconia)
mapview(franconia,
zcol = "district",
col.region=viridis::viridis_pal(option = "A"))
Where you can change option="A" for "B" or "C" (the current palette used is "D").
For using the other gradients from viridis, you can use
library(mapview)
data(franconia)
mapview(franconia,
zcol = "district",
col.region=viridis::mako(n = 3))
where n must be at least as big as the number of discrete variable or bins.
For your gradient, I see you have 6 values on your scale, so I'd put n at 6.
Is it possible to give every variable a different color and how?
I would like to keep the legend.
Please note that it is made with ggerrorplot function.
You can use a different palette in your ggerrorplot function. I will use the ToothGrowth dataset as an example:
the color palette to be used for coloring or filling by groups.
Allowed values include "grey" for grey color palettes; brewer palettes
e.g. "RdBu", "Blues", ...; or custom color palette e.g. c("blue",
"red"); and scientific journal palettes from ggsci R package, e.g.:
"npg", "aaas", "lancet", "jco", "ucscgb", "uchicago", "simpsons" and
"rickandmorty".
For example use palette="Paired" for blue color:
df<- ToothGrowth
library(ggpubr)
ggerrorplot(df, x = "dose", y = "len",
color = "supp", palette = "Paired",
error.plot = "pointrange",
position = position_dodge(0.5))
Output:
Or palette="npg":
I have produced a map of Denmark
library(leaflet.extras)
library(leaflet)
#Data
folk1 <- readr::read_csv2("http://api.statbank.dk/v1/data/folk1a/CSV?OMR%C3%85DE=*")
municipalityDK("INDHOLD","OMRÅDE",data=folk1,legend=T,pal = "GnBu") %>%
setMapWidgetStyle(list(background= "white"))
Which looks like:
However, I have not succeeded in finding a color palette that visualizes data the way I prefer. I have used those listed here.
I would like the color palette to look like this:
Can this be done manually?
You can create a custom palette with RColorBrewer:
library(RColorBrewer)
colfunc <- colorRampPalette(c("#2C77BF", "red"))
Then include your custom palette and number of selected colors in your map:
municipalityDK("INDHOLD","OMRÅDE",data=folk1,legend=T,pal = colfunc(10)) %>%
setMapWidgetStyle(list(background= "white"))
Map
I want to change the colours of my clusters but maintain the shape. habillage=iris$Species changes both colour and shape, what can to change only colours?
library("devtools")
install_github("kassambara/factoextra")
library("factoextra")
res.pca <- prcomp(iris[, -5], scale = TRUE)
fviz_pca_ind(iris.pca, geom="point", pointsize = 1, habillage=iris$Species, addEllipses=TRUE, ellipse.level=0.95)
fviz_pca() works like ggplot plots, hence, for changing shapes you can use the aesthetic mappings of ggplot, e.g., for changing shapes. Concerning colors, the documentation called by ?fviz_pca_ind tells you that you can change colors via palette.
fviz_pca_ind(res.pca, geom="point", pointsize = 1, habillage=iris$Species, addEllipses=TRUE, ellipse.level=0.95
, palette = c("green", "orange", "grey") #change colors
) +
scale_shape_manual(values=c(2, 8, 11)) #change shapes