Add "p = " in front of geom_boxplot p-value in ggplot2 - r

P-values can be added to ggplot2 figures using the function ggpubr::stat_compare_mean(). However I cannot get the text "p = " to show up in front of the p-values. There are examples of how to add "p = " in front of p-values on the help page for the function but they do not seem to work.
Example
library(ggplot2)
library(ggpubr)
library(dplyr)
data("Cars93")
# List of the comparisons I would like to make for which p-values will be derived
my_comparisons <- list(c("Front", "Rear"),
c("Front", "4WD"),
c("Rear", "4WD"))
# creates the figure with p-value but no label indicating the values are p-values
Cars93 %>%
mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
ggplot(aes(x = DriveTrain, y = Price)) +
stat_compare_means(paired = F,
comparisons = my_comparisons) +
geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0) +
geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2) +
theme_bw() +
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
following the example at the bottom of the ?stat_compare_means page suggests using aes(label = paste0("p = ", ..p.format..) which does not work.
?stat_compare_means
Cars93 %>%
mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
ggplot(aes(x = DriveTrain, y = Price)) +
stat_compare_means(paired = F,
comparisons = my_comparisons,
aes(label = paste0("p = ", ..p.format..))) +
geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0) +
geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2) +
theme_bw() +
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
If you look at the label argument on the ?stat_compare_means help page it says the allowed values include "p.signif" or "p.format" which made me think ..p.format.. was deprecated, so I tried adding in "p.format" which also did not work.
Cars93 %>%
mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
ggplot(aes(x = DriveTrain, y = Price)) +
stat_compare_means(paired = F,
comparisons = my_comparisons,
aes(label = paste0("p = ", "p.format"))) +
geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0) +
geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2) +
theme_bw() +
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
In the end I would like the p-values to be preceded by p = such that the labels would say p = 0.00031, p = 0.059, and p = 0.027.

When you use a list of comparisons, stat_compare_means defaults to using geom_signif from the ggsignif package, essentially acting as a glorified wrapper function. In so doing, you lose some of the formatting flexibility. Better in this case to use geom_signif directly:
library(ggsignif)
Cars93 %>%
mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
ggplot(aes(x = DriveTrain, y = Price)) +
geom_signif(y_position = c(55, 60, 65),
comparisons = my_comparisons,
map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
geom_boxplot(outlier.colour="white", outlier.fill = "white",
outlier.shape = 1, outlier.size = 0) +
geom_jitter(shape=1, position=position_jitter(0.2),
color = "black", fill = "white", size = 2) +
theme_bw() +
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())

Related

How to display p-values above boxplots on exponential (log10) y-axis?

I have a data frame with three groups (group1, group2, group3). I would like to show the p-value of their mean comparisons in ggplot2 which I can do however, the values are stacked ontop of one another making it difficult to see what is being compared. When I try to adjust where the p-values are located using the y_position() function, the boxplots collapse (I think because the y-axis is log10) but the p-values are no longer stacked ontop of one another. How can I keep the boxplots from collapsing and keep the p-values displayed so that you can see what is being compared?
Example data
library(ggplot2)
library(dplyr)
library(ggsignif)
df <- data.frame(matrix(ncol = 2, nrow = 30))
colnames(df)[1:2] <- c("group", "value")
df$group <- rep(c("group1","group2","group3"), each = 10)
df[1:10,2] <- rexp(10, 1/10)
df[11:20,2] <- rexp(10, 1/100)
df[21:30,2] <- rexp(10, 1/900)
# Need to say what should be compared for p-value determination
my_comparisons <- list(c("group1", "group2"),
c("group1", "group3"),
c("group2", "group3"))
Boxplots showing the distribution of value for each group however the p-values are ontop of one another so you cannot compare among groups.
df %>%
mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
ggplot(aes(x = group, y = value)) +
geom_signif(comparisons = my_comparisons,
map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
scale_y_log10() +
geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0) +
geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2) +
labs(x = "",
y = "value") +
theme_bw() +
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
Adjusting the y_position() of where the p-values should display but this collapses the y-axis. I have tried several values within y_position.
df %>%
mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
ggplot(aes(x = group, y = value)) +
geom_signif(y_position = c(2000,1800,1600),
comparisons = my_comparisons,
map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
scale_y_log10() +
geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0) +
geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2) +
labs(x = "",
y = "value") +
theme_bw() +
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
For some reason this parameter ignores the axis transformation. You therefore need to use the log10 values of the desired positions:
df %>%
mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
ggplot(aes(x = group, y = value)) +
geom_signif(comparisons = my_comparisons,
y_position = log10(c(5000, 10000, 25000)),
map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
scale_y_log10() +
geom_boxplot(outlier.colour="white", outlier.fill = "white",
-outlier.shape = 1, outlier.size = 0) +
geom_jitter(shape=1, position=position_jitter(0.2), color = "black",
fill = "white", size = 2) +
labs(x = "",
y = "value") +
theme_bw() +
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())

