ggplot stat_smooth: change look of multiple bands - r

I'm trying to customize the look of multiple loess plots within the same graph for different levels of a group var. I looked at this post, but wasn't able to make it work:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
stat_smooth(method = "loess")
I'd like to change the color of each band and line.

You can specify the looks with for example the scale_color_manual scale. In the example below I also used override.aes within guides to get a nice legend as well:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
stat_smooth(aes(fill=Species), method = "loess", size=1) +
scale_color_manual(values = c("green","blue","red")) +
scale_fill_manual(values = c("green","blue","red")) +
scale_linetype_manual(values = c("dashed","dotted","solid")) +
theme_bw() +
guides(fill=guide_legend(override.aes = list(fill="white",size=1.2)))
this gives:
Other alternatives to the manual scales are the hue and brewer scales.

I added size = 2 so you can see that the line type is different for each line:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
stat_smooth(method = "loess", aes(fill = Species), size= 2)

Related

Facet, plot axis on all outsides

Is there a way to mirror the x-/y-axis to the opposite side (only outside, no axis on the inside)? My goal is to get something like:
MWE
library(ggplot2)
ggplot(data = iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() +
facet_grid(rows = "Species")
This should work:
ggplot(data = iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() +
scale_y_continuous(sec.axis = sec_axis(~.*1))+
scale_x_continuous(sec.axis = sec_axis(~.*1))+
facet_grid(rows = "Species")
Yes, you can use the guides() function to set axes for secondary positions as well, see example below. To have it more like your example, the strip placement should be changed to "outside".
library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() +
facet_grid(rows = vars(Species)) +
guides(
x.sec = "axis", y.sec = "axis"
) +
theme(strip.placement = "outside")
Created on 2022-06-07 by the reprex package (v2.0.0)

Add mean to grouped box plot in R with ggplot2

I created a grouped box plot in R with ggplot2. However, when I want to add the mean, the dots appear between the two boxes in a group. How can I change it such that the dots are within each box?
Here my code:
ggplot(results, aes(x=treatment, y=effect, fill=sex)) +
geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=20, size=3, color="red")`
You can use position_dodge2. Because points and boxplots have differing widths, you will need to trial and error with the width argument to centralise the dots.
ggplot(mtcars, aes(x=factor(gear), y=hp, fill=factor(vs))) +
geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=20, size=3, color="red",
position = position_dodge2(width = 0.75,
preserve = "single"))
In most cases, you will not be able to place the points inside each grouped box as they overlap with each other through the axes. One alternative is to use facet_wrap.
Here is one example with iris data:
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, fill=Species)) +
geom_point(aes(color = Species), shape = 20, size = 3) +
geom_boxplot(alpha = 0.8) +
facet_wrap(~Species)
If you don't want the color of the points to be the same as the color of the boxplots, you have to remove the grouping variable from the aes inside the geom_point. Again, with the iris example,
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, fill=Species)) +
geom_boxplot(alpha = 0.8) +
geom_point(shape = 20, size = 3, color = 'red') +
facet_wrap(~Species)
Note that the ggplot2 package works in layers. Therefore, if you add the geom_point layer after the geom_boxplot layer, the points will be on the top of the boxplot. If you add the geom_point layer before the geom_boxplot layer, the points will be in the background.
Edit:
If what you want is to add a single point in your boxplot to indicate the mean, you can do something like:
iris %>%
group_by(Species) %>%
mutate(mean.y = mean(Sepal.Width),
mean.x = mean(Sepal.Length)) %>%
ggplot(aes(x=Sepal.Length, y=Sepal.Width, fill=Species)) +
geom_boxplot(alpha = 0.8) +
geom_point(aes(y = mean.y, x = mean.x), shape = 20, size = 3, color = 'red')
But be aware that it would probably require some calibration on the x axis to make it exactly in the middle of each box.

Mixed fill color in ggplot2 legend using geom_smooth() in R

When plotting two regression curves using geom_smooth() in ggplot2, for the fill color, the legend picks the one where the confidence intervals intersect. I do think this behaviour arises when the overlapping area is proportionally bigger that the other, however I find this quite undesired because the reader is capable of deducing that the "darkened" area is the one where the CI intersect. It is IMHO a bit harder or unintuitive to assign the same color for both the curves.
How can I correct this ?
MWE:
library(ggplot2)
p <- ggplot(data=iris, aes(x=Sepal.Width, y=Sepal.Length)) + geom_point()
p <- p + geom_smooth(method=loess, aes(colour="Loess"), fill="yellow")
p <- p + geom_smooth(method=lm, aes(colour="LM"))
print(p)
Output:
You can add the fill as an aesthetic mapping, ensuring you name it the same as the color mapping to get legends to merge:
library(ggplot2)
ggplot(data=iris, aes(x=Sepal.Width, y=Sepal.Length)) +
geom_point(aes(shape = "data")) +
geom_smooth(method=loess, aes(colour="Loess", fill="Loess")) +
geom_smooth(method=lm, aes(colour="LM", fill = "LM")) +
scale_fill_manual(values = c("yellow", "gray"), name = "model") +
scale_colour_manual(values = c("red", "blue"), name = "model") +
labs(shape = "")

Bar plot options with ggplot2

I am actually trying to do a graph with ggplot2 but I'd like to add some options (colors, legend...).
Here is my code :
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment)) +
stat_summary(fun.y="mean", geom="bar") +
facet_grid(. ~ treatment) +
theme_grey() +
xlab("Treatment") +
ylab("OT") +
scale_fill_grey() +
theme(strip.background = element_rect(colour = "black", fill = "white"))
And here the actual output.
Could you please indicate me how to change the name of 1 and 2 (without changing in it the dataframe) and how to add colours to this ?
I tried this
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, colour=Treatment))
But it applies the color only to the outline of the figure.
To change the color of the bars you need fill = Treatment.
To change the labels on the x axis you need scale_x_discrete(labels = your_labels). See here.
So your code will look like:
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, fill= Treatment)) +
scale_x_discrete(labels = your_labels) +
...

setting trasparency in ggplot2 using aes_color_fill

I wrote this few lines in order to plot some boxplots, and everything works:
plotSerie <- ggplot(fileIn) +
geom_boxplot(aes(x=DOY_S1, y=S1_VV, alpha=0.5, fill="S1", group=paste(fileIn$DOY_S1, sep=""))) +
geom_boxplot(aes(x=DOY_RS2, y=RS2_VV, alpha=0.5, fill="RS2", group=paste(fileIn$DOY_RS2, sep=""))) +
scale_fill_manual(name="Sensor", values=c("S1"="brown", "RS2"="grey"))
but I would like to define the trasparency in scale_fill_manual.
In google I found this solution, but doesn't work
plotSerie <- ggplot(fileIn)+
geom_boxplot(aes(x=DOY_S1, y=S1_VV, fill="S1",group=paste(fileIn$DOY_S1, sep=""))) +
geom_boxplot(aes(x=DOY_RS2, y=RS2_VV, fill="RS2", group=paste(fileIn$DOY_RS2, sep=""))) +
scale_fill_manual(name="Sensor", values=alpha(c("S1"="brown", "RS2"="grey"), 0.5))+
can you help me?
Thank you very much
Specify alpha in the geom:
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_boxplot(alpha = 0.5) +
scale_fill_manual(values = c(setosa = "blue",
versicolor = "green",
virginica = "red"))
The transparency is adopted for the legend automatically.
Of course, you can also map a variable to a transparency scale with aes.

Resources