changing the color palette in ggplot - r

I have created ggplot from my data (sample below):
I have created a violin plot of the NKV with the individual NKV data points plotted over it. I want to differentiate betweeen which PID my datapoints belong to. So far so good:
violin.murgang <- ggplot(nkv.murgang, aes(x = factor("Murgang"), nkv.murgang$NK)) +
geom_violin(color = "black", fill = "darkorange") +
ggtitle("NKV Murgang - Einfamilienhaus") +
labs(x = "Prozess", y = "Nutzen / Konsten \n Verhälhniss") +
stat_summary(geom = "text", fun.y = quantile,
aes(label=sprintf("%1.1f", ..y..)),
position=position_nudge(x=0.4), size=3) +
theme (legend.position = "none") +
stat_summary(fun.data = give.n, geom = "text", position=position_nudge(x=-0.4)) +
geom_jitter(aes(col = PID ), width = 0.35)
violin.murgang
The problem is that all the NKV data points are only visualized in different shade of blue. I would like to have different colours. I have tried adding this:
scale_colour_brewer(palette="Spectral")
which yields the error:
Error: Continuous value supplied to discrete scale
How can i achieve having different colour for the geom_jitter part?
What causes the error?
Thanks!

If you PID have more levels than colors of 'Spectral' palette, you could try scale_color_distiller, which extends brewer colors to continuous scale, see the manual of scale_color_distiller:
# Use distiller variant with continous data
v <- ggplot(faithfuld) +
geom_tile(aes(waiting, eruptions, fill = density))
v
v + scale_fill_distiller()
v + scale_fill_distiller(palette = "Spectral")
Therefore, we could try:
ggplot(nkv.murgang, aes(x = factor("Murgang"), nkv.murgang$NK)) +
geom_violin(color = "black", fill = "darkorange") +
ggtitle("NKV Murgang - Einfamilienhaus") +
labs(x = "Prozess", y = "Nutzen / Konsten \n Verhälhniss") +
stat_summary(geom = "text", fun.y = quantile,
aes(label=sprintf("%1.1f", ..y..)),
position=position_nudge(x=0.4), size=3) +
theme (legend.position = "none") +
geom_jitter(aes(color = PID), width = 0.35) +
scale_color_distiller(palette = "Spectral")
If you data has a few levels, we could use discrete scales. PID is integer, which does work with discrete scales. You should convert it to character or factor first:
ggplot(nkv.murgang, aes(x = factor("Murgang"), nkv.murgang$NK)) +
geom_violin(color = "black", fill = "darkorange") +
ggtitle("NKV Murgang - Einfamilienhaus") +
labs(x = "Prozess", y = "Nutzen / Konsten \n Verhälhniss") +
stat_summary(geom = "text", fun.y = quantile,
aes(label=sprintf("%1.1f", ..y..)),
position=position_nudge(x=0.4), size=3) +
theme (legend.position = "none") +
geom_jitter(aes(color = as.factor(PID) ), width = 0.35) +
scale_color_brewer(palette = "Spectral")

Related

Merge legend in ggplot when the geoms are different

I have the following code which yields the figure below:
ggplot(data=data.frame(x=x, y=y, mass=mass)) +
geom_line(mapping = aes(x=x, y=y, linetype='Gompertz predicted mass', col='Gompertz predicted mass')) +
geom_point(mapping = aes(x=x, y=mass, shape='Actual mass',col='Actual mass')) +
theme_bw() +
ylab('Mass') +
xlab('t') +
scale_color_manual(name='',values = c("black",'red')) +
scale_linetype_manual(name='',values = c("solid")) +
scale_shape_manual(name='', values = c(19)) +
scale_x_continuous(breaks=seq(4,26,2)) +
ylim(c(0, 20000)) +
ggtitle('Problem 3: Plot of tumor mass with time')
Notice how the legend is separated. I'd like to merge it for shape and color. When the geoms are the same, the technique of using scale_something_manual works perfectly fine to merge the legends. However, I'm having trouble with it here since I have two different geoms.
The problem is similar to the one described in https://github.com/tidyverse/ggplot2/issues/3648. There is no elegant solution at the moment. Because you haven't included any data, I've presumed that your problem is conceptually similar to the plot below:
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(shape = "Point", colour = "Point")) +
geom_smooth(aes(linetype = "Line", colour = "Line"),
formula = y ~ x, se = FALSE, method = "loess") +
scale_colour_manual(values = c("red", "black")) +
scale_linetype_manual(values = "solid") +
scale_shape_manual(values = 19)
The way to fix the problem is to get rid of the linetype and shape aesthetics and scales, and instead override aesthetics at the level of the legend.
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = "Point")) +
geom_smooth(aes(colour = "Line"),
formula = y ~ x, se = FALSE, method = "loess") +
scale_colour_manual(
values = c("red", "black"),
guide = guide_legend(override.aes = list(shape = c(NA, 19),
linetype = c(1, NA)))
)
Created on 2021-09-04 by the reprex package (v2.0.1)