In R, how to add p-values for multiple regression models with various adjustments to ggplot2 scatter plot? [duplicate]

I did linear regression analysis between the response variable(y) and
predictor variables in the surgical data set considering pindex as a confounding variable.
I aim to plot the response variable(y) against the experimentally determined values of the predictor variables and to this end, I am Successful. However, could not able to indicate the estimated regression and p-value in the ggplot2.
In the code below, trying to do the analysis and the plot.
It would be much appreciated if someone could show me how to indicate the estimated regression and p-values inside the ggplot2.
library(olsrr)
library(tidyverse)
library(reshape2)
data(surgical)
## Regression
pre1 <-setdiff(names(surgical), c("y", "pindex"))
mod_dres<-NULL
for (j in pre1) {
model <- lm(y ~ pindex + get(j), data = surgical)
bmodel <- broom::tidy(model)
bmodel$term[3]<-j
bmodel<-bmodel[3,]
mod_dres<-rbind(mod_dres,bmodel)
}
mod_dres
## Matching the significant variables with the orginal data and reshaping
pre1.plot = melt(surgical[,-c(2)], id.vars='y') %>%
dplyr::filter(variable %in% mod_dres$term)
## plot the predictors varaibles
ggplot(pre1.plot) +
geom_jitter(aes(value,y, colour=variable),
colour="darkorange", size = 3) +
geom_smooth(aes(value,y, colour=variable),
method=lm, se=FALSE, colour="darkorange") +
theme_minimal() +
theme(axis.title = element_text(size = 16,face="bold", colour = "black"),
axis.text = element_text(size = 16,face="bold", colour = "black"),
axis.line = element_line(colour='black'),
axis.ticks = element_line(colour='black'),
plot.title = element_text(hjust = 0.5,size=18,face="bold"),
legend.position = "bottom",
legend.title = element_text(color = "Black", size = 9, face = "bold"),
legend.text=element_text(color = "Black", size = 9, face = "bold"),
strip.text.x = element_text(size = 16,face="bold", colour = "black")) +
labs(x = "value",title = " ") +
facet_wrap(~variable, scales="free_x",nrow = 2, ncol = 4) +
theme(panel.background = element_rect(fill = "white", colour = "black"),
strip.background = element_rect(fill = "white", colour = "black"),
legend.key = element_blank(),
legend.title = element_blank())
Thank you!
You can add the equation and p-value to the plot using the "stat_poly_eq" function from the ggpmisc package:
library(tidyverse)
library(olsrr)
library(reshape2)
library(ggpmisc)
data(surgical)
## Regression
pre1 <-setdiff(names(surgical), c("y", "pindex"))
mod_dres<-NULL
for (j in pre1) {
model <- lm(y ~ pindex + get(j), data = surgical)
bmodel <- broom::tidy(model)
bmodel$term[3]<-j
bmodel<-bmodel[3,]
mod_dres<-rbind(mod_dres,bmodel)
}
mod_dres
## Matching the significant variables with the orginal data and reshaping
pre1.plot = melt(surgical[,-c(2)], id.vars='y') %>%
dplyr::filter(variable %in% mod_dres$term)
## plot the predictors varaibles
ggplot(pre1.plot) +
geom_jitter(aes(value, y, colour=variable),
colour="darkorange", size = 3) +
geom_smooth(aes(value, y, colour=variable),
formula = y ~ x, method="lm", se=FALSE,
colour="darkorange") +
stat_poly_eq(formula = y ~ x,
aes(x = value, y = y, label = paste(..eq.label..,
..p.value.label..,
sep = "~~~~")),
parse = TRUE) +
theme_minimal() +
theme(axis.title = element_text(size = 16,face="bold", colour = "black"),
axis.text = element_text(size = 16,face="bold", colour = "black"),
axis.line = element_line(colour='black'),
axis.ticks = element_line(colour='black'),
plot.title = element_text(hjust = 0.5,size=18,face="bold"),
legend.position = "bottom",
legend.title = element_text(color = "Black", size = 9, face = "bold"),
legend.text=element_text(color = "Black", size = 9, face = "bold"),
strip.text.x = element_text(size = 16,face="bold", colour = "black")) +
labs(x = "value",title = " ") +
facet_wrap(~variable, scales="free_x",nrow = 2, ncol = 4) +
theme(panel.background = element_rect(fill = "white", colour = "black"),
strip.background = element_rect(fill = "white", colour = "black"),
legend.key = element_blank(),
legend.title = element_blank())
Edit
And you can add other statistics, like the R^2 value:
ggplot(pre1.plot) +
geom_jitter(aes(value, y, colour=variable),
colour="darkorange", size = 3) +
geom_smooth(aes(value, y, colour=variable),
formula = y ~ x, method="lm", se=FALSE,
colour="darkorange") +
stat_poly_eq(formula = y ~ x,
aes(x = value, y = y, label = paste(..rr.label..,
..p.value.label..,
sep = "~~~~")),
parse = TRUE) +
theme_minimal() +
theme(axis.title = element_text(size = 16,face="bold", colour = "black"),
axis.text = element_text(size = 16,face="bold", colour = "black"),
axis.line = element_line(colour='black'),
axis.ticks = element_line(colour='black'),
plot.title = element_text(hjust = 0.5,size=18,face="bold"),
legend.position = "bottom",
legend.title = element_text(color = "Black", size = 9, face = "bold"),
legend.text=element_text(color = "Black", size = 9, face = "bold"),
strip.text.x = element_text(size = 16,face="bold", colour = "black")) +
labs(x = "value",title = " ") +
facet_wrap(~variable, scales="free_x",nrow = 2, ncol = 4) +
theme(panel.background = element_rect(fill = "white", colour = "black"),
strip.background = element_rect(fill = "white", colour = "black"),
legend.key = element_blank(),
legend.title = element_blank())
See the docs for other statistics that can be included, e.g. "adj.rr.label", "f.value.label" etc: https://rdrr.io/cran/ggpmisc/man/stat_poly_eq.html

