 # How to give unique symbol and color to "zero" value?

Hi! I am a beginner, and trying to make a scatter plot where point size represents the value in the "corr" column and the point color represents the value in the "corrdir" column (which shows whether the "corr" value is positive or negative).

With the code below, zero values are represented as "negative". Instead, I would like for the zero value to have a unique color. In addition, if possible, I would like to have the zero value as a different shape than the rest.

Any advice would be really appreciated.

Thanks in advance!

library(ggplot2)
library(plyr)

lat<-c(20,21,22,23,24)
long<-c(55,56,57,58,59)
corr<-c(-5,-3,0,3,5)
df<-data.frame(lat,long,corr)

d <- df %>%
mutate(corrdir=ifelse(corr>0,"positive","negative"))

ggplot(d,aes(x=long,y=lat,size=corr^2,colour=corrdir,))+
geom_point()

The change in shape of the zero value is hard to see and I dropped the legend for shapes. The case_when takes the place of using multiple if_else.

``````library(ggplot2)
library(dplyr)
lat<-c(20,21,22,23,24)
long<-c(55,56,57,58,59)
corr<-c(-5,-3,0,3,5)
df<-data.frame(lat,long,corr)

d <- df %>%
mutate(corrdir = case_when(
corr < 0 ~ "negative",
corr == 0 ~ "zero",
corr > 0 ~ "positive"
),
NonZero = ifelse(corr == 0, "Zero", "Nonzero"))

ggplot(d,aes(x=long,y=lat,size=corr^2,colour=corrdir, shape = NonZero))+
geom_point() + scale_shape(guide = FALSE)
``````
1 Like

Yes, that solves the problem. Thanks a lot!

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.