ggplot legend / label change with various guides

I am trying to change the labels of a legend on a ggplot where I have legends for 2 aes.
With scale_file_manual, it works, but for one of the two legends only. I guess I should use "guides" that I already used to remove the titles and also remove legend for a 3rd aes, but I do not manage to do it.
Do you have a solution?
Here is my code :
p <- ggplot(data, aes(x = Nb))
p + geom_ribbon(aes(ymin = Sandwich.min, ymax = Sandwich.max, fill = 'grey70',alpha=0.8)) +
geom_ribbon(aes(ymin = Assiette.min, ymax = Assiette.max, fill = '#6495ED80',alpha=0.8)) +
geom_line(aes(y = Pizza, col = '#FF7F24')) +
geom_line(aes(y = Sushis, col = '#228B22')) +
labs(title = "Business lunch cost by number of participants",
x = "Number of participants",
y = "Price (Euros)") +
scale_x_continuous(breaks=seq(1,20,1)) +
scale_y_continuous(breaks = seq(0,300,50)) +
theme_light() +
theme(plot.title = element_text(size = 12, hjust = 0.5)) +
guides(alpha = FALSE, colour = guide_legend(" "), fill = guide_legend(" ")) +
scale_fill_manual(
values=c('#6495ED80','grey70'),
labels=c("Assiettes","Sandwiches"))

Removing points from geom_bar legend ggplot r

This is my data.
Mod <- as.factor(c(rep("GLM",5),rep("MLP",5),rep("RF",5),rep("SVML",5),rep("SVMR",5)))
Manifold <- as.factor(rep(c("LLE","Iso","PCA","MDS","kPCA"),5))
ROC <- runif(25,0,1)
Sens <- runif(25,0,1)
Spec <- runif(25,0,1)
df <- data.frame("Mod"= Mod, "Manifold"= Manifold, "ROC" = ROC, "Sens" = sens, "Spec" = spec)
And I am making this graph
resul3 <- ggplot(df, aes(x = Mod, y = ROC, fill= Manifold)) +
geom_bar(stat = "identity", position = "dodge", color = "black") +
ylab("ROC & Specificity") +
xlab("Classifiers") +
theme_bw() +
ggtitle("Classifiers' ROC per Feature Extraction Plasma") +
geom_point(aes(y=Spec), color = "black", position=position_dodge(.9)) +
scale_fill_manual(name = "Feature \nExtraction", values = c("#FFEFCA",
"#EDA16A" ,"#C83741", "#6C283D", "#62BF94"))
first graph
And what I want is another legend with tittle "Specificity" and a single black point. I dont want the point to be inside the Manifolds legend.
Something like this but without the points inside the manifold squares
Changing the geom_point line, adding a scale_color_manual and using the override as seen in #drmariod's answer will result in this plot:
ggplot(df, aes(x = Mod, y = ROC, fill= Manifold)) +
geom_bar(stat = "identity", position = "dodge", color = "black") +
ylab("ROC & Specificity") +
xlab("Classifiers") +
theme_bw() +
ggtitle("Classifiers' ROC per Feature Extraction Plasma") +
geom_point(aes(y=Spec, color = "Specificity"), position=position_dodge(.9)) +
scale_fill_manual(name = "Feature \nExtraction", values = c("#FFEFCA",
"#EDA16A" ,"#C83741", "#6C283D", "#62BF94")) +
scale_color_manual(name = NULL, values = c("Specificity" = "black")) +
guides(fill = guide_legend(override.aes = list(shape = NA)))
You can overwrite the aesthetics for shape and set it to NA like this
ggplot(df, aes(x = Mod, y = ROC, fill= Manifold)) +
geom_bar(stat = "identity", position = "dodge", color = "black") +
ylab("ROC & Specificity") +
xlab("Classifiers") +
theme_bw() +
ggtitle("Classifiers' ROC per Feature Extraction Plasma") +
geom_point(aes(y=Spec), color = "black", position=position_dodge(.9)) +
scale_fill_manual(name = "Feature \nExtraction", values = c("#FFEFCA",
"#EDA16A" ,"#C83741", "#6C283D", "#62BF94")) +
guides(fill = guide_legend(override.aes = list(shape = NA)))

