Ecg
Ecg

Reputation: 942

Adjust point size to individual variables using geom_point in R

I would like to adjust the point size for individual variables.

I have tried to scale the values, but I'd like to scale them PER VARIABLE, so for each variable I see a small, medium and big cirle, instead of big circles on A and small on C. To see changes between experiments across variables A, B, C. I'd like to keep the colour as an indicator of abundance overall as it is.

data <- tibble::tibble(
      value = c(4.07,   5.76,   2.87,4.94,  5.48,   6.75,1.53,  1.35,   1.32), 
    Variable = rep(c(rep("A",3),rep("B",3), rep("C",3))),
    Experiment = rep(c(1:3),3))

data <- data %>%
  mutate(scaled_val = scale(value)) %>%
  ungroup()

data$Variable <- factor(data$Variable,levels=rev(unique(data$Variable)))

ggplot(data, aes(x = Experiment, y = Variable, label=NA)) +
    geom_point(aes(size = scaled_val, colour = value)) + 
    geom_text(hjust = 1, size = 2) +
    # scale_size(range = c(1,3)) +
    theme_bw()+
  scale_color_gradient(low = "lightblue", high = "darkblue")

enter image description here

Upvotes: 2

Views: 1761

Answers (1)

tjebo
tjebo

Reputation: 23817

Decided to make my comment an answer. You need to group by variable before scaling.

library(tidyverse)
data <- tibble::tibble(
  value = c(4.07,   5.76,   2.87,4.94,  5.48,   6.75,1.53,  1.35,   1.32), 
  Variable = rep(c(rep("A",3),rep("B",3), rep("C",3))),
  Experiment = rep(c(1:3),3))

data <- data %>%group_by(Variable)%>%
  mutate(scaled_val = scale(value)) %>%

  ungroup()

data$Variable <- factor(data$Variable,levels=rev(unique(data$Variable)))

ggplot(data, aes(x = Experiment, y = Variable, label=NA)) +
  geom_point(aes(size = scaled_val, colour = value)) + 
  geom_text(hjust = 1, size = 2) +
  # scale_size(range = c(1,3)) +
  theme_bw()+
  scale_color_gradient(low = "lightblue", high = "darkblue")
#> Warning: Removed 9 rows containing missing values (geom_text).

Created on 2020-04-22 by the reprex package (v0.3.0)

Upvotes: 3

Related Questions