user9166085
user9166085

Reputation: 33

GGplot how to make geom_point size just two discrete sizes

I want to use geom_point but make point sizes two discrete sizes, ideally based on another variable. I don't want it to scale between the two values. For example:

#set variable where if cat=A then point is plotted size=1.5, and all other cat values are plotted at size=1
df$ptsize=ifelse(df$cat == 'A', 1.5, 1)
geom_point(aes(shape = factor(lbl), size = ptsize))

I ended up getting points that are very different sizes. I searched a bit and it seems R wants to scale between sizes. I don't exactly understand this, but scaling is not what I want. It seems like specifying specific sizes should be simple to accomplish.

I tried using a factor variable as ptsize but then I had no control over the sizes or what category was plotted larger.

Any help would be greatly appreciated!!

Thanks!

Upvotes: 1

Views: 2873

Answers (1)

CALUM Polwart
CALUM Polwart

Reputation: 537

Can you post a reprex? My quick test says what you have fond should work:

library(ggplot2)

mpg$ptsize=ifelse(mpg$drv == 'f', 1.5, 1)

ggplot(mpg, aes(displ, hwy)) + 
geom_point( aes(size=ptsize))

It's not the usual way to do it. You'd usually use the factor and control it...

library(ggplot2)
library(tidyverse)
mpg %>% 
mutate (drv = as_factor (drv))%>%
mutate(drv = fct_relevel(drv, "f", "4")) %>%

ggplot( aes(displ, hwy)) + 
geom_point( aes(size=drv))+
scale_size_manual(values = c("f" = 1.5, "4"=1, 1))

Upvotes: 1

Related Questions