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"))
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))
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))
Related Topics
Create Line Graph with Ggplot2, Using Time Periods as X-Variable
Ordered Factors in Ggplot2 Bar Chart
Marginal Effects of Mlogit in R
Integrate a Very Peaked Function
Collapse a Data.Frame into a Vector
Updating a Subset of a Dataframe
How to Convert Time Stamp String "2014-07-20T05:11:49.988Z" into Posixt in R
What's the Easiest Way to Deploy an API Incorporating R Functions
Change All Columns from Factor to Numeric in R
Order of Dates Is Not Chronological in Ggplot2
Tukeys Post-Hoc on Ggplot Boxplot
How to Create Group Indices for Nested Groups in R
Quantmod Omitting Tickers in Getsymbols
In R, How to Find the Optimal Variable to Maximize or Minimize Correlation Between Several Datasets