Changing Viridis to different color palette - r

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.

Related

ggerrorplot: how can I change the colors on the Plot?

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 need to change the color and fill of a barplot

I have the following code that I used to create a barplot:
barplot(table(data$r_difficulty), ylab = "count", xlab = "r_difficulty")
I was wondering how I can change the fill and color with this code.
Using the col= argument for the fill color and border= for the border, among others it accepts numeric vectors of predefined colors from 1 to 8 (see below).
barplot(table(dat$value), ylab = "count", xlab = "r_diff", col=1:8, border=8:1)
You may also:
use a vector of color names such as c("red", "blue"), check colors() for all available color names
define any RGB color such as #FF0000, or #FF000000 where the additional two digits are for the alpha (transparency)
use color palettes such as the builtin rainbow, hcl.colors, heat.colors etc., or those of a whole bunch of additional packages
mix some of the listed possibilities
Data:
set.seed(42)
dat <- data.frame(value=sample(8, 100, replace=TRUE))

How to manually change diverging colors in leaflet map/R?

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

Modify viridis palette in ggplot

I'm trying to color a ggplot by a factor that's mostly numerical, like so:
iris %>%
ggplot(aes(Sepal.Length, Sepal.Width, color = cut(Petal.Length, 0:7))) +
geom_point() +
scale_color_viridis_d()
What I would like to do is to special-case the (0,1] value and color that in red but to retain the viridis gradient for the remaining values. I know I could change the color on the fly but I wonder if it's also possible to construct a new discrete color palette that has red as its first color and the gradient thereafter, which would make the code a bit more re-usable.
The more general question is therefore: Can one easily add/remove/edit the colors in an existing discrete color palette?
This is basically the same as #seasmith's answer, except with the addition of saving the palette to a variable to use later. I do this often for explicitly setting a NA color and then using the palette across multiple plots.
Other packages have similar functions to the viridis ones: for example, you can get ColorBrewer palettes from RColorBrewer::brewer.pal, CartoColor palettes from rcartocolor::carto_pal, or any of the ggthemes ones from functions with the same name as the palette.
library(tidyverse)
plasma_pal <- c("red", viridis::plasma(n = 6))
plasma_pal
#> [1] "red" "#0D0887FF" "#6A00A8FF" "#B12A90FF" "#E16462FF" "#FCA636FF"
#> [7] "#F0F921FF"
iris %>%
ggplot(aes(Sepal.Length, Sepal.Width, color = cut(Petal.Length, 0:7))) +
geom_point() +
scale_color_manual(values = plasma_pal)
Created on 2018-04-19 by the reprex package (v0.2.0).
Requires a call to scale_color_manual()
iris %>%
ggplot(aes(Sepal.Length, Sepal.Width, color = cut(Petal.Length, 0:7))) +
geom_point() +
scale_color_manual(values = c("red", viridis::viridis(6)))

Changing color of heatmap in R

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.

Resources