Brittany Schwartzkopf
Brittany Schwartzkopf

Reputation: 13

how to plot proportion data with a bubble plot in R

I have proportion data on the diet of a fish species from two separate years. I am struggling with how to get the bubble sizes to reflect that the range of possible values is from 0-1, but no value actually reaches 1. This is a plot I made in SigmaPlot that I would like to recreate in R. There are 12 different prey item categories.

enter image description here

I have managed to create a plot in R but the sizes seem to be scaled to the largest proportion. Here is the code and reproduced plot.

library(reshape)
library(ggplot2)

Species <- as.character(c(1:12))
yr2016 <- as.numeric(c(0.17, 0.011, 0.022, 0.003, 0.51, 0.1, 
                       0.01, 0.03, 0.004, 0.06, 0.07, 0.01))
yr2017 <- as.numeric(c(0.197, 0.005, 0.027, 0.01, 0.337, 0.157,
                       0.008, 0.038, 0.017, 0.17, 0.032, 0.002))
data <- as.data.frame(cbind(Species, yr2016, yr2017))
data$yr2016 <- as.numeric(as.character(data$yr2016))
data$yr2017 <- as.numeric(as.character(data$yr2017))
data2 <- melt(data)

ggplot(data2,
       aes(x = variable, y = factor(Species, levels = unique(Species))))+
  geom_point(aes(size = value))+
  labs(y = "Prey Items", x = "Year")+
  theme_classic() +
  scale_size_area()

enter image description here

Upvotes: 1

Views: 1496

Answers (1)

Mark
Mark

Reputation: 2899

You can set the limits manually inside scale_size_area with the argument limits = c(0,1) and manually set the size of the largest area with the max_size argument, i.e. max_size = 20

Hope this gets you what you are looking for.

    library(reshape)
    library(ggplot2)
    library(data.table)
    Species <- as.character(c(1:12))
    yr2016 <-as.numeric(c(0.17,0.011,0.022,0.003,0.51,0.1,0.01,0.03,0.004,0.06,0.07,0.01))
    yr2017 <-as.numeric(c(0.197,0.005,0.027,0.01,0.337,0.157,0.008,0.038,0.017,0.17,0.032,0.002))
    data<-as.data.frame(cbind(Species,yr2016,yr2017))
    data$yr2016 <- as.numeric(as.character(data$yr2016)); 
    data$yr2017 <- as.numeric(as.character(data$yr2017))
    data2<-melt(data)
    p <-  ggplot2::ggplot(data2,aes(x=variable, y=factor(Species, levels=unique(Species))))+
      geom_point(aes(size=value))+
      labs(y="Prey Items",x="Year")+
      theme_classic() +
      scale_size_area( limits = c(0,1),max_size = 20)
    p

enter image description here

If you want, you can also add your own breaks like c(0.1, 0.2, 0.5, etc) or make a sequence of breaks: seq(from = 0.1, to = max(data2$value), by = 0.1)

If you don't just want to set the maximum size but also the minimum, you can switch to scale_size instead of scale_size_area, where range(min, max) sets the size of both ends of the scale

library(reshape)
library(ggplot2)
library(data.table)
Species <- as.character(c(1:12))
yr2016 <-as.numeric(c(0.17,0.011,0.022,0.003,0.51,0.1,0.01,0.03,0.004,0.06,0.07,0.01))
yr2017 <-as.numeric(c(0.197,0.005,0.027,0.01,0.337,0.157,0.008,0.038,0.017,0.17,0.032,0.002))
data<-as.data.frame(cbind(Species,yr2016,yr2017))
data$yr2016 <- as.numeric(as.character(data$yr2016)); 
data$yr2017 <- as.numeric(as.character(data$yr2017))
data2<-melt(data, id = 'Species')
sizes <- c('0.2' = 0.2, '0.4' = 0.4, '0.6' = 0.6, '0.8'= 0.8, '1.0' = 1.0)
p <-  ggplot2::ggplot(data2,aes(x=variable, y=factor(Species, levels=unique(Species))))+
  geom_point(aes(size=value))+
  labs(y="Prey Items",x="Year")+
  theme_classic() +
  scale_size( limits = c(0,1),breaks = seq(from = 0.1, to = max(data2$value), by = 0.1),range = c(1,20))
p

enter image description here

Upvotes: 0

Related Questions