PCA change colours of clusters - r

I want to change the colours of my clusters but maintain the shape. habillage=iris$Species changes both colour and shape, what can to change only colours?
library("devtools")
install_github("kassambara/factoextra")
library("factoextra")
res.pca <- prcomp(iris[, -5], scale = TRUE)
fviz_pca_ind(iris.pca, geom="point", pointsize = 1, habillage=iris$Species, addEllipses=TRUE, ellipse.level=0.95)

fviz_pca() works like ggplot plots, hence, for changing shapes you can use the aesthetic mappings of ggplot, e.g., for changing shapes. Concerning colors, the documentation called by ?fviz_pca_ind tells you that you can change colors via palette.
fviz_pca_ind(res.pca, geom="point", pointsize = 1, habillage=iris$Species, addEllipses=TRUE, ellipse.level=0.95
, palette = c("green", "orange", "grey") #change colors
) +
scale_shape_manual(values=c(2, 8, 11)) #change shapes

Related

ggerrorplot: how can I change the colors on the Plot?

Is it possible to give every variable a different color and how?
I would like to keep the legend.
Please note that it is made with ggerrorplot function.
You can use a different palette in your ggerrorplot function. I will use the ToothGrowth dataset as an example:
the color palette to be used for coloring or filling by groups.
Allowed values include "grey" for grey color palettes; brewer palettes
e.g. "RdBu", "Blues", ...; or custom color palette e.g. c("blue",
"red"); and scientific journal palettes from ggsci R package, e.g.:
"npg", "aaas", "lancet", "jco", "ucscgb", "uchicago", "simpsons" and
"rickandmorty".
For example use palette="Paired" for blue color:
df<- ToothGrowth
library(ggpubr)
ggerrorplot(df, x = "dose", y = "len",
color = "supp", palette = "Paired",
error.plot = "pointrange",
position = position_dodge(0.5))
Output:
Or palette="npg":

Different line colors in forest plot output from sjPlot R package

I am preparing forest plots with regression coefficients using sjPlot package. How can I customize the line color of regression coefficients to have one color for each coefficient?
I have tried to use the argument colors = c("blue", "red", "black") within the plot_model function, but it did not work. I have also tried to use different palettes combined with scale_color_manual, but it did not work either.
Here is an example from sjPlot package:
library(sjPlot)
library(sjmisc)
data(efc)
#I used log before each predictor to have an example of confidence interval
fit <- lm(tot_sc_e ~ log(c161sex) + log(e17age) + log(c160age), data = efc)
plot_model(fit, colors = c("blue", "red", "black"))
It gives me a plot with two blue lines and one red line. No black line in the plot!
Trying to use other ways did not help:
plot_model(fit, colors = NULL)+
scale_fill_sjplot(palette = "viridis", discrete = TRUE)+
scale_color_viridis(discrete = TRUE)
It actually uses the viridis palette, but again, two purple lines and one yellow line. And it returns the following messages:
"Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale."
"Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale."
If I set colors = NULL in the code, it should not return these messages, should it?
I would appreciate any help to get one different color for each predictor.
Note: this plot will be combined with other plots with the same predictors. Therefore, I want to have them with the same color in both plots to improve the readability.
By default, the colour / fill aesthetics are mapped to whether each coefficient is positive or negative. To override this & tell sjPlot to treat each of the 3 coefficients as its own group, you can specify group.terms = c(1, 2, 3) as a parameter in plot_model:
plot_model(fit,
group.terms = c(1, 2, 3),
colors = c("blue", "red", "black"))
The function's default behaviour also sorts the coefficients alphabetically, which messes up the blue-red-black sequence. To keep the original order of coefficients (i.e. the order in fit), we can additionally specify order.terms in plot_model:
plot_model(fit,
group.terms = c(1, 2, 3),
order.terms = c(1, 2, 3),
colors = c("blue", "red", "black"))

ggplot2: different alpha values for border and filling of geom_point

