Home > Back-end >  geom_point shapes not the same size when aes size are the same value
geom_point shapes not the same size when aes size are the same value

Time:12-08

I have a population and two samples. One sample is a random sample and another is proportional to the population. I want to create plots that show how proportional the samples are by some factors in this case year and gear. I want a plot that has a circle and cross the same size when they sample is proportional to population for given factors and if the cross is larger than the circle this indicates the sample sample more relative to the population and vice versa. See below for my code.

library(ggplot2)
library(dplyr)
library(tidyr)
n_y = 5
min_year = 1900
years = min_year:(min_year   n_y - 1)

fixed_gear_catch = rlnorm(n = n_y, meanlog = log(2500), 0.5)
trawl_gear_catch = rlnorm(n = n_y, meanlog = log(1200), 0.3)

fixed_gear_obs = rlnorm(n = n_y, meanlog = log(250), 0.5)
trawl_gear_obs = rlnorm(n = n_y, meanlog = log(120), 0.3)


population_df = data.frame(fixed_gear = fixed_gear_catch, 
                        trawl_gear = trawl_gear_catch,
                        years = years)

sample_data = data.frame(fixed_gear = fixed_gear_obs, 
                        trawl_gear = trawl_gear_obs,
                        years = years)
proportional_sample_data = data.frame(fixed_gear = fixed_gear_catch * 0.2, 
                           trawl_gear = trawl_gear_catch * 0.2,
                           years = years)
population_df = population_df %>% gather("gear", "catch", -years)
sample_data = sample_data %>% gather("gear", "catch", -years)
proportional_sample_data = proportional_sample_data %>% gather("gear", "catch", -years)

## give type and merge
population_df$type = "Catch"
sample_data$type = "Observed"
proportional_sample_data$type = "Observed"

full_df = rbind(population_df, sample_data)
full_proportional_df = rbind(population_df, proportional_sample_data)

## convert to proporitons
full_df = full_df %>% group_by(years, type) %>% mutate(percentage = catch / sum(catch) * 100)
full_proportional_df = full_proportional_df %>% group_by(years, type) %>% mutate(percentage = catch / sum(catch) * 100)
## check the perfect proportions are what we think they should be
full_proportional_df %>% pivot_wider(id_cols = years, values_from = percentage, names_from = c(gear, type))
full_df %>% pivot_wider(id_cols = years, values_from = percentage, names_from = c(gear, type))

## plot with circle and crosses
shpe_manual = c("Catch" = 1, "Observed" = 3)
col_manual = c("Catch" = "red", "Observed" = "blue")

ggplot(full_df, aes(x = gear, y = years))  
  geom_point(aes(shape = type, col = type, size = percentage))  
  scale_shape_manual(values=shpe_manual)  
  scale_size_continuous(limits = c(0,100), range = c(0,15))   
  scale_color_manual(values = col_manual)

## this should have perfec sized circles and crosses but doesn't
ggplot(full_proportional_df, aes(x = gear, y = years))  
  geom_point(aes(shape = type, col = type, size = percentage))  
  scale_shape_manual(values=shpe_manual)  
  scale_size_continuous(limits = c(0,100), range = c(0,15))   
  scale_color_manual(values = col_manual) 

CodePudding user response:

The cross is naturally 2x as tall/wide, so I think this fixes it visually:

ggplot(full_df, aes(x = gear, y = years))  
  geom_point(aes(shape = type, col = type, size = percentage * if_else(type == "Observed", 0.5, 1)))  
  scale_shape_manual(values=shpe_manual)  
  # scale_size_continuous(limits = c(0,100), range = c(0,15))   
  scale_color_manual(values = col_manual)  
  scale_size_area(max_size = 15)

enter image description here

As a check:

ggplot(full_proportional_df, aes(x = gear, y = years))  
  geom_point(aes(shape = type, col = type, 
                 size = percentage * if_else(type == "Observed", 0.5, 1)))  
  scale_shape_manual(values=shpe_manual)  
  scale_size_continuous(limits = c(0,100), range = c(0,15))   
  scale_color_manual(values = col_manual) 

enter image description here

  • Related