Draw a Trend Line Using Ggplot

Adding two separate trend lines for specific time period in ggplot

You can use geom_segment:

geom_segment(aes(x=..., y=..., xend=..., yend=...))

You just have to replace with the values you want your line to start and stop.

Edit: with your data, that will do something like

ggplot(sns1, aes(x = date, y = metric_value)) +
geom_line(size = 1, color = "#E41A1C") +
geom_segment(aes(x= 11/14/2020, y=20.6, xend=12/26/2020, yend=21.5))+
geom_segment(...)

With the same idea for the second line.

Plot trend lines and equation for multiple variable in ggplot

This sort of problem is usually a data reformating problem. See reshaping data.frame from wide to long format.

And with the data as posted there is no need to coerce columns SA, SA1 and SA2 to numeric, they were not posted as factors.

library(dplyr)
library(tidyr)
library(ggplot2)

test %>%
mutate(SA1 = SA1/100) %>%
gather(sa, value, -year) %>%
ggplot(aes(x = year, y = value, group = sa, colour = sa)) +
geom_line() +
geom_smooth(method = "lm", formula = y ~ x) +
scale_color_manual(values = c("black", "red", "blue")) +
scale_y_continuous(name = " Primary axis", sec.axis = sec_axis(~.*100, name = "Secondary axis"))

Sample Image

Is it possible to draw multiple trendlines within a single time series graph in ggplot2?

You can simply repeat your geom_smooth call with subsets of the original data frame:

ggplot(data, aes(x = Year, y = SPM)) +
geom_line(color = 'black', size = 1.3) +
geom_point(color = "blue", size = 1.3) +
stat_smooth(method = "lm", formula = y ~ x, size = 0.75, se = TRUE,
color = "blue", fill = "#9AE5D7") +
stat_smooth(method = "lm", formula = y ~ x, size = 0.75, se = TRUE,
color = "red", fill = "red", alpha = 0.2,
data = data[data$Year < as.Date("2009-06-01"),]) +
stat_smooth(method = "lm", formula = y ~ x, size = 0.75, se = TRUE,
color = "forestgreen", fill = "forestgreen", alpha = 0.2,
data = data[data$Year >= as.Date("2009-06-01"),]) +
stat_poly_eq(face = "bold", parse = TRUE, aes(label = ..eq.label..),
formula = y ~ x, label.x.npc = 0.5, label.y.npc = 0.1,
size = 6, coef.digits = 4) +
stat_fit_glance(method = 'lm', method.args = list(formula = y ~ x),
geom = 'text',
aes(label = paste(
"P-value = ", signif(..p.value.., digits = 4), sep = ""
)), size = 6, label.x = "left", label.y = "top") +
scale_x_date(date_labels = "%Y", date_breaks = "1 year") +
labs(x = "Time (Years)", y = "Concentration") +
theme_bw() +
theme(plot.title = element_text(size = 17, face = "bold"),
axis.title.x = element_text(size = 20, face = "bold"),
axis.title.y = element_text(size = 20, face = "bold"),
axis.text.x = element_text(size = 18, face = "bold"),
axis.text.y = element_text(size = 18, face = "bold"),
strip.text.x = element_text(size = 16, face = "bold"),
strip.text.y = element_text(size = 16, face = "bold"),
axis.line.x = element_line(color = "black", size = 1),
axis.line.y = element_line(color = "black", size = 1),
axis.ticks = element_line(color = "black", size = 1.2),
axis.ticks.length = unit(0.2, "cm"),
panel.border = element_rect(fill = NA, size = 1),
legend.title = element_blank(),
legend.position = c(.8, .2))

Sample Image

In this case, the overall trend is fairly constant, so the background blue line is obscured by the two partial segments.

Adding a simple lm trend line to a ggplot boxplot

The error message is pretty much self-explanatory: Add aes(group=1) to geom_smooth:

ggplot(iris, aes(factor(Sepal.Length), Sepal.Width)) +
geom_boxplot() +
geom_smooth(method = "lm", se=FALSE, color="black", aes(group=1))

Sample Image



Related Topics



Leave a reply



Submit