R: default palette used for pie charts - r

I would like to add a legend to a pie chart in R
The default color palette is fine for me, but how can I use it in the legend (to match the colors of the plot)?
pie(table(iris$Species))
legend('right',c('A','B','C'), fill = colors(3))
(I know that I could create a vector of colors to use in the chart in the legend in advance like in my_colors <- colors(3), but this would then not be the default colors.
The official documentation only says that the pie() function uses "pastel" colors if the col argument is omitted, but doesn't specify the exact values.)

If you look at the body of the pie function, you will see that the colors are hard-coded into it in the following lines:
if (is.null(col))
col <- if (is.null(density))
c("white", "lightblue", "mistyrose",
"lightcyan", "lavender", "cornsilk")
else par("fg")
So, you can use the same colors yourself in the call to legend
pie(table(iris$Species))
legend('right', c('A','B','C'), fill = c("white", "lightblue", "mistyrose"))

Related

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))

change the default color of a ggplot2 object

I am trying to make a plot using a custom package fishtaco. Please download it here:
download package
install.packages("<path to downloaded package.tar.gz", repos = NULL,type="source")
library(FishTacoPlot)
p <- MultiFunctionTaxaContributionPlots(input_dir="<path_to_package>/examples", input_prefix="HMP_fishtaco",
input_taxa_taxonomy="<path_to_package>/examples/HMP_TAXONOMY.tab", sort_by="list", plot_type="bars",
input_function_filter_list=c("ko00020", "ko00540","ko02040"), add_predicted_da_markers=TRUE, add_original_da_markers=TRUE)
This gives a plot as follows:
As visible from the plot it is very hard to distinguish between the shades of blue. Is there a way to modify the ggplot2 object p generated with the code above? I tried to add a manual color scale:
p + scale_color_manual(values = c("red", "black", "dodgerblue2", "antiquewhite", "aquamarine", so on for as many legend
variables))
But that did not change the default underlying color scheme. I can't change the package, but can I change the ggplot2 color?

Single legend when using group, linetype and colour in ggplot2?

I am creating a very simple plot that groups data and uses the grouping variable to determine linestyle and colour. I then override those using 'scale_linetype_manaul' and 'scale_colour_manual'. So far so good, but when I try to modify legend labels or its title, the legend splits into two parts: one for linetype and one for colour. I just want one legend, but with the custom labels and title.
Following this question, I made sure to name both scale objects the same, but that doesn't appear to help.
Minimal example:
X <- data.frame(TPP=factor(c(1,5,10,1,5,10,1,5,10)),
value=c(-0.035819, 0.003356, 0.066091, -0.028039, 0.004333, 0.060292, -0.023115, 0.005661, 0.058821),
horizon=c(1,1,1,2,2,2,3,3,3))
ggplot(X, aes(x=horizon, y=value, group=TPP, col=TPP, linetype=TPP))+
geom_line(size=1)+
scale_linetype_manual(name="X", values = c("solid","dashed", "dotted")) +
scale_color_manual(name="X", values = c("black", "red", "blue"), labels=c("Low", "5","High"))
This yields the following figure with two legends. How can I recombine those legends again, with custom labels and a title?
This might help:
ggplot(X, aes(x=horizon, y=value, group=TPP, col=TPP, linetype=TPP))+geom_line(size=1)+
scale_linetype_manual(name="X", values = c("solid","dashed", "dotted"),labels=c("Low", "5","High")) +
scale_color_manual(name ="X", values = c("black", "red", "blue"),labels=c("Low", "5","High"))
If the labels defined in scale_color_manual and in scale_linetype_manual are different, or if they are specified in only one of them, you will obtain two different legends.

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.

How to change the boundary color of boxplot?

I'd like to create boxplot like this:
This plot is created by bwplot in lattice package. Instead using this function, I hope to use boxplot to plot similar thing.
I notice in boxplot we could only change the color of the box body, how could I change the boundary color of the box by boxplot function?
Thanks!
Look at ?boxplot to find that there's an argument border= that does what you want. For example:
boxplot(count ~ spray, data = InsectSprays, col = "lightgray",
border=c("blue", "green", "blue", "green", "blue", "green"))

Resources