"red", "red", "red", "green", "green", "blue"
I have tried using rep, etc but I can only get one the colors.
use rep and/or c, twice at most, each with a max of 2 arguments.
rep(c("red", "green", "blue"), c(3, 2, 1))
Related
I need to add values of same column names across four different data frames in R. The problem is that there are different number of columns in these 4 data frames with only one data frame out of them containing all the columns. Rest of the data frames have a subset of column names of the 1st data frame. Number of rows are equal across 4 data frames.
Minimum replicable example is:
Say there are 4 data frames with the structure as follows:
df1 <- setNames(data.frame(matrix(ncol = 10, nrow = 900)), c("Red", "Blue", "Yellow", "Green", "Orange", "Pink", "Brown", "Black", "Grey", "Purple"))
df2 <- setNames(data.frame(matrix(ncol = 9, nrow = 900)), c("Red", "Blue", "Yellow", "Orange", "Pink", "Brown", "Black", "Grey", "Purple"))
df3 <- setNames(data.frame(matrix(ncol = 8, nrow = 900)), c("Red", "Blue", "Yellow", "Orange", "Pink", "Brown", "Black", "Purple"))
df4 <- setNames(data.frame(matrix(ncol = 6, nrow = 900)), c("Red", "Yellow", "Green", "Orange", "Brown", "Purple")
Assume that each of these columns in the four data frames have integer values across 900 rows. How do I return a data frame which is basically the addition of values of same columns across four data frames?
In other words, df.sum[1:10] <- df1[1:10] + df2[1:9] + df3[1:8] + df4[1:6], but while adding identify the same columns to be added
If there are no NA elements, we can do the + after making the dimensions same
lst <- mget(paste0("df", 1:4)) # get the datasets in a list
nm1 <- Reduce(union, lapply(lst, names)) # find all the column names
# assign missing columns in each of the dataset with value 0
# get the `+` of all list elements with Reduce
dfout <- Reduce(`+`, lapply(lst, function(x) {
x[setdiff(nm1, names(x))] <- 0
x[nm1]}))
dim(dfout)
#[1] 900 10
data
set.seed(24)
df1 <- setNames(data.frame(matrix(rnorm(900 * 10), ncol = 10, nrow = 900)),
c("Red", "Blue", "Yellow", "Green", "Orange", "Pink",
"Brown", "Black", "Grey", "Purple"))
df2 <- setNames(data.frame(matrix(rnorm(900 * 9), ncol = 9, nrow = 900)),
c("Red", "Blue", "Yellow", "Orange", "Pink", "Brown",
"Black", "Grey", "Purple"))
df3 <- setNames(data.frame(matrix(rnorm(900 * 8), ncol = 8, nrow = 900)),
c("Red", "Blue", "Yellow", "Orange", "Pink", "Brown", "Black", "Purple"))
df4 <- setNames(data.frame(matrix(rnorm(900 * 6), ncol = 6, nrow = 900)),
c("Red", "Yellow", "Green", "Orange", "Brown", "Purple"))
I'm fairly new to R.
I'm trying to create a density plot, which wasn't problem thanks to previous questions & answers here.
My current problem is the graphs's legend. I've assigned the wanted colors (col = c("red", "orange", "yellow", "green", "blue", "purple")) but they don't show on the legend itself, instead I get random colors.
I think it's important to mention that there are no errors after running the code & i've checked that all these colors are available here.
This the density plot code, along with the legend info.
plot(density(df$a1), col = "red", xlim = c(0, 1000), ylim = c(0, 0.004))
lines(density(df$a2), col = "orange")
lines(density(df$a3), col = "yellow")
lines(density(df$a4), col = "green")
lines(density(df$a5), col = "blue")
lines(density(df$a6), col = "purple")
legend(x = "topright", legend = names(df), fill = 1:6, col = c("red", "orange", "yellow", "green", "blue", "purple"))
Yet, the result is this:
Thank you!
fill specifies color for boxes while colspecifies color for points and lines.
In your case, the code should be:
legend(x = "topright", legend = names(df), lty=1, col = c("red", "orange", "yellow", "green", "blue", "purple"))
lty=1 indicates that you want solid lines in place of boxes (lty stands for line type).
I would like to replicate the annotation image seen below, by creating a matrix of colors based on annotations I have created:
ann1 <- c("blue", "red", "green", "red")
ann2 <- c("black", "gray", "yellow", "white")
ann3 <- c("orange", "blue", "pink", "green")
object <- cbind(ann1, ann2, ann3)
Which function can I use? (you don't need you to do the work!)
Finished product for future reference:
ann1 <- c("blue", "blue", "red", "red")
ann2 <- c("black", "gray", "black", "black")
ann3 <- c("red", "red", "red", "green")
object <- cbind(ann1, ann2, ann3)
colnames(object) <- c("group1", "group2", "group3")
image(matrix(1:length(object), nrow(object), ncol(object)), col=object, yaxt='n', xaxt='n', yaxs="i")
par(las=1)
increment <- seq(0,1, by=(1/(ncol(object)-1)))
increment <- increment[1:ncol(object)]
axis(side=2, at=increment, labels=colnames(object))
One way to get you started...
ann1 <- c("blue", "red", "green", "red")
ann2 <- c("black", "gray", "yellow", "white")
ann3 <- c("orange", "blue", "pink", "green")
object <- cbind(ann1, ann2, ann3)
image(t(matrix(1:12, 4, 3)), col=object)
I have a dataset that looks like that, I want to plot the exact same bar plot in R.
This is what I've been trying:
barplot(as.matrix(table[,2:8]), beside = T, ylim= c(0,1),
col = c("red", "blue", "green", "yellow",
"orange", "purple", "cyan", "grey",
"deeppink", "red4", "black", "brown"))
I get a bar plot, but now when I want to change the labels instead of a,b,c...g to this:
(-0.20,-0.15)
(-0.15,-0.10)
.
.
.
(0.20,0.25)
I get an X character in front of every label I was trying to write:
Thank you.
I am using heatmaps.plus to create heatmaps with RowSideColors. The only thing I can't seem to figure out is how to make a legend for the RowSideColors (for example, green is Group1 and black is Group2). Any help would be greatly appreciated, thank you!
You should use legend for this.
library("heatmap.plus")
#Create dummy data
data <- replicate(10, rnorm(10))
rsc <- c("green", "green", "black", "green", "green", "black", "black", "green", "green", "black")
rsc <- cbind(rsc, rsc)
colnames(rsc) <- c("Groups", "")
#Plot
heatmap.plus(data, RowSideCol = rsc)
#Legend on position (40, 2)
legend(40, 2,legend=c("Title","","Group1","Group2"), fill=c("white", "white", "green","black"), border=FALSE, bty="n", y.intersp = 0.7, cex=0.7)