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

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.

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.

How to create two different regression line based on factor for each facet? R, ggplot2

I am trying to create two different lines based on exercise = 0 or exercise = 1 for each facet (by gender). The first code is without facet_wrap and the two lines based on gender are different. The second code is with facet_wrap and the two lines seem to be the same line. How can I change the code so that the two lines are different within each facet?
ggplot(cdc, aes(weight,wtdesire, color = exercise, group =
interaction(gender,exercise))) + geom_point(alpha = 1/5) +
geom_smooth(method = lm, aes(linetype=exercise))
produces: facet
However, when I add facet_wrap the two lines for each facet seem to be the same.
ggplot(cdc, aes(weight,wtdesire, color = exercise, group =
interaction(gender,exercise))) + geom_point(alpha = 1/5) +
geom_smooth(method = lm, aes(linetype=exercise)) + facet_wrap(~gender)
produces: second
#LoBu solution is correct. Here's an example using mtcars data:
ggplot(mtcars, aes(hp, mpg, group=interaction(vs, am))) +
geom_point(alpha = 0.2) +
geom_smooth(method = lm, aes(linetype=as.factor(vs)))
ggplot(mtcars, aes(hp, mpg, group=vs)) +
geom_point(alpha = 0.5) +
geom_smooth(method = lm, aes(linetype=as.factor(vs))) +
facet_wrap(~am)

Different colours for facet_grid strip backgrounds

I would like to change the colours of the strip backgrounds to a predefined order.
This code generates the plot, and changes the strip backgrounds to red:
p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(. ~ cyl) +
theme(strip.background = element_rect(fill="red"))
I'd like to do something like the below however, which ideally would specify a different colour for each strip
p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(. ~ cyl) +
theme(strip.background = element_rect(fill=c("red","green","blue","yellow")))
Which just makes them all red...
This was asked in similar questions years ago, the answer was to manipulate grobs. I was hoping that there was a simpler solution in the years since?

Smooth line to the full datase (faceting) ggplot2

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)

How to place multiple boxplots in the same column with ggplot(geom_boxplot)

I would like to built a boxplot in which the 4 factors (N1:N4) are overlaid in the same column. For example with the following data:
df<-data.frame(N=N,Value=Value)
Q<-c("C1","C1","C2","C3","C3","C1","C1","C2","C2","C3","C3","Q1","Q1","Q1","Q1","Q3","Q3","Q4","Q4","Q1","Q1","Q1","Q1","Q3","Q3","Q4","Q4")
N<-c("N2","N3","N3","N2","N3","N2","N3","N2","N3","N2","N3","N0","N1","N2","N3","N1","N3","N0","N1","N0","N1","N2","N3","N1","N3","N0","N1")
Value<-c(4.7,8.61,8.34,5.89,8.36,1.76,2.4,5.01,2.12,1.88,3.01,2.4,7.28,4.34,5.39,11.61,10.14,3.02,9.45,8.8,7.4,6.93,8.44,7.37,7.81,6.74,8.5)
with the following (usual) code, the output is 4 box-plots displayed in 4 columns for the 4 variables
ggplot(df, aes(x=N, y=Value,color=N)) + theme_bw(base_size = 20)+ geom_boxplot()
many thanks
Updated Answer
Based on your comment, here's a way to add marginal boxplots. We'll use the built-in mtcars data frame.
First, some set-up:
library(cowplot)
# Common theme elements
thm = list(theme_bw(),
guides(colour=FALSE, fill=FALSE),
theme(plot.margin=unit(rep(0,4),"lines")))
Now, create the three plots:
# Main plot
p1 = ggplot(mtcars, aes(wt, mpg, colour=factor(cyl), fill=factor(cyl))) +
geom_smooth(method="lm") + labs(colour="Cyl", fill="Cyl") +
scale_y_continuous(limits=c(10,35)) +
thm[-2] +
theme(legend.position = c(0.85,0.8))
# Top margin plot
p2 = ggplot(mtcars, aes(factor(cyl), wt, colour=factor(cyl))) +
geom_boxplot() + thm + coord_flip() + labs(x="Cyl", y="")
# Right margin plot
p3 = ggplot(mtcars, aes(factor(cyl), mpg, colour=factor(cyl))) +
geom_boxplot() + thm + labs(x="Cyl", y="") +
scale_y_continuous(limits=c(10,35))
Lay out the plots and add the legend:
plot_grid(plotlist=list(p2, ggplot(), p1, p3), ncol=2,
rel_widths=c(5,1), rel_heights=c(1,5), align="hv")
Original Answer
You can overlay all four boxplots in a single column, but the plot will be unreadable. The first example below removes N as the x coordinate, but keeps N as the colour aesthetic. This results in the four levels of N being plotted at a single tick mark (which I've removed by setting breaks to NULL). However, the plots are still dodged. To plot them one on top of the other, set the dodge width to zero, as I've done in the second example. However, the plots are not readable when they are overlaid.
ggplot(df, aes(x="", y=Value,color=N)) +
theme_bw(base_size = 20) +
geom_boxplot() +
scale_x_discrete(breaks=NULL) +
labs(x="")
ggplot(df, aes(x="", y=Value,color=N)) +
theme_bw(base_size = 20) +
geom_boxplot(position=position_dodge(0)) +
scale_x_discrete(breaks=NULL) +
labs(x="")

Resources