including regression coefficient and pvalue in the ggplot2

I did linear regression analysis between the response variable(y) and
predictor variables in the surgical data set considering pindex as a confounding variable.
I aim to plot the response variable(y) against the experimentally determined values of the predictor variables and to this end, I am Successful. However, could not able to indicate the estimated regression and p-value in the ggplot2.
In the code below, trying to do the analysis and the plot.
It would be much appreciated if someone could show me how to indicate the estimated regression and p-values inside the ggplot2.
library(olsrr)
library(tidyverse)
library(reshape2)
data(surgical)
## Regression
pre1 <-setdiff(names(surgical), c("y", "pindex"))
mod_dres<-NULL
for (j in pre1) {
model <- lm(y ~ pindex + get(j), data = surgical)
bmodel <- broom::tidy(model)
bmodel$term[3]<-j
bmodel<-bmodel[3,]
mod_dres<-rbind(mod_dres,bmodel)
}
mod_dres
## Matching the significant variables with the orginal data and reshaping
pre1.plot = melt(surgical[,-c(2)], id.vars='y') %>%
dplyr::filter(variable %in% mod_dres$term)
## plot the predictors varaibles
ggplot(pre1.plot) +
geom_jitter(aes(value,y, colour=variable),
colour="darkorange", size = 3) +
geom_smooth(aes(value,y, colour=variable),
method=lm, se=FALSE, colour="darkorange") +
theme_minimal() +
theme(axis.title = element_text(size = 16,face="bold", colour = "black"),
axis.text = element_text(size = 16,face="bold", colour = "black"),
axis.line = element_line(colour='black'),
axis.ticks = element_line(colour='black'),
plot.title = element_text(hjust = 0.5,size=18,face="bold"),
legend.position = "bottom",
legend.title = element_text(color = "Black", size = 9, face = "bold"),
legend.text=element_text(color = "Black", size = 9, face = "bold"),
strip.text.x = element_text(size = 16,face="bold", colour = "black")) +
labs(x = "value",title = " ") +
facet_wrap(~variable, scales="free_x",nrow = 2, ncol = 4) +
theme(panel.background = element_rect(fill = "white", colour = "black"),
strip.background = element_rect(fill = "white", colour = "black"),
legend.key = element_blank(),
legend.title = element_blank())
Thank you!
You can add the equation and p-value to the plot using the "stat_poly_eq" function from the ggpmisc package:
library(tidyverse)
library(olsrr)
library(reshape2)
library(ggpmisc)
data(surgical)
## Regression
pre1 <-setdiff(names(surgical), c("y", "pindex"))
mod_dres<-NULL
for (j in pre1) {
model <- lm(y ~ pindex + get(j), data = surgical)
bmodel <- broom::tidy(model)
bmodel$term[3]<-j
bmodel<-bmodel[3,]
mod_dres<-rbind(mod_dres,bmodel)
}
mod_dres
## Matching the significant variables with the orginal data and reshaping
pre1.plot = melt(surgical[,-c(2)], id.vars='y') %>%
dplyr::filter(variable %in% mod_dres$term)
## plot the predictors varaibles
ggplot(pre1.plot) +
geom_jitter(aes(value, y, colour=variable),
colour="darkorange", size = 3) +
geom_smooth(aes(value, y, colour=variable),
formula = y ~ x, method="lm", se=FALSE,
colour="darkorange") +
stat_poly_eq(formula = y ~ x,
aes(x = value, y = y, label = paste(..eq.label..,
..p.value.label..,
sep = "~~~~")),
parse = TRUE) +
theme_minimal() +
theme(axis.title = element_text(size = 16,face="bold", colour = "black"),
axis.text = element_text(size = 16,face="bold", colour = "black"),
axis.line = element_line(colour='black'),
axis.ticks = element_line(colour='black'),
plot.title = element_text(hjust = 0.5,size=18,face="bold"),
legend.position = "bottom",
legend.title = element_text(color = "Black", size = 9, face = "bold"),
legend.text=element_text(color = "Black", size = 9, face = "bold"),
strip.text.x = element_text(size = 16,face="bold", colour = "black")) +
labs(x = "value",title = " ") +
facet_wrap(~variable, scales="free_x",nrow = 2, ncol = 4) +
theme(panel.background = element_rect(fill = "white", colour = "black"),
strip.background = element_rect(fill = "white", colour = "black"),
legend.key = element_blank(),
legend.title = element_blank())
Edit
And you can add other statistics, like the R^2 value:
ggplot(pre1.plot) +
geom_jitter(aes(value, y, colour=variable),
colour="darkorange", size = 3) +
geom_smooth(aes(value, y, colour=variable),
formula = y ~ x, method="lm", se=FALSE,
colour="darkorange") +
stat_poly_eq(formula = y ~ x,
aes(x = value, y = y, label = paste(..rr.label..,
..p.value.label..,
sep = "~~~~")),
parse = TRUE) +
theme_minimal() +
theme(axis.title = element_text(size = 16,face="bold", colour = "black"),
axis.text = element_text(size = 16,face="bold", colour = "black"),
axis.line = element_line(colour='black'),
axis.ticks = element_line(colour='black'),
plot.title = element_text(hjust = 0.5,size=18,face="bold"),
legend.position = "bottom",
legend.title = element_text(color = "Black", size = 9, face = "bold"),
legend.text=element_text(color = "Black", size = 9, face = "bold"),
strip.text.x = element_text(size = 16,face="bold", colour = "black")) +
labs(x = "value",title = " ") +
facet_wrap(~variable, scales="free_x",nrow = 2, ncol = 4) +
theme(panel.background = element_rect(fill = "white", colour = "black"),
strip.background = element_rect(fill = "white", colour = "black"),
legend.key = element_blank(),
legend.title = element_blank())
See the docs for other statistics that can be included, e.g. "adj.rr.label", "f.value.label" etc: https://rdrr.io/cran/ggpmisc/man/stat_poly_eq.html