I want to plot my data as a dotplot using geom_point. My datapoints are overlapping, so I want to use jitter and transparency to increase visibility. Now I would also like to add a border to every datapoint to make it even more easier for the reader to see each datapoint. However, due to the alpha, it looks like my datapoints have halos around them. That's why I would like to use alpha only for the filling and alpha=0 for the border. But so far I haven't found a solution. I could plot two geom_points one being slightly larger than the other to create a border around each point with alpha=0. But becuase I need jitter, the same datapoints won't lie on top of each other.
Does anyone has an idea how to solve this problem?
Here is my code:
ggplot(data=comp24, aes(x=spatial, y=lnfit, colour=spatial, fill=spatial, shape=spatial, backgroundColor="white", na.rm=T))+
geom_point(position=position_jitter(w=0.5), size=1.75, alpha=0.2, stroke=0.3)+
scale_colour_manual(name="spatial structure", values = c("black", "black", "black"))+
scale_fill_manual(name="spatial structure", values = c("black","black", "black"))
And some data:
spatial focal competitor lnfit
low pco pch -1.79175947
low pco pch -1.49165488
low pco pch -0.98082925
low pco pch -1.97716269
intermediate pco pch -0.84729786
intermediate pco pch -0.48379695
intermediate pco pch -0.64574494
intermediate pco pch -0.51082562
intermediate pco pch 1.43693809
high pco pch 0.89608802
high pco pch 0.04879016
high pco pch -2.20625398
high pco pch 0.31003898
high pco pch -0.01694956
Here is a detail of the graph, that shows the halo I am talking about. I guess it comes from filling and border overlapping a bit. That's why I see this draker line within the grey area. Changing the stroke value unfortunately only increases the halo effect.
I save my graphs as .tif with:
tiff('C:/_..._..._.tif', bg = "white", res = 1600, width = 115, height = 160, units = "mm", compression="lzw")
Looking forward to your suggestions.
Cheers
Anne
This should works for you :
ggplot(data=comp24, aes(x=spatial, y=lnfit, colour=spatial, fill=spatial, shape=spatial, backgroundColor="white", na.rm=T))+
geom_point(position=position_jitter(w=0.5), size=1.75, stroke=0.3)+
scale_colour_manual(name="spatial structure", values = c("black", "black", "black"))+
scale_fill_manual(name="spatial structure", values =alpha(c("black","black", "black"),0.2))
Let me know
A simpler way, available here because you want the same alpha and color for all, is to specify the fill in the geom_point command using alpha. Alternatively one could also "old-fashioned" method of specifying transparency with two additional hex codes, like this, #00000044.
ggplot(data=comp24, aes(x=spatial, y=lnfit, shape=spatial)) +
geom_point(position=position_jitter(width=0.5), size=1.75,
fill=alpha("black", 0.2), stroke=0.3)
To achieve the desired plot I will use position_nudge function.
First I create a nudge vector with a uniform probability distribution of the same length as the data points. This can be done as below
set.seed(10)
nudgeWidth = 0.5
nudgeVec <- (runif(nrow(comp24))-0.5)*nudgeWidth
Now I use the above nudge vector "nudgeVec" to get the desired plot
plot1 <- ggplot(data=comp24, aes(x=spatial, y=lnfit, backgroundColor="white", na.rm=T))
plot1 <- plot1 + geom_point(position=position_nudge(x = nudgeVec),size=10.75,aes(alpha=0.2, stroke=0.5,shape=(as.integer(comp24$spatial)+14)))
plot1 <- plot1 + scale_colour_identity()
plot1 <- plot1 + scale_shape_identity()
plot1 <- plot1 + geom_point(position = position_nudge(x = nudgeVec),size=10.75,aes(alpha=1, stroke=0.5, colour="black",shape=(as.integer(spatial)-1)))
plot1
The output looks like this

Controlling color in ggparcoord (from GGally package)

I am trying to hard-code the desired line color for a particular ggparcoord plot. For instance, when I create my ggparcoord plot below:
library(GGally)
x = data.frame(a=runif(100,0,1),b=runif(100,0,1),c=runif(100,0,1),d=runif(100,0,1))
x$cluster = "green"
x$cluster2 = factor(x$cluster)
ggparcoord(x, columns=1:4, groupColumn=5, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)")
Even though I try to specify a color of "green", I get a pink color. How can I control the color of the lines in ggparcoord? (By the way, I want all lines the same color as I specify).
You should be able to map the colour (darkgreen) to a corresponding factor level (green) by adding:
+ scale_colour_manual(values = c("green" = "darkgreen"))

How to set the color range of scale_colour_brewer() in ggplot2? (palette selected)

I am sorry I cannot offer an image because of limited reputation I have on this site...
I used the following code to generate my line chart in R:
p <- ggplot()+
geom_line(data=data, aes(x, y, color=Label))+ scale_colour_brewer(palette="Oranges")
I used the palette"Oranges" because I want to generate a series of lines with similar while different colors.
However, the color of lower/upper range is too light, so I want to set a limit for the palette to avoid whitish colors.
I know I should specify something like scale_color_gradient(low = "green", high = "red"), but how can I find the specify color with a given palette?
Many thanks!
Since you have a discrete scale, you should be able to manually create the set of colors and use scale_color_manual without too much trouble.
library(ggplot2)
theme_set(theme_bw())
fake_data = data.frame(
x = rnorm(42),
y = rnorm(42),
Label = rep(LETTERS[1:7], each = 6))
p_too_light <- ggplot()+ geom_line(data=fake_data, aes(x, y, color=Label))+
scale_colour_brewer(palette="Oranges")
p_too_light
Now use brewer.pal and http://www.datavis.ca/sasmac/brewerpal.html.
library(RColorBrewer)
my_orange = brewer.pal(n = 9, "Oranges")[3:9] #there are 9, I exluded the two lighter hues
p_better <- ggplot()+ geom_line(data=fake_data, aes(x, y, color=Label))+ scale_colour_manual(values=my_orange)
p_better
If you have more than 6 categories, you could use colorRampPalette with the boundary colors from the brewer.pal call earlier. However, now choosing the palette scheme requires more thought (maybe why ggplot2 doesn't do this automatically for discrete scales).
fake_data2 = data.frame(
x = rnorm(140),
y = rnorm(140),
Label = rep(LETTERS[1:20], each = 7))
orange_palette = colorRampPalette(c(my_orange[1], my_orange[4], my_orange[6]), space = "Lab")
my_orange2 = orange_palette(20)
p_20cat <- ggplot()+ geom_line(data=fake_data2, aes(x, y, color=Label))+
scale_colour_manual(values=my_orange2)
p_20cat
A nice direct answer to this question is to use the {shades} library. For example, to reduce the lightness of scale_color_brewer by 20%, do:
library(shades)
lightness(scale_color_brewer(palette="Oranges"),
scalefac(0.8))

Resources