multiple colors on beanplot in R - 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')

Related

Plot a curve with different color for each point in R

I have a curve, for instance
y_curve=c(1,2,5,6,9,1).
and the colors for each curve point
colors=c("#0000FF","#606060","#606060","#FF0000","#FF0000","#FF0000").
In theory I want to plot a curve where the first half has one color (except for the first point which is blue) and the second half has another color. In my example the dataset has more than 3000 observations so it makes sense.
For some reason, if I plot the data just using the command
plot(y_curve,col=colors), the color of points is plotted corrently.
Nevertheless, if I add the option type="l", the plotted curve has only one color - the blue, which is the first color in the vector colors ("#0000FF").
Does anyone know what am I doing wrong?
So the code is
y_curve=c(1,2,5,6,9,1)
colors=c("#0000FF","#606060","#606060","#FF0000","#FF0000","#FF0000")
plot(y_curve,col=colors,type="l")
Thank you all in advance.
I avoid using ggplot since this part of code is inside an already complicated function and I prefer using the base R commands.
The line option for the plot function does not accept multiple colors.
There is the segments() function that we can use to manually draw in each separate segment individually with a unique color.
y_curve=c(1,2,5,6,9,1)
colors=c("#0000FF","#606060","#606060","#FF0000","#FF0000","#FF0000")
#create a mostly blank plot
plot(y_curve,col=NA)
# Use this to show the points:
#plot(y_curve,col=colors)
#index variable
x = seq_along(y_curve)
#draw the segments
segments(head(x,-1), head(y_curve,-1), x[-1], y_curve[-1], type="l", col=colors)
This answer is based on the solution to this question:
How do I plot a graph in R, with the first values in one colour and the next values in another colour?

ggplot2: How to make a legend only have 2 variables when it automatically makes 6

I need to graph data in ggplot2 for a homework assignment, and while the data is fine, I want the legend for the plot to only have 2 options instead of one for every boxplot. Each of the boxes represents a plot of trees (the x-value) that is either evergreen or deciduous according to the color. However, the fill, which I set to be Forest Type for the legend, made a legend for every single plot when I only wanted two symbols in the legend: the gray45-colred box for the 3 evergreen plots and gray69-colored box for the three deciduous plots. In short, how can I make the plot only have 2 symbols to match the 2 colors?
ggplot(data=dbh.full, aes(x=Plot, y=D_2017,fill=Plot))+geom_boxplot()+
labs(x='Plot ID', y='Tree DBH', fill='Forest Type')+
scale_fill_manual(values=c('gray45','gray45','gray45','gray69','gray69','gray69'))
The professor for this class suggested to use substr() on the dataframe plot labels, so I would prefer to have the problem solved that way, but I am open to simple solutions as well. I placed a link below to a picture of the graph because I am not high enough of a level to post images.
You can create a binary variable and use it as the variable for fill. There many ways to do it, in the example below I detected the levels of Plot that had E to make Evergreen, otherwise Deciduous
library(tidyverse)
dbh %>%
mutate(forest_type = if_else(str_detect(Plot,"E"),"Evergreen","Deciduous")) %>%
ggplot(data=dbh.full, aes(x=Plot, y=D_2017,fill=forest_type))+
geom_boxplot()+
labs(x='Plot ID', y='Tree DBH', fill='Forest Type')+
scale_fill_manual(values=c('gray45','gray69'))

change siberDensityPlot colors by group

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.

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.

Parallel Co-ordinates Plot in R

I'd like to plot a parallel co-ordinate plot for a dataset mtcars. I want to set a variable on color. I used the code :
library(GGgally)
ggparcoord(data=mtcars, columns=1:10 , groupColumn=11)
It generated the graph but all the lines are in shades of blue. However I have trouble comprehending the graph and making observations due to similar colors used. How can I introduce a different set of colors like blue, green and red etx for the same variable.
You can use the ggparcoord()'s coloring function for this by turning the grouping-column into a factor.
mtcars[,11] <- as.factor(mtcars[,11])
ggparcoord(data=mtcars, columns=1:10 , groupColumn=11)

Resources