How to hide a single key in R?

Just to be clear: I am relatively new to R, and the code I am using is borrowed from someone else.
I have this graph for polling averages:
Here is my code: https://pastebin.com/qvQERRUH
library("tidyverse")
polls <- read.csv("polls_Paris.csv")
polls <- polls %>%
mutate(
date = format(as.Date(c(paste(year,month, day, sep="-")), by = "days"))
)
for(i in c("LFI", "PS", "EELV", "PP", "Griveaux", "LREM", "Villani", "Agir", "LR", "RN", "LP")) {
polls <- within(polls, {
assign(paste0("ci_", i), 1.96 * sqrt(( get(paste0("liste_", i)) * (100 - get(paste0("liste_", i)))) / n))
}
)
}
polls.10m <- polls[polls$date > seq(as.Date(Sys.Date()), length = 2, by = "-10 months")[2],]
polls.100 <- polls[order(as.Date(polls$date)),] %>% top_n(5000, as.Date(polls$date))
#Results = data.frame(date = as.Date("2019-12-01"), support = c(69.1,30.9))
svg('Opinion polling for the 2020 Paris municipal election.svg', width = 12, height = 6)
polls.100 %>%
gather(party, support, c(liste_LFI,liste_PS,liste_EELV,liste_PP,liste_Griveaux,liste_LREM,liste_Villani,liste_Agir,liste_LR,liste_RN,liste_LP), factor_key=TRUE) %>%
ggplot(aes(x=as.Date(date), y=support, colour=party)) +
geom_point(size=2.5, alpha=0.275) +
geom_smooth(se=FALSE, method="loess", span=1) +
labs(y = NULL,
x = NULL) +
guides(colour = guide_legend(ncol = 1, override.aes = list(linetype = 0, size = 3, alpha = 1))) +
scale_colour_manual(labels = c("Simonnet (LFI)", "Hidalgo (PS-PCF-G·s)", "Belliard (EELV)", "Gantzer (DVG)", "Griveaux (LREM-MR-UDI)", "Griveaux (avant diss. de Villani)", "Villani (Diss. LREM-PRG)", "Bournazel (Agir)", "Dati (LR)", "Federbusch (DVD-RN)", "Campion (SE)"), values = c("#cc2443", "#FF8080", "#00c000", "#ffc0c0", "#ffeb00", "#ffeb00", "#FF7F50", "#adc1fd", "#0066CC", "#0D378A", "#808080", "#808080")) +
theme(
plot.margin = margin(t = 0, unit = "cm"),
plot.background = element_blank(), panel.background = element_rect(fill = "grey92", colour = NA),
panel.border = element_blank(), legend.background = element_rect(fill = "transparent", colour = NA),
legend.key = element_rect(fill = "transparent", colour = NA), legend.title = element_blank(),
strip.background = element_rect(fill = "transparent", colour = NA),
panel.grid.major = element_line(colour = "#FFFFFF"), panel.grid.minor = element_line(colour = "#FFFFFF", size = 0.25),
axis.ticks = element_line(colour = "grey20"), axis.line = element_blank(),
plot.title = element_text(size = 12, hjust = 0),
plot.subtitle = element_text(size = 12, hjust = 0),
plot.caption = element_text(size = 12, colour = "#212121"),
axis.title = element_text(size = 12, face = "plain"), axis.text = element_text(size = 12, face = "plain", colour = "grey30"),
legend.position = "right",
legend.text = element_text(size = 12), strip.text = element_text(size = 12, face = "plain"),
legend.margin = margin(t = 0, unit = "cm"),
) +
scale_y_continuous(breaks = seq(0,33,5), minor_breaks = seq(0,33,1), limits = c(0, 33), expand = c(0, 0)) +
scale_x_date(breaks="6 months", minor_breaks="1 month", expand = c(0, 0))
#geom_point(data = Results, colour = c("#808080", "#E81B23"), size=4, shape=5) +
#geom_point(data = Results, colour = c("#808080", "#E81B23"), size=3.5, shape=18)
dev.off()
As you can see, Griveaux's line is split to separate the before-and-after of Villani's dissident candidacy; it's actually 2 separate lines (also separate in the dataset). Griveaux's name therefore has to appears twice.
How do I do to remove the key of a single set (remove the key for both the dots and regression line)?
Here is a hack. To remove a legend key, remove it from the breaks argument to scale_*_manual or equivalent but you must keep the same number of values as there are unique values in the color/fill aesthetic.
This is better shown with an example. I will use built-in data set iris.
To remove the legend key relative to "versicolor",
levels(df1$Species)
#[1] "setosa" "versicolor" "virginica"
just don't include it in the breaks.
library(ggplot2)
df1 <- iris[3:5]
ggplot(df1, aes(Petal.Length, Petal.Width, color = Species)) +
geom_point() +
geom_smooth(se = FALSE, method = "loess", span = 1) +
scale_color_manual(breaks = c("setosa", "virginica"),
values = c("red", "green", "blue"))

