create a vector in R using variable names - r

I have a variable called school_name
I am creating a vector to define colors that I will use later in ggplot2.
colors <- c("School1" = "yellow", "School2" = "red", ______ = "Orange")
In my code I am using the variable school_name for some logic want to add that as the third element of my vector. The value changes in my for loop and cannot be hard-coded.
I have tried the following but it does not work.
colors <- c("School1" = "yellow", "School2" = "red", get("school_name") = "Orange")
Please can someone help me with this

You can use structure:
school_name = "coolSchool"
colors <- structure(c("yellow", "red", "orange"), .Names = c("School1","School2", school_name))

You can just set the names of the colors using names():
colors <- c("yellow", "red", "orange")
names(colors) <- c("School1", "School2", school_name)

This also works:
school_name <- "school3"
colors <- c("School1" = "yellow", "School2" = "red")
colors[school_name] <- "Orange"
# School1 School2 school3
# "yellow" "red" "Orange"

Related

Correctly Specifying Colors in Plotly

I am looking at this tutorial over here : https://plotly.com/r/line-and-scatter/
I tried to make a scatter plot in plotly (in R). The data I am using looks something like this:
library(plotly)
var1 = rnorm(100,100,100)
var2 = rnorm(100,100, 100)
my_data = data.frame(var1, var2)
my_data$color = ifelse(my_data$var1 > 0 & my_data$var1 < 50, "red", ifelse(my_data$var1>=50 & my_data$var1<100, "green", "yellow"))
Based on the tutorial, I then tried to color the points:
pal <- c("red", "green", "yellow")
pal <- setNames(pal, c("red", "green", "yellow"))
fig <- plot_ly(data = my_data, x = ~var1, y = ~var2, color = ~color, colors = pal)
fig
Is there a way to directly specify these colors without using the "pal" statement and directly specifying the colors from the data frame itself? I am worried that a point that "should" be colored a certain color (i.e. based on my_data$color) might not actually be colored with that color (e.g. a point that I wanted colored "red" is actually colored "green").
Is there a standard reference that can be used to specify colors? For example, the yellow color is too bright and difficult to read. I found this website that allows you to convert colors to their hexadecimal format (https://www.rapidtables.com/convert/color/rgb-to-hex.html), and this website that contains popular choices of colors in R (http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf). Can such inputs be used for specifying colors in plotly?
Thanks!
If I fully understand the question, then you can try this solution:
Code:
library(plotly)
my_data <- data.frame(var1 = rnorm(100, 100, 100),
var2 = rnorm(100, 100, 100))
my_data$color <- ifelse(my_data$var1 > 0 & my_data$var1 < 50, "red",
ifelse(my_data$var1>=50 & my_data$var1<100, "green", "yellow"))
fig <- plot_ly(data = my_data, x = ~var1, y = ~var2, color = ~color, colors = c("#228B22", "#FF4500", "#CCCC00"))
fig
Output:

R - Finding duplicates in list entries

I am trying to figure out how to get duplicates out of list objects in R.
So my example list:
examplelist <- list(a = c("blue", "red", "yellow"),
b = c("red", "black", "green"),
c = c("black", "green", "brown"))
What I would like to get as a result:
duplicates: c("red", "black", "green")
vector of all entries, without double entries: c("blue", "red", "yellow", "black", "green", "brown")
I was not able to find a function for that other than duplicated() which just checks my list objects in total but not the entries itselves.
Thank you for your help :)
You can unlist first:
unlisted <- unlist(examplelist)
unlisted[duplicated(unlisted)]
# b1 c1 c2
# "red" "black" "green"
unlisted[!duplicated(unlisted)]
# a1 a2 a3 b2 b3 c3
# "blue" "red" "yellow" "black" "green" "brown"
If you only want the vector (without the names), use unname:
unlisted <- unname(unlist(examplelist))

How do I change the font color of specific variables in corrplot in R?

How do I change the color of the font tl.col specific for certain variables like in the image?
Do I have to create a variable before? Or can you select columns within the command?
corrplot(cor(iris[,-5]), tl.col="black", tl.cex=0.8, tl.srt=70)
tl.col takes a vector with colors for each variable. So you can set the color for each variable directly with the tl.col argument like this:
corrplot(cor(iris[,-5]), tl.col=c("red", "red", "blue", "blue"), tl.cex=0.8, tl.srt=70)
Alternatively you can define the colors within a function:
colors <- ifelse(grepl("Sepal", names(iris)[-5]), "red", "blue")
corrplot(cor(iris[,-5]), tl.col=colors, tl.cex=0.8, tl.srt=70)
# or
corrplot(cor(iris[,-5]), tl.col=ifelse(grepl("Sepal", names(iris)[-5]), "red", "blue"),
tl.cex=0.8, tl.srt=70)
Try with this:
library (corrplot)
library(MASS)
library(calibrate)
data("iris")
corrplot(cor(iris[,-5]), cl.pos = "n", tl.pos = "n")
textxy(0,1,labs=c("Petal.Width"),cex=0.8,offset=-0.2,col="blue")
textxy(0,2,labs=c("Petal.Length"),cex=0.8,offset=-0.2,col="blue")
textxy(4,5,labs=c("Petal.Width"),cex=0.8,offset=0,col="blue",srt=45)
textxy(3,5,labs=c("Petal.Length"),cex=0.8,offset=0,col="blue",srt=45)
textxy(0,3,labs=c("Sepal.Width"),cex=0.8,offset=-0.2,col="red")
textxy(0,4,labs=c("Sepal.Length"),cex=0.8,offset=-0.2,col="red")
textxy(2,5,labs=c("Sepal.Width"),cex=0.8,offset=0,col="red",srt=45)
textxy(1,5,labs=c("Sepal.Length"),cex=0.8,offset=0,col="red",srt=45)
Basically you have to remove the variable names displayed by corrplot and textxy creates labels. You have to set position, color..., it's a shortcut for your problem

