Adding smoother to ggplot 2 - r

I have used the following code to create a plot in r using ggplot2:
g <- ggplot(newdata, aes(MVPAper, FMI) +
geom_smooth(method = 'lm'))
I then added the following:
p <- g + geom_point(aes(color = Age)) +
facet_grid(Age ~ .) +
stat_smooth(method = 'lm') +
theme_bw(base_family = 'Times')`
I am wanting to have a smoother for each of the four graphs I have created, using the facet grid to split the graph into four ages 8,9,12,and 15) can anyone assist with my code?

You don't need both geom_smooth() and stat_smooth(). Try this:
library(tidyverse)
df <- diamonds %>% filter(price < 10000, carat < 2.5)
g <- ggplot(df, aes(carat, price, color = cut))
g +
geom_point() +
geom_smooth(method = 'lm') +
facet_grid(cut ~ .) +
theme_bw()

Related

apply transparent background to divide plot area based on x values using ggplot

I would appreciate any help to apply the transparent background colours below to
divide into two parts the plot area based on x-values as illustrated in the plot below (vertical division).
Here are my sample data and code:
mtcars$cyl <- as.factor(mtcars$cyl)
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
geom_point() +
theme(legend.position="none")+
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
Here is the plot I would like to replicate, and the legend illustrates the change I want to implement:
Thank you in advance.
I think you want something like this. You'll have to designate groups and fill by that group in your geom_ribbon, and set your ymin and ymax as you like.
library(tidyverse)
mtcars$group <- ifelse(mtcars$wt <= 3.5, "<= 3.5", "> 3.5")
mtcars <- arrange(mtcars, wt)
mtcars$group2 <- rleid(mtcars$group)
mtcars_plot <- head(do.call(rbind, by(mtcars, mtcars$group2, rbind, NA)), -1)
mtcars_plot[,c("group2","group")] <- lapply(mtcars_plot[,c("group2","group")], na.locf)
mtcars_plot[] <- lapply(mtcars_plot, na.locf, fromLast = TRUE)
ggplot(mtcars_plot, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(aes(), method=lm, se=F, fullrange=TRUE) +
geom_ribbon(aes(ymin = mpg *.75, ymax = mpg * 1.25, fill = group), alpha = .25) +
labs(fill = "Weight Class")
Edit:
To map confidence intervals using geom_ribbon you'll have to calculate them beforehand using lm and predict.
mtmodel <- lm(mpg ~ wt, data = mtcars)
mtcars$Low <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,2]
mtcars$High <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,3]
Followed by the previous code to modify mtcars. Then plot with the calculated bounds.
ggplot(mtcars_plot, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(aes(), method=lm, se=F, fullrange=TRUE) +
geom_ribbon(aes(ymin = Low, ymax = High, fill = group), alpha = .25) +
labs(fill = "Weight Class") +
scale_fill_manual(values = c("red", "orange"), name = "fill")

Insert percentage of values in ggplot

I'm quite new in R, but I'm trying to do a facet_grid using ggplot package in R and, for better data visualization, I'd like to insert the percentage of values in each quadrant for the column groupings, like the image below:
Can be one or another, in each quadrant or in legend.
My code is this one below
ggplot(df1_final,aes(x=revenue,y=visits,col=groupings)) +
geom_jitter(alpha=I(1/2)) +
xlim(c(0,20000)) +
facet_grid(group_lvl_1_visits ~ group_lvl_1_revenue)
Could anyone help me on this?
EDIT: Both solutions helped me a lot, very good ones.
Here's one way to do it:
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class, nrow = 4) ->
p
p + geom_text(
data = setNames(as.data.frame(prop.table(table(mpg$class))),c("class", "lab")),
mapping = aes(label = scales::percent(lab)),
x = 4,
y = 40
)
or, for facet_grid:
p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p <- p + facet_grid(drv ~ cyl)
p + geom_text(
data = setNames(as.data.frame(prop.table(table(mpg$drv, mpg$cyl)), stringsAsFactors = F), c("drv","cyl","lab")),
mapping = aes(label = scales::percent(lab)),
x=4.5,
y=30
)
Here's a tidyverse + label-in-legend solution:
library(ggplot2)
library(dplyr)
library(scales)
group_by(mpg, cyl, drv) %>%
mutate(color=sprintf("%s-%s",cyl,drv)) %>%
ungroup(mpg) -> mpg
gg <- ggplot(mpg, aes(displ, cty))
gg <- gg + geom_point(aes(color=color))
gg <- gg + facet_grid(drv ~ cyl)
count(mpg, color) %>%
ungroup() %>%
mutate(pct=percent(n/sum(n)),
lab=sprintf("%s (%s)", color, pct)) -> pct_df
gg <- gg + scale_color_discrete(name="Title", labels=pct_df$lab)
gg

add titles to ggplot2 plot as titles are showed in facet_wrap plots

It's posible to add titles to a single plot, like:
library(ggplot2)
gdURL <- "http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt"
gDat <- read.delim(file = gdURL)
jDat <- droplevels(subset(gDat, continent != "Oceania"))
str(jDat)
jYear <- 2007
q <- ggplot(subset(jDat, year == jYear),
aes(x = gdpPercap, y = lifeExp)) + scale_x_log10()
q + geom_point()
q + geom_point(aes(size = sqrt(pop/pi)), pch = 21)
As titles are showed (In a grey box and integrated with the plot) in a facet_wrap plot like this??:
r <- q +
geom_point(aes(size = sqrt(pop/pi)), pch = 21, show_guide = FALSE) +
scale_size_continuous(range=c(1,40))
r <- r + facet_wrap(~ continent)
r + aes(fill = continent)
Per #jraab comment, one way to accomplish this is add a dummy column and use facet_grid:
jDat$dummy <- NA
ggplot(subset(jDat, year == jYear), aes(x = gdpPercap, y = lifeExp)) +
scale_x_log10() +
geom_point() +
facet_grid(. ~ dummy)
If you want to manipulate the title, you could use:
facet_grid(. ~ dummy, labeller=label_bquote("My New Title"))

Combined bar plot and points in ggplot2

I would like to plot a "combined" bar plot with points.
Consider to following dummy data:
library(ggplot2)
library(gridExtra)
library(dplyr)
se <- function(x){sd(x)/sqrt(length(x))}
p1 <- ggplot(mtcars, aes(y=disp, x=cyl, fill=cyl))
p1 <- p1 + geom_point() + theme_classic() + ylim(c(0,500))
my_dat <- summarise(group_by(mtcars, cyl), my_mean=mean(disp),my_se=se(disp))
p2 <- ggplot(my_dat, aes(y=my_mean,x=cyl,ymin=my_mean-my_se,ymax=my_mean+my_se))
p2 <- p2 + geom_bar(stat="identity",width=0.75) + geom_errorbar(stat="identity",width=0.75) + theme_classic() + ylim(c(0,500))
The final plot should look like that:
You can add layers together, but if they have different data and/or aesthetics you'll want to include the data and aes arguments in each graphical layer.
p3 <- ggplot() +
geom_bar(data=my_dat, aes(y=my_mean,x=cyl,ymin=my_mean-my_se,ymax=my_mean+my_se), stat="identity", width = 0.75) +
geom_errorbar(data=my_dat, aes(y=my_mean,x=cyl,ymin=my_mean-my_se,ymax=my_mean+my_se), width = 0.75) +
geom_point(data=mtcars, aes(y=disp, x=cyl, fill=cyl)) +
ylim(c(0,500)) +
theme_classic()
If you want to make it so that the the points are off to the side of the bars, you could subtract an offset from the cyl values to move over the points. Like #LukeA mentioned, by changing the geom_point to geom_point(data=mtcars, aes(y=disp, x=cyl-.5, fill=cyl)).
You can specify each layer individually to ggplot2. Often you are using the same data frame and options for each geom, so it makes sense to set defaults in ggplot(). In your case you should specify each geom separately:
library(ggplot2)
library(gridExtra)
library(dplyr)
se <- function(x){sd(x)/sqrt(length(x))}
my_dat <- summarise(group_by(mtcars, cyl),
my_mean = mean(disp),
my_se = se(disp))
p1 <- ggplot() +
geom_bar(data = my_dat,
aes(y = my_mean, x = cyl,
ymin = my_mean - my_se,
ymax = my_mean + my_se), stat="identity", width=0.75) +
geom_errorbar(data = my_dat,
aes(y = my_mean, x = cyl,
ymin = my_mean - my_se,
ymax = my_mean + my_se), stat="identity", width=0.75) +
geom_point(data = mtcars, aes(y = disp, x = cyl, fill = cyl)) +
theme_classic() + ylim(c(0,500))
p1

Plot mean in an R plot

I would like to add a mean of valuus to windows in a scatter plot I have. I created the scatter plot with ggplot2
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point()
This will give the scatter plot but I woudl like to add add the mean of a window (say size equals 1) and plot this points of the mean as a line. Additionally I woudl like to have vertical bars at each point to indicate the variance.
Mtcars is the data set standard available in ggplot 2
This uses the new dplyr library.
library(dplyr)
forLines <- mtcars %.%
group_by(cut(wt, breaks = 6)) %.%
summarise(mean_mpg = mean(mpg), mean_wt = mean(wt))
p +
geom_point(size=5) +
geom_boxplot(aes(group = cut(wt, breaks = 6))) +
geom_line(data=forLines,aes(x=mean_wt,y=mean_mpg))
Maybe this is what you're looking for:
library(ggplot2)
s <- seq(0, ceiling(max(mtcars$wt)), 1)
ind <- as.integer(cut(mtcars$wt, s))
myfun <- function(i)
c(y = mean(i), ymin = mean(i) - var(i), ymax = mean(i) + var(i))
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
stat_summary(fun.data = myfun, aes(group = ind, x = ind - .5),
colour = "red") +
stat_summary(fun.y = mean, aes(x = ind - .5), geom = "line",
colour = "red")
Is this what you want?
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point() + geom_smooth(aes(wt, mpg, group=1), method = "lm")

Resources