Smooth line to the full datase (faceting) ggplot2 - r

I want to add smooth line of the full dataset to each facet.
However, the following code add smooth line different(of each facet) smooth line to each facet.
ggplot(mpg2,aes(displ,hwy)) + geom_point() + facet_wrap(~class) + geom_smooth(se = FALSE)
How can I fix that?

You can add the geom_smooth layer using a dataset that doesn't contain the faceting variable. So remove class from the dataset.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class) +
geom_smooth(data = mpg[,1:10], se = FALSE)

Related

Why does geom_jitter replicates black points when we use aes colour?

I came across this issue while analyzing my data and I was able to replicate it with the example of the official ggplot reference.
This code creates black points that seem to be the original points before jitter was applied with collors:
ggplot(mpg, aes(cyl, hwy)) +
geom_point() +
geom_jitter(aes(colour = class))
However, this code works fine, it doesn't show the black points:
p <- ggplot(mpg, aes(cyl, hwy))
p + geom_point()
p + geom_jitter(aes(colour = class))
I was thinking it may be related to geom_point printing the black dots before geom_jitter, but if this is the case, why does it work fine in the second example, which follows the same order?
This is the image of the black points
geom_jitter is merely a convenience function, it is calling geom_point under the hood. Because of this, your use of p + geom_point() + geom_jitter(aes(color=class)) is actually the same as
ggplot(ggplot2::mpg, aes(cyl, hwy)) +
geom_point() +
geom_point(aes(color = class), position = "jitter")
which is plotting the same points twice. You can clarify this a little by changing the color of the original points:
ggplot(ggplot2::mpg, aes(cyl, hwy)) +
geom_point(color = "red") +
geom_jitter(aes(color = class))
If you want jittered points, use either geom_point(position = "jitter") or geom_jitter(), not both.

ggplot2: smooth and fill

I'd like to smooth the geom_lines and fill the area between. I've tried stat_smooth() to smooth the lines, and both geom_ribbon() and geom_polygon() but without success.
Apologies for the double barrel question.
bell <- data.frame(
month = c("Launch","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th"),
rate = c(0,.05,.12,.18,.34,.42,.57,.68,.75,.81,.83,.85,.87))
bell$month <- factor(bell$month, levels = rev(c("Launch","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th")))
ggplot() +
theme_minimal() +
coord_flip() +
scale_fill_manual(values=cols) +
geom_line(data=bell, aes(x=month, y=.5-(rate/2), group=1), color='pink', size=1) +
geom_line(data=bell, aes(x=month, y=.5+(rate/2), group=1), color='pink', size=1) +
theme(legend.position='none', axis.ticks=element_blank(), axis.text.x=element_blank(),axis.title.x=element_blank())
One option is to calculate the points of the loess regression outside of ggplot and then plot them using geom_line (for a line) or geom_area for a filled area (geom_area is geom_ribbon, but with ymin fixed at zero).
Also, you don't need coord_flip. Instead, just switch your x and y mappings. This is necessary anyway if you want to fill underneath the curve.
In the example below I've created a numeric month variable for the regression. I've also commented out the scale_fill_manual line because your example doesn't provide a cols vector and the plot code doesn't produce a legend anyway. I've also commented out the legend.position='none' line as it's superfluous.
bell$month.num = 0:12
m1 = loess(rate ~ month.num, data=bell)
bell$loess.mod = predict(m1)
ggplot(bell, aes(y=month, group=1)) +
theme_minimal() +
#scale_fill_manual(values=cols) +
geom_area(aes(x=.5-(loess.mod/2)), fill='pink', size=1) +
geom_area(aes(x=.5+(loess.mod/2)), fill='pink', size=1) +
theme(#legend.position='none',
axis.ticks=element_blank(),
axis.text.x=element_blank(),
axis.title.x=element_blank())

ggplot2, multiple lines plot(from grammar point of view)

I have 2 codes:
The first code makes plot with one lm line:
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = class)) +
geom_smooth(se = FALSE)
The second codemakes plot with n lm lines:
ggplot(mpg, aes(displ, hwy,colour = class)) +
geom_point() +
geom_smooth(se = FALSE)
from the grammar point of view, can you please explain how the colour = class(or more precisely the place in code line, where this input is situated) influences on the plot(one line or multiple lines)?
Many thanks.

Plotting only stat_smooth without original ggplot2 data

I plot data with ggplot, and I wanted to see the smoothed lines using stat_smooth.
But now I would like to only plot the smoothed lines (somehow extract it), without the original ggplot.
Do you think it's possible?
Here is my code :
Graph <- ggplot(data=Forecasttemp, aes(x=Price.date, y=Price, colour=Group)) + geom_line() + scale_colour_hue(guide = "none")
Graph <- Graph + stat_smooth(se = FALSE, aes(fill = Group)) + scale_colour_hue(guide = "none")
If you want to plot only the smoothed lines without original sample points, you can simply omit geom_line(), thus resulting in:
Graph <- ggplot(data=Forecasttemp, aes(x=Price.date, y=Price, colour=Group)) +
stat_smooth(se = FALSE, aes(fill = Group)) +
scale_colour_hue(guide = "none")
Unfortunately I can not try this due to the lack of a reproducible example, but I make a try with an R base dataset and it worked:
library(ggplot2)
data(iris)
g1 <- ggplot(data=iris, aes(x=Sepal.Length, y=Petal.Length, colour=Species)) +
scale_colour_hue(guide = "none") + geom_smooth()
g1

ggplot. Adding regression lines by group

If I plot this
dodge <- position_dodge(.35)
ggplot(mediat, aes(x=t, y=Value, colour=factor(act),group=id )) +
geom_point(position=dodge) + geom_errorbar(aes(ymin=Value-sdt, ymax=Value+sdt),
width=0, position=dodge) + theme_bw() + geom_smooth(method="lm",se=FALSE,
fullrange=TRUE)
I get this
As you can see the regression line is not plotted.
with +stat_smooth(method=lm, fullrange=TRUE, se = FALSE) the result is the same.
I've found that removing the "group=id" I can get the regression lines but
then
ggplot(mediat, aes(x=t, y=Value, colour=factor(act) ))+ geom_point(position=dodge) +
geom_errorbar(aes(ymin=Value-sdt, ymax=Value+sdt), width=0, position=dodge) +
theme_bw() + geom_smooth(method="lm",se=FALSE, fullrange=TRUE)
As you can see, now it plot the lines but I loose the dodge function by groups.
How can I get both things at once?. I mean, regression lines by "id" on the first uncluttered plot?
Any other solution with base plot, lattice or any other common package would also be welcome.
Regards

Resources