Josh
Josh

Reputation: 23

Is there a way to change range of size values in ggplot2 (geom_point) in R?

I am unsure of how to set the size of geom_point in R ggplot in my bubble chart. One of my numerical variables is p-value, and the smallest bubbles are automatically plotted from the smallest p-values, but I would like to make it so that the largest p-values are shown by the smallest size of bubble on the plot.

enter image description here

I've tried using p + guides(size= guide_legend(reverse = TRUE)) but this just changes the order of the size of bubbles on the legend.

library(ggplot2)
data(TFRC, package="ggplot2") 

TFRC <- read.csv(file.choose(), header = TRUE)

# bubble chart showing position of polymorphisms on gene, the frequency of each of these polymorphisms, where they are prominent on earth, and p-value 

TFRCggplot <- ggplot(TFRC, aes(Position, Frequency))+
  geom_jitter(aes(col=Geographical.Location, size=p.value))+
  labs(subtitle="Frequency of Various Polymorphisms", title="TFRC")
  TFRCggplot + guides(size = guide_legend(reverse = TRUE))

Upvotes: 1

Views: 2243

Answers (1)

neilfws
neilfws

Reputation: 33822

See ?scale_size_continuous.

You could try reversing the range values:

library(tibble)
library(ggplot2)

tibble(x = 1:5, 
       y = c(0.001, 0.01, 0.05, 0.1, 1)) %>% 
  ggplot(aes(x, y)) + 
  geom_point(aes(size = y)) + 
  scale_size_continuous(range = c(6, 1))

enter image description here

Or you could try trans = "reverse":

tibble(x = 1:5, 
       y = c(0.001, 0.01, 0.05, 0.1, 1)) %>% 
  ggplot(aes(x, y)) + 
  geom_point(aes(size = y)) + 
  scale_size_continuous(trans = "reverse")

enter image description here

Upvotes: 3

Related Questions