Khanh Truong
Khanh Truong

Reputation: 11

Resize points of scatter plot in ggplot

I have data:

+-----------+---------+----------+
| AGE_group | mean_y  |  count_y |
+-----------+---------+----------+
|         1 |   0.141 |     1115 |
|         2 |   0.196 |     1043 |
|         3 |   0.202 |     1093 |
|         4 |   0.114 |     1123 |
+-----------+---------+----------+

I use ggpplot:

g_age <- ggplot(AGE_group_df, aes(AGE_group, mean_y, group = 1)) +
  geom_line(size=1, color='blue') +
  geom_point(aes(size=AGE_group_df$count_y), color='darkblue') +
  labs(x = 'Age Group',
       y='% Subscribe',
       title='Age Group and Subscribe Rate',
       size='# Customer')
g_age

enter image description here

Problem: Size of second point (count is 1043) is very small compare to the others.

Question: How can I change the size of the points? (I want to make size of all 4 points nearly equal) while keeping the original count unit.

Thank you very much.

Upvotes: 0

Views: 685

Answers (1)

Mikko
Mikko

Reputation: 7755

You can use (at least) three approaches

  1. Use scale_size_area: scale_size_area()
  2. Use the range argument for scale_size: scale_size(range = 4:5)
  3. Set the limits argument between 0 and maximum count_y: scale_size(limits = c(0, max(AGE_group_df$count_y))

Here more detailed: one option is to use scale_size_area:

AGE_group_df <- data.frame(AGE_group = 1:4, 
                           mean_y = c(0.141, 0.196, 0.202, 0.114), 
                           count_y = c(1115, 1043, 1093, 1123))


ggplot(AGE_group_df, aes(x = AGE_group, y = mean_y, size = count_y)) +
  geom_line(size=1, color='blue') +
  scale_size_area(breaks = round(seq(min(AGE_group_df$count_y),
                  max(AGE_group_df$count_y), length.out = 4), 0)) + 
  geom_point(color='darkblue') +
  labs(x = 'Age Group',
       y='% Subscribe',
       title='Age Group and Subscribe Rate',
       size='# Customer')

enter image description here Another one to manually define the range argument in scale_size:

ggplot(AGE_group_df, aes(x = AGE_group, y = mean_y, size = count_y)) +
  geom_line(size=1, color='blue') +
  scale_size(breaks = round(seq(min(AGE_group_df$count_y), 
             max(AGE_group_df$count_y), length.out = 4), 0), range = 4:5) + 
  geom_point(color='darkblue') +
  labs(x = 'Age Group',
       y='% Subscribe',
       title='Age Group and Subscribe Rate',
       size='# Customer')

enter image description here Third one is to set the lower limit for scale_size to 0:

ggplot(AGE_group_df, aes(x = AGE_group, y = mean_y, size = count_y)) +
  geom_line(size=1, color='blue') +
  scale_size(breaks = round(seq(min(AGE_group_df$count_y), 
             max(AGE_group_df$count_y), length.out = 4), 0), 
             limits = c(0, max(AGE_group_df$count_y))) + 
  geom_point(color='darkblue') +
  labs(x = 'Age Group',
       y='% Subscribe',
       title='Age Group and Subscribe Rate',
       size='# Customer')

Note that you can define the size within the aes function. I added breaks to show the minimum and maximum value, but that is not necessary. It's just an extra feat.

Upvotes: 1

Related Questions