tjebo
tjebo

Reputation: 23747

Force size aesthetic to scale to given breaks

I am creating several plots in order to create frames for a gif. It is supposed to show growing points over time. (see plot 1 and 2 - the values increase). Using size aesthetic is problematic, because the scaling is done for each plot individually.
I tried to set breaks with scale_size_area() to provide a sequence of absolute values, in order to scale on 'all values' rather than only the values present in each plot. (no success).

Plot 3 shows how the points should be scaled, but this scaling should be achieved in each plot.

library(tidyverse)
df1 <- data.frame(x = letters[1:5], y = 1:5, size2 = 21:25) 
ggplot(df1, aes(x, y, size = y))   +
  geom_point() +
scale_size_area(breaks = seq(0,25,1))

ggplot(df1, aes(x, y, size = size2))   +
  geom_point() +
  scale_size_area(breaks = seq(0,25,1))

df2 <- data.frame(x = letters[1:5], y = 1:5, size2 = 21:25) %>% gather(key, value, y:size2)

ggplot(df2, aes(x, value, size = value))   +
  geom_point() +
  scale_size_area(breaks = seq(0,25,1))

Created on 2019-05-12 by the reprex package (v0.2.1)

Upvotes: 2

Views: 2242

Answers (2)

Seungwook Kim
Seungwook Kim

Reputation: 69

How about this?

library("ggplot2")
df1 <- data.frame(x = letters[1:5],
                  y = 1:5) 
ggplot(data = df1, 
       aes(x = x, 
           y = y, 
           size = y)) +
  geom_point() +
  scale_size_area(breaks = seq(1,25,1),
                  limits = c(1, 25)) 

enter image description here

Upvotes: 1

pogibas
pogibas

Reputation: 28339

Pass lower and upper bound to limits argument in scale_size_area function:

ggplot(df1, aes(x, y, size = y))   +
  geom_point() +
  labs(
    title = "Y on y-axis",
    size = NULL
  ) +
  scale_size_area(limits = c(0, 25))

ggplot(df1, aes(x, y, size = size2 ))   +
  geom_point() +
  labs(
    title = "size2 on y-axis",
    size = NULL
  ) +
  scale_size_area(limits = c(0, 25))

enter image description here

Upvotes: 4

Related Questions