Adding Quadrants to R Scatterplots, and lines pointing from plots to their respective labels

With ggplot2, can I add four quadrants. Basically just a line down the middle for the y and x axis (a plus sign)? I also wanted to see if i can have a lines pointing from the labels to their respective dots. The labeling overlap is getting problematic.
data$Goal <- factor(data$Goal, levels = c("KPI",
"Strategic Support and Relationship Management",
"Sales and Marketing Support",
"Google AW Account Management and Product Support"))
library(ggplot2)
ggplot(data = data, aes(x = X, y = Y, color = Goal)) +
geom_point(size=3) +
scale_colour_manual(values = c("blue", "red4", "purple4", "olivedrab")) +
geom_text(aes(label = Label), color = "black",
position = "jitter", hjust=0.6, vjust=1.1, size = 2.5) +
labs(title = "PSAT", x = "Impact on Overall Satisfaction", y = "Higher Rating")
Just doing the same thing (the quadrants part) and have used:
p<-ggplot(survey.df, aes(x=specificity, y=success)) +
geom_point() +
lims(x=c(1,10),y=c(1,10)) +
theme_minimal() +
coord_fixed() +
geom_vline(xintercept = 5) + geom_hline(yintercept = 5)
p
Giving
for the lines pointing from the labels to their respective dots:
ggplot(data = data, aes(x = X, y = Y, color = Goal)) +
geom_point(size=3) +
scale_colour_manual(values = c("blue", "red4", "purple4", "olivedrab")) +
geom_text_repel(aes(label = Label), color = "black", size = 2.5) +
labs(title = "PSAT", x = "Impact on Overall Satisfaction", y = "Higher Rating")

to show mean value in ggplot box plot

I need to be able to show the mean value in ggplot box plot. Below works for a point but I need the white dashed lines? Any body help?
x
Team Value
A 10
B 5
C 29
D 35
ggplot(aes(x = Team , y = Value), data = x)
+ geom_boxplot (aes(fill=Team), alpha=.25, width=0.5, position = position_dodge(width = .9))
+ stat_summary(fun.y=mean, colour="red", geom="point")
Here's my way of adding mean to boxplots:
ggplot(RQA, aes(x = Type, y = engagementPercent)) +
geom_boxplot(aes(fill = Type),alpha = .6,size = 1) +
scale_fill_brewer(palette = "Set2") +
stat_summary(fun.y = "mean", geom = "text", label="----", size= 10, color= "white") +
ggtitle("Participation distribution by type") +
theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank())
ggplot(df, aes(x = Type, y = scorepercent)) +
geom_boxplot(aes(fill = Type),alpha = .6,size = 1) +
scale_fill_brewer(palette = "Set2") +
stat_summary(fun.y = "mean", geom = "point", shape= 23, size= 3, fill= "white") +
ggtitle("score distribution by type") +
theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank())
I would caution against using text to this and do geom_line instead as text is offset slightly and gives the wrong portrayal of the mean.
Hey user1471980, I think people are more inclined to help if you have a unique user name but then again you have a lot of points :)
this is a hack but does this help:
Value<-c(1,2,3,4,5,6)
Team<-c("a","a","a","b","b","b")
x<-data.frame(Team,Value) #note means for a=2, mean for b=5
ggplot(aes(x = Team , y = Value), data = x) + geom_boxplot (aes(fill=Team), alpha=.25, width=0.5, position = position_dodge(width = .9)) +
annotate(geom="text", x=1, y=2, label="----", colour="white", size=7, fontface="bold", angle=0) +
annotate(geom="text", x=2, y=5, label="----", colour="white", size=7, fontface="bold", angle=0)

Resources