I have a data frame dt.Data
with time data (values of this data frame are changing each day) and I'm plotting an correlation scatter plot and the regression line with ggplot()
. The R code looks like this:
set.seed(123)
## Create data frame: ##
df.Data <- data.frame(date = seq(as.Date('2018-01-01'), by = '1 day', length.out = 1100),
DE = rnorm(1100, 2, 1), AT = rnorm(1100, 5, 2))
corPearson <- cor.test(x = df.Data$DE, y = df.Data$AT, method = "pearson")
df.Data$year <- format(as.Date(df.Data$date), '%Y')
p <- ggplot(data = df.Data, aes(x = DE, y = AT, group = 1)
) +
geom_point(aes(color = year)) +
geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
theme_classic() +
theme(legend.position = "none") +
theme(panel.background = element_blank()) +
scale_colour_brewer(palette = 'Greens') +
xlab(product1) +
ylab(product2) +
ggtitle("Correlation Scatter Plot (Pearson)") +
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
# Correlation plot converting from ggplot to plotly: #
CorrelationPlot <- plotly::ggplotly(p, tooltip = "text")
The regression line is plotted with: geom_smooth(method = "lm", se = FALSE, color = "#007d3c")
.
The plot looks like this:
My question now is: How do I get the function of the regression line in the form f(x) = kx + d? I have already seen this question a few times in stackoverflow, but no answer there was complete or useful. Can someone help me?
EDIT: If I use this
reg <- lm(df.Data$AT ~ df.Data$DE)
summary(reg)
the output of the summary is:
where the d=5.07667
(red) and k=-0.03306
(blue)? Is this correct?
How can I extract both values and construct a function like this: f(x)=kx+d=-0.3303x+5.07667
??
I need this f(x)
as an output of a valueBox()
in a RShiny app.
You can use the lm()
function:
reg <- lm (df.Data$AT ~ df.Data$DE)
summary (reg)
When you summarize you can see the intercept, which is your d and the slop which is you k. Let me know if this helps :)