R ggplot2 scale_shape_manual not working but scale_colour_manual works

I would like to set the color and the shape of my 2 indicators which has been plotted in in two layes. The scale_color_manual works however the scale_shape_manual is not working. By having or not having this line "scale_shape_manual"; the result is the same and shape "16" (filled circle) is picked up?
comp_graph_1 <- ggplot() +
layer( mapping = aes(x=log(FV), y= msd, colour = "Reference"), #factor(Dataset)
data = ref,
stat = "identity",
geom = "point",
position = "identity")+
layer(mapping = aes(x=log(FV), y= msd, colour = "Target"), # "red" "blue"
data = target, #data = target[Is_Phone == 0],
stat = "identity",
geom = "point",
position = "identity")+
theme(panel.background = element_rect(fill = 'white'),
panel.grid = element_line(colour = "grey90") , panel.ontop = FALSE)+
theme(legend.justification = c(0, 0), legend.position = "bottom",
legend.background = element_rect(), legend.title = element_blank(), legend.key = element_rect(fill = "white"),
legend.text = element_text(size = 9,colour = "#7F7F7F"), panel.border = element_blank(),
axis.line = element_line(color = "#7F7F7F"))+
theme(plot.title = element_text(size = 16, colour = "#7F7F7F"),
axis.title.x = element_text(size = 11, hjust = 1, face = "bold", colour = "#7F7F7F"),
axis.title.y = element_text(size = 11, hjust = 1, face = "bold", colour = "#7F7F7F")) +
ggtitle(paste0(x, " / ", y, " distribution ")) + xlab(paste0("log ", x)) + ylab(y) +
scale_color_manual(values = c("Reference" ="#FFC000","Target" = "#00AEEF")) +
scale_shape_manual(values = c("Reference" =17, "Target" = 4))
I think where you have colour = "Target" you need a shape statement as well
shape = "Target" and shape = "Reference" and it should work.

Resources