geom_point colors based on two variables

Hi, I'm trying to make a scatter plot with the size of the points representing the "estimate" and the color of the points representing "est.dir". I have my code below to show how I am doing that so far.

Here is where I am getting stuck. I want the insignificant points ("p.value" > 0.5 ) to be gray , with the rest having color based on "est.dir". Many thanks!

#create dummy dataset

estimate<-c(-1.5,2.5)
est.dir<-c("neg","pos")
p.value<-c(0.001,0.1)
lat<-c(62,68)
long<-c(22,28)
df<-data.frame(estimate,est.dir,p.value,lat,lon)

#make scatterplot

library(ggplot2)

ggplot(data=dataset,
aes(x=lon,y=lat,size=estimate^2,colour=estdir))+
geom_point()

I would define a new column with a category for Not Significant along with neg and pos and then set the colors manually.

estimate<-c(-1.5,2.5, 1.8)
est.dir<-c("neg","pos", "pos")
p.value<-c(0.001,0.1, 0.02)
lat<-c(62,68, 66)
long<-c(22,28, 24)
df<-data.frame(estimate,est.dir,p.value,lat,long)
df$est.dir2 <- ifelse(p.value <= 0.05, est.dir, "NS")
#make scatterplot

library(ggplot2)

ggplot(data=df,
       aes(x=long,y=lat,size=estimate^2,colour=est.dir2))+
  geom_point() +
  scale_color_manual(values = c("neg" = "red", "pos" = "blue", "NS" = "gray"))
1 Like

Yes, that works perfectly! Thanks so much for the quick response.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.