I'm trying to plot two regression on the same plot. So where the first regression ends, the second should continue and since the second regression line has a different slope it produces a kink.
I'm new to ggplot and I'm getting the error: "Error: Aesthetics must either be length one, or the same length as the dataProblems:gdpless15"
Here is a link to the R file and the dataset needed for this. dataset and code
I'm essentially doing a ols regression on the variable gdp on hap. The variable gdp is subsetted into two parts < 15000 and > 15000. Then I want to plot the regression line of < 15000 and when it gets to >15000 it will plot the regression line from the second regression on the same plot.
I think I'm having most trouble with using ggplot2 to plot the two regressions together.
This is the version of my code I feel is closest to getting what I want.
library(foreign)
library(ggplot2)
gdpless15 <- isspmacro$gdp[(isspmacro$gdp < 15000) ] <- subset(isspmacro,gdpless15)
gdpless15 <-gdp < 15000
gdpmore15 <- gdp > 15000
combinedreg <- ggplot() +
geom_point(data=less15, aes(x=gdpless15, y=hap)) +
geom_smooth(data=less15, aes(x=gdpless, y=hap), fill="blue",colour="darkblue", size=1) +
geom_point(data=more15, aes(x=gdpmore15, y=hap)) +
geom_smooth(data=more15, fill="red",colour="red", size=1)
combinedreg <- ggplot() +
geom_point(data=less15, aes(x=gdpless15, y=hap)) +
geom_smooth(data=less15, aes(x=gdpless, y=hap), fill="blue",colour="darkblue", size=1) +
geom_point(data=more15, aes(x=gdpmore15, y=hap))+
geom_smooth(method=lm) + geom_point(shape=1)
plot(combinedreg)