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)
Related
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")
I really like the way ggplot2 2.0 improved the looks, but suspect that the upgrade changed the way colors and legend is defined. How can I update my code for ggplot 2.0?
The first abline should be black (and still is). Should not be in legend.
The ablines "Line1", "Line2", and "Line3" should have different colors, and be in legend. They are all black now.
The legend should be visible, but is not anymore.
library(ggplot2)
plot.data <- data.frame(x=c(2, 8), y=c(3, 6))
p <- ggplot(plot.data, aes(x=x, y=y))
p <- p + geom_point(color="black")
p <- p + geom_abline(intercept=0, slope=0.5, color="black", linetype="dashed")
#p <- p + geom_abline(intercept=0, slope=1, aes(color="Line1"), linetype="dashed", show_guide=TRUE)
p <- p + geom_abline(intercept=0, slope=1, aes(color="Line1"), linetype="dashed", show.legend=TRUE)
p <- p + geom_abline(intercept=0, slope=2, aes(color="Line2"), linetype="dashed")
p <- p + geom_abline(intercept=0, slope=3, aes(color="Line3"), linetype="dashed")
p <- p + xlim(0,10)
p <- p + ylim(0,10)
p <- p + theme(legend.title=element_blank(), legend.position="bottom")
p
With the original code (with # in example above) I get a warning message "show_guide has been deprecated. Please use show.legend instead`", but changing show_guide to show.legend above makes no difference.
Note: I'm not 100% sure it is the upgrade that is the problem, it might be my original example that is wrong.
I just repost my comment here as an answer.
to make the legend reappear, the intercept and the slope also have to be in the aes() call.
p + geom_abline(aes(intercept=0, slope=1, color="Line1"), linetype="dashed", show.legend=TRUE)
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
data=data.frame(x=rep(0:9, each=2))
ggplot(data, aes(x=factor(x))) + geom_bar(alpha=0.5) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=factor(x), y=y), alpha=0.5)
ggplot(data, aes(x=factor(x))) + geom_bar(alpha=0.5) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=factor(x), y=y), alpha=0.5) +
scale_x_discrete(limits=0:10)
Also, do I have to factor given x is integer so it is discrete already?
Wrong order
Wrong x axis label.
ggplot(data, aes(x=x)) + geom_bar(alpha=0.5) + scale_x_discrete(limits=0:10) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=x, y=y), alpha=0.5)
You can force a discrete scale to get what you want. It is odd how when you mix geom_point() and geom_bar() ggplot starts ordering things in unexpected ways.
Currently my regression plot looks like this. Notice that
the regression line is deeply buried.
Is there any way I can modify my code here, to show it on top of the dots?
I know I can increase the size but it's still underneath the dots.
p <- ggplot(data=my_df, aes(x=x,y=y),) +
xlab("x") +
ylab("y")+
geom_smooth(method="lm",se=FALSE,color="red",formula=y~x,size=1.5) +
geom_point()
p
Just change the order:
p <- ggplot(data=my_df, aes(x=x,y=y),) +
xlab("x") +
ylab("y")+
geom_point() +
geom_smooth(method="lm",se=FALSE,color="red",formula=y~x,size=1.5)
p
The issue is not the color, but the order of the geoms.
If you first call geom_point() and then geom_smooth()
the latter will be on top of the former.
Plot the following for comparison:
Before <-
ggplot(data=my_df, aes(x=x,y=y),) +
xlab("x") +
ylab("y")+
geom_smooth(method="lm",se=FALSE,color="red",formula=y~x,size=1.5) +
geom_point()
After <-
ggplot(data=my_df, aes(x=x,y=y),) +
xlab("x") +
ylab("y")+
geom_point() +
geom_smooth(method="lm",se=FALSE,color="red",formula=y~x,size=1.5)
How about transparent points?
library(ggplot2)
seed=616
x1<- sort(runif(rnorm(1000)))
seed=626
x2<- rnorm(1000)*0.02+sort(runif(rnorm(1000)))
my_df<- data.frame(x= x1, y = x2)
p <- ggplot(data=my_df, aes(x=x,y=y),) +
xlab("x") +
ylab("y")+
geom_smooth(method="lm",se=FALSE,color="red",formula=y~x,size=1.5)+
geom_point(size = I(2), alpha = I(0.1))
p