This is my sample data. I want to plot both y1 and y2 against x1 in a single plot. This is what I did:
library(ISLR)
library(ggplot2)
y1<-scale(Auto$horsepower,scale = T,center=T)
y2<-scale(Auto$weight,scale = T,center=T)
x1<-Auto$mpg
df<-data.frame(y1,y2,x1)
p<-ggplot(df,aes(x=x1)) +
geom_point(aes(y = y1), shape = 16) +
geom_point(aes(y = y2), shape = 2)
I want to insert a quadratic line for both y1 and y2 against x. I did this:
p + stat_smooth(method = "lm", formula = y ~ x + I(x^2), size = 1)
It throws up an error:
Warning message:
Computation failed in `stat_smooth()`:
variable lengths differ (found for 'x')
Other than this, the stat_smooth command will only put one quadratic line while I need two quadratic lines
for both y1 and y2.
How did I achieve this in R?
Thanks
You should add two stat_smooth() calls and add aes() to show which y to use.
ggplot(df,aes(x=x1)) +
geom_point(aes(y = y1), shape = 16) +
geom_point(aes(y = y2), shape = 2) +
stat_smooth(aes(y = y1),method = "lm", formula = y ~ x + I(x^2), size = 1) +
stat_smooth(aes(y = y2),method = "lm", formula = y ~ x + I(x^2), size = 1, color = "red")
Or make long format table and then you will need just one call of stat_smooth() and geom_point().
library(tidyr)
df_long <- df %>% gather(variable, value, y1:y2)
ggplot(df_long, aes(x1, value, color = variable)) +
geom_point() +
stat_smooth(method = "lm", formula = y ~ x + I(x^2), size = 1)
Related
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:
I do a Multiple Linear Regression in R, where I want to add a simple legend to a graph (ggplot). The legend should show the points and fitted lines with their corresponding colors. So far it works fine (without legend):
ggplot() +
geom_point(aes(x = training_set$R.D.Spend, y = training_set$Profit),
col = 'red') +
geom_line(aes(x = training_set$R.D.Spend, y = predict(regressor, newdata = training_set)),
col = 'blue') +
geom_line(aes(x = training_set$R.D.Spend, y = predict(regressor_sig, newdata = training_set)),
col = 'green') +
ggtitle('Multiple Linear Regression (Training set)') +
xlab('R.D.Spend [k$]') +
ylab('Profit of Venture [k$]')
How can I add a legend here most easily?
I tried the solutions from similar question, but did not succeed (add legend to ggplot2 | Add legend for multiple regression lines from different datasets to ggplot)
So, I appended my original model like this:
ggplot() +
geom_point(aes(x = training_set$R.D.Spend, y = training_set$Profit),
col = 'p1') +
geom_line(aes(x = training_set$R.D.Spend, y = predict(regressor, newdata = training_set)),
col = 'p2') +
geom_line(aes(x = training_set$R.D.Spend, y = predict(regressor_sig, newdata = training_set)),
col = 'p3') +
scale_color_manual(
name='My lines',
values=c('blue', 'orangered', 'green')) +
ggtitle('Multiple Linear Regression (Training set)') +
xlab('R.D.Spend [k$]') +
ylab('Profit of Venture [k$]')
But here I am getting the error of "Unknown colour name: p1". which makes somewhat sense, as I do not define p1 above. How can I make the ggplot recognise my intended legend?
Move col into the aes and then you can set the color using scale_color_manual:
library(ggplot2)
set.seed(1)
x <- 1:30
y <- rnorm(30) + x
fit <- lm(y ~ x)
ggplot2::ggplot(data.frame(x, y)) +
geom_point(aes(x = x, y = y)) +
geom_line(aes(x = x, y = predict(fit), col = "Regression")) +
scale_color_manual(name = "My Lines",
values = c("blue"))
ggpmisc::stat_poly_eq has an option output.type = "numeric" allowing to get the estimates of the parameters of the fitted model. Below is my attempt to use it with facet_wrap. I get a different R² per facet but the coefficients are the same in the two facets. Do I do something wrong, or is it a bug?
library(ggpmisc)
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x,
y = y,
group = c("A", "B"))
my.data[my.data$group=="A",]$y <- my.data[my.data$group=="A",]$y + 200000
formula <- y ~ poly(x, 1, raw = TRUE)
myformat <- "Intercept: %s\nSlope: %s\nR²: %s"
ggplot(my.data, aes(x, y)) +
facet_wrap(~ group) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, output.type = "numeric",
mapping = aes(label =
sprintf(myformat,
formatC(stat(coef.ls)[[1]][[1, "Estimate"]]),
formatC(stat(coef.ls)[[1]][[2, "Estimate"]]),
formatC(stat(r.squared)))))
Edit
We have to catch the panel number. It is strange that formatC(stat(as.integer(PANEL))) returns the panel number per facet:
but however formatC(stat(coef.ls)[[stat(as.integer(PANEL))]][[1, "Estimate"]]) does not work, because here PANEL = c(1,2).
Ok, I figured it out.
ggplot(my.data, aes(x, y)) +
facet_wrap(~ group) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(
formula = formula, output.type = "numeric",
mapping = aes(label =
sprintf(myformat,
c(formatC(stat(coef.ls)[[1]][[1, "Estimate"]]),
formatC(stat(coef.ls)[[2]][[1, "Estimate"]])),
c(formatC(stat(coef.ls)[[1]][[2, "Estimate"]]),
formatC(stat(coef.ls)[[2]][[2, "Estimate"]])),
formatC(stat(r.squared)))))
Version 0.3.2 of 'ggpmisc' is now in CRAN. Submitted earlier this week. In the documentation I now give some examples of the use of geom_debug() from my package 'gginnards' to have a look at the data frame returned by stats (usable with any ggplot stat or by itself). For your example, it would work like this:
library(ggpmisc)
library(gginnards)
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x,
y = y,
group = c("A", "B"))
my.data[my.data$group=="A",]$y <- my.data[my.data$group=="A",]$y + 200000
formula <- y ~ poly(x, 1, raw = TRUE)
myformat <- "Intercept: %s\nSlope: %s\nR²: %s"
ggplot(my.data, aes(x, y)) +
facet_wrap(~ group) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, output.type = "numeric",
aes(label = ""),
geom = "debug")
Which prints to the console, two tibbles, one for each panel:
Example below added to address comment:
ggplot(my.data, aes(x, y)) +
facet_wrap(~ group) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, output.type = "numeric",
aes(label = ""),
summary.fun = function(x) {x[["coef.ls"]][[1]]})
prints just the coefs.ls.
I added the "numeric" option recently in response to a suggestion and with this example I noticed a bug: aes(label = "") should not have been needed, but is needed because the default mapping for the label aesthetic is wrong. I will fix this for the next release.
I have a data set (dat), with raw data (raw_x and raw_y). I have predicted a model and the predictions from the model are stored in dat$predict.
I wish to plot the raw data but overlay the data with a geom_smooth (here a quadratic function) but using the predicted data. This is my attempt at the basic code. I am not sure how to use predicted values in the geom_smooth yet.
ggplot(dat, aes(x = raw_x, y = raw_y, colours = "red")) +
geom_point() +
theme_bw() +
geom_smooth(method = "lm", formula = y ~ x + I(x^2))
The following plots the original points, the linear fit line and the fitted points. I use made up data since you have posted none.
set.seed(1234)
x <- cumsum(rnorm(100))
y <- x + x^2 + rnorm(100, sd = 50)
dat <- data.frame(raw_x = x, raw_y = y)
fit <- lm(y ~ x + I(x^2), dat)
dat$predict <- predict(fit)
ggplot(dat, aes(x = raw_x, y = raw_y)) +
geom_point(colour = "blue") +
theme_bw() +
geom_smooth(method = "lm", formula = y ~ x + I(x^2), colour = "red") +
geom_point(aes(y = predict), colour = "black")
I plotted a time series data on ggplot with Year on the x axis and rain on the y axis.
I would like to overlay a trend line on this plot ( my equation for this trend line is rain = 2.6*Year + 23). My slope was computed using the theil sen method
How can I overlay this on my plot
My code thus far is
ggplot(data = Datarain, aes(x = year, y = rain)) +
geom_smooth(color="red", formula = y ~ x) +
geom_smooth(method = "lm", se=FALSE color="blue", formula = y ~ x) +
geom_line() + scale_x_continuous("Year")
I am not sure how to add my own equation on my plot or how to add a thiel sen line in ggplot
Any ideas would be grateful
You can use geom_abline to specify your linear equation
ggplot(data = Datarain, aes(x = year, y = rain)) +
geom_smooth(color="red", formula = y ~ x) +
geom_smooth(method = "lm", se=FALSE color="blue", formula = y ~ x) +
geom_line() + scale_x_continuous("Year") +
geom_abline(intercept = 23, slope = 2.6)