Color dendrogram branches based on external labels uptowards the root until the label matches

From question Color branches of dendrogram using an existing column, I can color the branches near the leaf of the dendrogram. The code:
x<-1:100
dim(x)<-c(10,10)
set.seed(1)
groups<-c("red","red", "red", "red", "blue", "blue", "blue","blue", "red", "blue")
x.clust<-as.dendrogram(hclust(dist(x)))
x.clust.dend <- x.clust
labels_colors(x.clust.dend) <- groups
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = groups, edgePar = "col") # add the colors.
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = 3, edgePar = "lwd") # make the lines thick
plot(x.clust.dend)
generates a dendrogram as shown in:
However, I want to color the branches up towards the root until the all the leaves in the current branch have the same labels. Even if there is a single mismatch switch to the default color of black. I want the resulting dendrogram to look like
What I want is little different from using color_branches like
x.clust.dend <-color_branches(x.clust.dend,k=3)
because it colors based on its own clusters not based on some external labels.
The function you are looking for is branches_attr_by_clusters. Here is how to use it:
library(dendextend)
x <- 1:100
dim(x) <- c(10, 10)
set.seed(1)
groups <- c("red","red", "red", "red", "blue", "blue", "blue","blue", "red", "blue")
dend <- as.dendrogram(hclust(dist(x)))
clusters <- as.numeric(factor(groups, levels = c("red", "blue")))
dend2 <-
branches_attr_by_clusters(dend , clusters, values = groups)
plot(dend2)
This function was originally created to display the results of dynamicTreeCut. See the vignette for another example.

change background and text of strips associated to multiple panels in R / lattice

The following is the example I work on.
require(lattice)
data(barley)
xyplot(yield ~ year | site, data = barley)
I want to put different strip color for different sprips and font color is also different optimized with the backgroud color. For example:
strip background colors = c("black", "green4", "blue", "red", "purple", "yellow")
font color = c("white", "yellow", "white", "white", "green", "red")
Rough sketch of the first one is provided:
How can I achieve this?
Here's a clean and easily customizable solution.
myStripStyle(), the function that is passed in to the strip= argument of xyplot() uses the counter variable which.panel to select colors and also the value of factor.levels for the panel that's currently being plotted.
If you want to play around with the settings, just put a browser() somewhere inside the definition of myStripStyle() and have at it!
bgColors <- c("black", "green4", "blue", "red", "purple", "yellow")
txtColors <- c("white", "yellow", "white", "white", "green", "red")
# Create a function to be passed to "strip=" argument of xyplot
myStripStyle <- function(which.panel, factor.levels, ...) {
panel.rect(0, 0, 1, 1,
col = bgColors[which.panel],
border = 1)
panel.text(x = 0.5, y = 0.5,
font=2,
lab = factor.levels[which.panel],
col = txtColors[which.panel])
}
xyplot(yield ~ year | site, data = barley, strip=myStripStyle)
It might not be wise to refer to variables outside of the scope of the function.
You could use par.strip.text to pass additional arguments to the strip function. par.strip.text can be defined at the plot level and is generally used for setting text display properties, but beeing a list you can use it to bring your variables to the strip function.
bgColors <- c("black", "green4", "blue", "red", "purple", "yellow")
txtColors <- c("white", "yellow", "white", "white", "green", "red")
# Create a function to be passes to "strip=" argument of xyplot
myStripStyle <- function(which.panel, factor.levels, par.strip.text,
custBgCol=par.strip.text$custBgCol,
custTxtCol=par.strip.text$custTxtCol,...) {
panel.rect(0, 0, 1, 1,
col = custBgCol[which.panel],
border = 1)
panel.text(x = 0.5, y = 0.5,
font=2,
lab = factor.levels[which.panel],
col = custTxtCol[which.panel])
}
xyplot(yield ~ year | site, data = barley,
par.strip.text=list(custBgCol=bgColors,
custTxtCol=txtColors),
strip=myStripStyle)

Resources