ggplot2: trouble filling point when using stat_summary argument - r

I have the following code:
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
stat_summary(fun.y=mean, geom='point', size=2, fill='white')
I know there are probably other ways of plotting this mean using the iris data. For my own data, though, it is the only way.
PROBLEM: the code above doesn't give white-filled points, but solid black points. Is there ar way to set the fill-colour when using the stat_summary argument?
Thanks!

Either use color instead of fill
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
stat_summary(fun.y=mean, geom='point', size=2, color='white')
or use a symbol shape that has a fill and a border color
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
stat_summary(fun.y=mean, geom='point', size=2, shape=21, fill="blue", color="red")

Related

ggplot: boxplot with flipped and log transformed coordinates

given the following reproducible example
ggplot(diamonds, aes(cut, price)) +
geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=5, size=2, colour='black')+
coord_trans(y="log10")
ggplot(diamonds, aes(cut, price)) +
geom_boxplot() +
coord_trans(x="log10")+
stat_summary(fun.y=mean, geom="point", shape=5, size=2, colour='black')+
coord_flip()
it's not clear to me how to transform the coordinates so that to keep the log transformation of the 'price' axis (y flipped);
in fact, after the coordinates flipping the price axis is apparentely loosing the log transformation as a (unwanted by me) side effect;
to be noted that I need that the transforming of the coordinate system occurs after the statistic has been computed, and this is why I used 'coord_trans()' and not 'scale_y_log10()'...
any help for pointing me in the right direction?
thank you
...oops, very simply switch the aes mappings, drop the flip of coordinates and it's done!
ggplot(diamonds, aes(price, cut)) +
geom_boxplot() +
stat_summary(fun=mean, geom="point", shape=5, size=2, colour='black')+
coord_trans(x="log10")

Separate stat_density for two density curves in ggplot2

I am trying to produce a ggplot that shows the histogram of the data as well as two density curves in which one has no adjust value and the other one has.
I tried the following code:
ggplot(df, aes_string(x=value))+
geom_histogram(aes(y=..density..), colour="grey", fill="grey", alpha=.3)+
geom_density(colour="red", fill="red", alpha=.3)+
stat_density(bw="SJ", alpha=0)+
geom_density(colour="blue", fill="blue", alpha=.3)+
stat_density(bw="SJ", adjust=5, alpha=0)+
theme_bw()
But this produces this graph with both curves overlapping 100%...
The .txt dataframe used is on my google drive
Thanks in advance!
Does adding a specific adjust argument to geom_density not do what you want?
ggplot(df, aes(x=value))+
geom_histogram(aes(y=..density..), colour="grey", fill="grey", alpha=.3)+
geom_density(colour="red", fill="red", alpha=.3, adjust = 1)+
geom_density(colour="blue", fill="blue", alpha=.3, adjust = 2)+
theme_bw()

geom_label_repel text justification and alignment

Is there a possible work-around to left-justify the text label created by geom_label_repel (or geom_text_repel) in the example below where all text are placed with positive nudge_x value and y-only adjusted position in direction parameter? Currently, the default behavior is to center-align the text:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label_repel(aes(label=rownames(mtcars)),
size=3, segment.size=0.25, nudge_x=0.5, direction="y")
I am looking to emulate the left-justification that is possible in geom_label (or geom_text) by setting hjust=0 as seen in example below, while being able to automatically repel labels in the y direction:
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label(aes(label=rownames(mtcars)), size=3, nudge_x=0.2, hjust=0)
Edited: As a hack, would it be possible to build hjust (and vjust) into ggrepel?
In the 4 years since OP posted this question, hjust= seems to have been added to the ggrepel package:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label_repel(
aes(label=rownames(mtcars)), hjust=0,
size=3, segment.size=0.25, nudge_x=0.5, direction="y")

lines linear model ggplot

How can I make the lines thicker without adding geom_line
ggplot(df, aes(x=c, y=d, colour=group)) +
geom_point()+
geom_smooth( method=lm,se=FALSE, fullrange=TRUE)
Thank you

geom_smooth in ggplot causes part of plot background to change colour

How can I avoid the grey shading of the plot area that occurs when plotting the following data?
df <-data.frame(x = c(0,0.2,0.5), y = c(0.6,0.7,0.9))
p <-ggplot(df, aes(x, y, ymin=0, ymax=1, xmin=0, xmax=1))
p <- p + geom_point(alpha=2/10, shape=21,
fill="blue", colour="black", size=5)
p
So fine up until this point but then adding a line equation using geom_smooth causes part of the background to become grey.
p <- p + geom_smooth(method="lm", se=FALSE, formula=y~x, colour="black")
p
Any suggestions on how to avoid this? Thanks.
Add fill=NA to your geom_smooth call:
p + geom_smooth(method="lm", se=FALSE, formula=y~x,colour="black",fill=NA)

Resources