change siberDensityPlot colors by group - r

I am using the SIBER package to develop a plot with three groups, I would like to color each group according to a legend. The SIBER pdf says to use the clr command, which states that
"a matrix of colours to use for shading each of the box regions. Defaults to greyscale grDevices::gray((9:1)/10) replicated for as many columns as there are in dat. When specified by the user, rows contain the colours of each of the confidence regions specified in probs and columns represent ecah of the columns of data in dat. In this way, one could have shades of blue, red and yellow for each of the groups."
However, if you run the code on a help website (https://rdrr.io/cran/SIBER/man/siberDensityPlot.html) to get a "colorful" plot instead of the greyscale plot, with the matrix of colors as specified:
# A basic default greyscale density plot
Y <- matrix(stats::rnorm(1000), 250, 4)
siberDensityPlot(Y)
# A more colourful example
my_clrs <- matrix(c("lightblue", "blue", "darkblue",
"red1", "red3", "red4",
"yellow1", "yellow3", "yellow4",
"turquoise", "turquoise3", "turquoise4"), nrow = 3, ncol = 4)
siberDensityPlot(Y, clr = my_clrs)
The result is NOT one boxplot with three shades of blue representing each CI, then another with red shades, yellow shades, etc...you create a plot with 3 shades of blue for each CI repeated across all of the different "groups" or boxplots in the panel.
My question is: does anyone know how to make the graphic so that you get shades of color in the boxplot representing different CIs, then a different color with shades for CIs for another group? The figure attached will hopefully further explain what I am trying to accomplish.

It looks like some type of bug. I see that the link you posted produced the all blue plot, but when I run the code on my PC I get this plot. I'm running Windows 10 R version 4.0.2 with the latest SIBER package from CRAN. The link is running linux and R 3.4.4 it seems.

Related

Phylogenetic tree tip color in v.phylomaker r package

I am using V.phylomaker R package for generating the phylogenetic tree of my data. I successfully ran the code and generated the circular phylogeny plot (CSV file used and figure generated is attached). I tried to give three colours for tip labels to my figure based on the three categories (Invasive, Naturalised and Casual) that I have in my data using the following r function
my_colours <- c("red", "blue", "green")
libarary(V.phylomaker)
plot(tree_random, cex=.6, type = "fan", label.offset =.1, no.margin = TRUE, tip.color = my_colours[as.factor(data$stage)])
However, using the above function by default colours are attributed in alphabetical order to species in each category and due to this some species which should get red colour in the figure get blue or green colour and vice versa and which happens mainly to genus with two or three species. Simply, I want to colour invasive species with red, naturalised with blue and casual plant species with green colour.

R heatmap function - can you add more colours so that differences within large regions are more obvious?

I am plotting a heatmap in R using the base R heatmap() function. Is there a way to define more colours so that the heatmap has a greater variation in the colours used. Currently it is using about 10 and the "hottest" area is quite large and dark purple. I want more colours so that this large area itself it broken down into more colours to better differentiate.
Try experimenting with the color palettes of the grDevices package.
library(grDevices)
heatmap(x, col = topo.colors(n))
where n is the number of colors.
Or, alternatively
col = rainbow(n)
col = terrain.colors(n)
col = cm.colors(n)
However, often the problem with differentiation does not depend on the number of colors, but on the data variability: many of them may be clustered in a small range of values. In such case you could try to differentiate them by chosing a subrange or transforming the data, for example by graphing their logaritm.
Examples:
50 colors from cm.colors palette:
heatmap(Ca, col=cm.colors(50), Rowv=NA, Colv=NA)
matrix of log values, with 50 colors from cm.colors palette:
heatmap(log(Ca), col=cm.colors(50), Rowv=NA, Colv=NA)
in which subtler differences can be seen.

Issues with colour in plots [duplicate]

I am making a scatter plot of two variables and would like to colour the points by a factor variable. Here is some reproducible code:
data <- iris
plot(data$Sepal.Length, data$Sepal.Width, col=data$Species)
This is all well and good but how do I know what factor has been coloured what colour??
data<-iris
plot(data$Sepal.Length, data$Sepal.Width, col=data$Species)
legend(7,4.3,unique(data$Species),col=1:length(data$Species),pch=1)
should do it for you. But I prefer ggplot2 and would suggest that for better graphics in R.
The command palette tells you the colours and their order when col = somefactor. It can also be used to set the colours as well.
palette()
[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" "gray"
In order to see that in your graph you could use a legend.
legend('topright', legend = levels(iris$Species), col = 1:3, cex = 0.8, pch = 1)
You'll notice that I only specified the new colours with 3 numbers. This will work like using a factor. I could have used the factor originally used to colour the points as well. This would make everything logically flow together... but I just wanted to show you can use a variety of things.
You could also be specific about the colours. Try ?rainbow for starters and go from there. You can specify your own or have R do it for you. As long as you use the same method for each you're OK.
Like Maiasaura, I prefer ggplot2. The transparent reference manual is one of the reasons.
However, this is one quick way to get it done.
require(ggplot2)
data(diamonds)
qplot(carat, price, data = diamonds, colour = color)
# example taken from Hadley's ggplot2 book
And cause someone famous said, plot related posts are not complete without the plot, here's the result:
Here's a couple of references:
qplot.R example,
note basically this uses the same diamond dataset I use, but crops the data before to get better performance.
http://ggplot2.org/book/
the manual: http://docs.ggplot2.org/current/
There are two ways that I know of to color plot points by factor and then also have a corresponding legend automatically generated. I'll give examples of both:
Using ggplot2 (generally easier)
Using R's built in plotting functionality in combination with the colorRampPallete function (trickier, but many people prefer/need R's built-in plotting facilities)
For both examples, I will use the ggplot2 diamonds dataset. We'll be using the numeric columns diamond$carat and diamond$price, and the factor/categorical column diamond$color. You can load the dataset with the following code if you have ggplot2 installed:
library(ggplot2)
data(diamonds)
Using ggplot2 and qplot
It's a one liner. Key item here is to give qplot the factor you want to color by as the color argument. qplot will make a legend for you by default.
qplot(
x = carat,
y = price,
data = diamonds,
color = diamonds$color # color by factor color (I know, confusing)
)
Your output should look like this:
Using R's built in plot functionality
Using R's built in plot functionality to get a plot colored by a factor and an associated legend is a 4-step process, and it's a little more technical than using ggplot2.
First, we will make a colorRampPallete function. colorRampPallete() returns a new function that will generate a list of colors. In the snippet below, calling color_pallet_function(5) would return a list of 5 colors on a scale from red to orange to blue:
color_pallete_function <- colorRampPalette(
colors = c("red", "orange", "blue"),
space = "Lab" # Option used when colors do not represent a quantitative scale
)
Second, we need to make a list of colors, with exactly one color per diamond color. This is the mapping we will use both to assign colors to individual plot points, and to create our legend.
num_colors <- nlevels(diamonds$color)
diamond_color_colors <- color_pallet_function(num_colors)
Third, we create our plot. This is done just like any other plot you've likely done, except we refer to the list of colors we made as our col argument. As long as we always use this same list, our mapping between colors and diamond$colors will be consistent across our R script.
plot(
x = diamonds$carat,
y = diamonds$price,
xlab = "Carat",
ylab = "Price",
pch = 20, # solid dots increase the readability of this data plot
col = diamond_color_colors[diamonds$color]
)
Fourth and finally, we add our legend so that someone reading our graph can clearly see the mapping between the plot point colors and the actual diamond colors.
legend(
x ="topleft",
legend = paste("Color", levels(diamonds$color)), # for readability of legend
col = diamond_color_colors,
pch = 19, # same as pch=20, just smaller
cex = .7 # scale the legend to look attractively sized
)
Your output should look like this:
Nifty, right?
The col argument in the plot function assign colors automatically to a vector of integers. If you convert iris$Species to numeric, notice you have a vector of 1,2 and 3s So you can apply this as:
plot(iris$Sepal.Length, iris$Sepal.Width, col=as.numeric(iris$Species))
Suppose you want red, blue and green instead of the default colors, then you can simply adjust it:
plot(iris$Sepal.Length, iris$Sepal.Width, col=c('red', 'blue', 'green')[as.numeric(iris$Species)])
You can probably see how to further modify the code above to get any unique combination of colors.
The lattice library is another good option. Here I've added a legend on the right side and jittered the points because some of them overlapped.
xyplot(Sepal.Width ~ Sepal.Length, group=Species, data=iris,
auto.key=list(space="right"),
jitter.x=TRUE, jitter.y=TRUE)

Heatmap distortion in R

I managed to generate the heatmap in R using heatmap function
( heatmap(heatmap_16m, col=redgreen(75))
to get the following:
As you see, it has a normal distribution of red, black and green colors.
Since heatmap function cannot provide any legend, I switched to heatmap.2 function (heatmap.2(heatmap_16m, col= redgreen(75), trace="none")) and got the following:
Here the color distribution is skewed to mainly red.
So, my question is following: how to get the apperance (legend, row and column dendrogram order) as in second heatmap with the distribution of greens and reds as in first heatmap?
I found the answer accidentally while searching for something else :)
Here it goes:
heatmap.2(heatmap_16m, col= greenred(75), trace="none",
scale="row")
You can also scale by column, depending on the data.

multiple colors on beanplot in R

I have created a bean plot in R using the following
beanplot(windA, side='both', border='NA',
col=list('gray',c('red','white')),
ylab='Wind Speed (m/s)' ,what=c(1,1,1,0),xaxt ='n')
axis(1,at=c(1:12),labels =c ('Jan','Feb','Mar','apr','may','Jun','Jul','Aug','Sep','Oct','Nov','Dec'))
legend('topright', fill=c('gray','red'), legend= c('Measured', 'calc'))
and I get the following image
Is there a way that I can alternate the colors? For example, can I get Jan to be gray and red then Feb to be gray and blue and continue this alternating color scheme for the year?
you could specify the color order you want, col=list('gray','red','grey','blue'), using a sample dataset USArrests from base R, the colors are cycled till all the points are plotted
require(beanplot)
beanplot(USJudgeRatings, side='both', border='NA',
col=list('gray','red','grey','blue'),
ylab='US Judge Ratings' ,what=c(1,1,1,0),xaxt ='n')

Resources