I need to bold the equations in stat_poly_eq() in ggplot2. How do I do this along with atop function?
Also, is there anyway to specify the coordinates of the stat_poly_eq?
x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
geom_point() +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") +
stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) +
geom_smooth(method="lm", formula = y ~ x ) +
stat_poly_eq(
formula = my.formula,
aes(label = paste("atop(", ..eq.label.., ",", ..rr.label.., ")")),
label.y = 0.9,
parse = TRUE,
size = 2.5
#, col = "black"
)+
facet_grid(.~z, scales = "free") +
theme_classic()
I don't know if you can; you can underline the equation, e.g.
x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
geom_point() +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") +
stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) +
geom_smooth(method="lm", formula = y ~ x ) +
stat_poly_eq(
formula = x ~ y,
aes(label = paste("atop(underline(", ..eq.label.., "),", ..rr.label.., ")")),
label.y = 0.9,
parse = TRUE,
size = 2.5
#, col = "black"
)+
facet_grid(.~z, scales = "free") +
theme_classic()
However, everything I tried (e.g. bold(), bolditalic()) didn't work when combined with atop(). Perhaps it's related to this statement in the docs:
Note that bold, italic and bolditalic do not apply to symbols, and
hence not to the Greek symbols such as mu which are displayed in the
symbol font. They also do not apply to numeric constants.
Even if you "bold" everything with e.g. theme_classic(base_family = "Arial Bold"), the equation and R2 value aren't changed.
--
In terms of positioning the labels, you can move them around with label.x and label.y, e.g. (label.y = 1 puts the label at the top of the plot):
library(tidyverse)
library(ggpmisc)
x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
geom_point() +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") +
stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) +
geom_smooth(method="lm", formula = y ~ x ) +
stat_poly_eq(
formula = x ~ y,
aes(label = paste("atop(underline(", ..eq.label.., "),", ..rr.label.., ")")),
label.y = 1,
label.x = 0.2,
parse = TRUE,
size = 2.5
#, col = "black"
)+
facet_grid(.~z, scales = "free") +
theme_classic()
Related
I have four factors each with it's unique regression line and Rsquared. I want to do three things:
Place the Rsq in line with the equation of the line (currently its at the bottom)
Arrange the four equations on seperate lines
Arrange the four equations on the top, right.
set.seed(111)
var <- rep(c("SP1","SP2","AB1","AB2"), times = 5)
var.val <- rnorm(20,5,1)
level <- rep(c(100,200,300,400), each = 5)
df <- data.frame(var, var.val, level)
df <- df[order(-level),]
ggplot(df, aes(x = level, y = var.val, col = var, group = var, linetype = var)) +
geom_point(aes(fill = var), colour="white",pch=21, size=4, stroke = 1,
alpha = 0.7, data = df[df$var %in% c("SP1", "SP2"),]) +
theme_classic() +
geom_smooth(data = df[df$var %in% c("SP1", "SP2"),],
method = "lm", formula = y ~ x, alpha = 0.2) +
geom_smooth(data = df[!df$var %in% c("SP1", "SP2"),],
method = "lm", formula = y ~ x, se = FALSE) +
scale_linetype_manual(values = c("dotdash", "dashed","solid", "solid")) +
scale_colour_manual(values = c("black","black","red","blue")) +
stat_poly_eq(formula = y ~ x, aes(label = paste("atop(", ..eq.label.., ",", ..rr.label.., ")")), label.y = 0.9, parse = TRUE, size = 3)
The atop() function creates a line break between two equations. You can use list() instead.
For right top alignment, you can give values in decimals as a sequence as below. Each equation needs different values, so that there is no overlap.
ggplot(df, aes(x = level, y = var.val, col = var, group = var, linetype = var)) +
geom_point(aes(fill = var), colour="white",pch=21, size=4, stroke = 1,
alpha = 0.7, data = df[df$var %in% c("SP1", "SP2"),]) +
theme_classic() +
geom_smooth(data = df[df$var %in% c("SP1", "SP2"),],
method = "lm", formula = y ~ x, alpha = 0.2) +
geom_smooth(data = df[!df$var %in% c("SP1", "SP2"),],
method = "lm", formula = y ~ x, se = FALSE) +
scale_linetype_manual(values = c("dotdash", "dashed","solid", "solid")) +
scale_colour_manual(values = c("black","black","red","blue")) +
stat_poly_eq(formula = y ~ x, aes(label = paste("list(", ..eq.label.., ",", ..rr.label.., ")")),
label.x = 0.9,
label.y = seq(0.85, 1, by =0.05), parse = TRUE, size = 3)
The scatterplot is colour-coded by factor z. By default, ggplot2 also pots the regression lines by factor. I want to plot a single regression line passing through the data. How do I achiece this?
x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("P1", "P2"), each = 25)
df <- data.frame(x,y,z)
my.formula = y ~ x
ggplot(aes(x = x, y = y, color = z), data = df) +
geom_point() + scale_fill_manual(values=c("purple", "blue")) +
geom_smooth(method="lm", formula = y ~ x ) +
stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE, size = 2.5, col = "black")+
theme_classic()
If I undertand you correctly, you can assign group = 1 in the aes to plot just one regression line. You can use the following code:
library(tidyverse)
library(ggpmisc)
my.formula = y ~ x
ggplot(aes(x = x, y = y, color = z, group = 1), data = df) +
geom_point() + scale_fill_manual(values=c("purple", "blue")) +
geom_smooth(method="lm", formula = y ~ x ) +
stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE, size = 2.5, col = "black")+
theme_classic()
Output:
I have 3 columns in a data frame from which I want to create a visualisation with geom_smooth() :
ggplot(my_data_frame) +
aes(x = fin_enquete,
y = intentions,
colour = candidat) +
geom_point(alpha = 1/6,
shape = "circle",
size = .5L) +
geom_smooth(mapping = aes(y = erreur_inf),
size = .5L,
span = .42,
se = F) +
geom_smooth(mapping = aes(y = erreur_sup),
size = .5L,
span = .42,
se = F) +
geom_smooth(method = "loess",
size = 1.5L,
span = .42,
se = F) +
labs(x = "Date de fin d'enquĂȘte",
y = "Pourcentage d'intentions de vote") +
theme_minimal() +
theme(text = element_text(family = "DIN Pro")) +
coord_cartesian(expand = F) +
easy_remove_legend()
3 lines with geom_smooth
I would like to color the area between the upper and the lower line. I know the geom_ribbon() function but I am not sure I can use it in this situation.
Does anybody have a solution?
Have a nice day!
You could use geom_ribbon and calculate the loess model yourself within the geom_ribbon call?
Toy random data
dat <- data.frame(x=1:100, y=runif(100), y2=runif(100)+1, y3=runif(100)+2)
Now suppose we want a smoothed ribbon between y and y3, with y2 drawn as a line between them:
ggplot( dat , aes(x, y2)) +
geom_ribbon(aes(ymin=predict(loess(y~x)),
ymax=predict(loess(y3~x))), alpha=0.3) +
geom_smooth(se=F)
You could use lapply() smooth to calculate the range of df values such as (5,11,13) to calculate the smooths and plot only the two edges of the se.
Sample code:
library(ggplot2)
ggplot(data = mtcars,
mapping = aes(x = wt,
y = mpg)) +
geom_point(size = 2)+
lapply(c(5,11, 13), function (i) {
geom_smooth(
data = ~ cbind(., facet_plots = i),
method = lm,
se=F,
formula = y ~ splines::bs(x, i)
)
})+
#facet_wrap(vars(facet_plots))
geom_ribbon(
stat = "smooth",
method = "loess",
se = TRUE,
alpha = 0, # or, use fill = NA
colour = "black",
linetype = "dotted")+
theme_minimal()
Plot:
For the graph dimensions I need, I want the R squared to appear on the next line. I also want the colour of the text to correspond to the color of factorz
x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each = 5)
df <- data.frame(x,y,z)
my.formula = y ~ x
ggplot(aes(x = x, y = y, color = z), data = df) +
geom_point() +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") +
stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) +
geom_smooth(method="lm", formula = y ~ x ) +
stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE, size = 2.5, col = "black")+
facet_grid(.~z, scales = "free") + theme_classic()
Here is a way.
To have the colors right, comment out color = "black";
To have r-squared below the regression equation, use atop, see ?plotmath.
The result of atop is not left aligned, but here it is.
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
geom_point() +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") +
stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) +
geom_smooth(method="lm", formula = y ~ x ) +
stat_poly_eq(
formula = my.formula,
aes(label = paste("atop(", ..eq.label.., ",", ..rr.label.., ")")),
label.y = 0.9,
parse = TRUE,
size = 2.5
#, col = "black"
)+
facet_grid(.~z, scales = "free") +
theme_classic()
I have two plots I just want to know how I can add a legend for the blue and gray bar charts and also could you please show me how you could also edit the legend tittle.
X1 <- c(seq(7.912087912,44.83516484,1.538461538))
X2 <- c(seq(7.912087912,49.45054945,1.538461538))
dat2 <- data.frame(x = X2 , y = rnorm(28, 26, 5))
dat1 <- data.frame(x = X1 , y = rnorm(100, 25, 4))
ggplot(NULL) +
geom_bar(dat1, mapping = aes(x = x, y = y), stat = "identity",alpha = 0.3, position = "stack" ) + labs( x = " Time [ S ]", y = "Frequency") + theme_minimal() +
ggtitle("Histogram Of Time In Tank") + theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(dat2, mapping = aes(x = x, y = y ), stat = "identity", alpha = .3, position = "stack", fill='lightblue' , color='lightblue4')
+ scale_linetype_discrete(name = LegendTitle)
If you want a legend in ggplot, you need to have an aesthetic mapping inside your aes() or no legend will appear. Here's how we can set a mapping and then use the scale to set the colors we want
ggplot(NULL) +
geom_bar(dat1, mapping = aes(x = x, y = y, fill="Grey Bars"), stat = "identity",alpha = 0.3, position = "stack" ) +
labs( x = " Time [ S ]", y = "Frequency") +
theme_minimal() +
ggtitle("Histogram Of Time In Tank") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(dat2, mapping = aes(x = x, y = y, fill='Blue Bars') , stat = "identity", alpha = .3, position = "stack", color='lightblue4') +
scale_fill_manual(name="Bars", values=c("Grey Bars" = "grey35", "Blue Bars" = "lightblue"))