如何在 R 中用气泡图绘制比例数据

how to plot proportion data with a bubble plot in R

我有两个不同年份的鱼类饮食比例数据。我正在努力研究如何让气泡大小反映可能值的范围是 0-1,但实际上没有值达到 1。这是我在 SigmaPlot 中制作的一个图,我想在 R 中重新创建。有12 种不同的猎物类别。

我已经成功地在 R 中创建了一个绘图,但尺寸似乎已缩放到最大比例。这是代码和转载的情节。

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()

您可以在 scale_size_area 内使用参数 limits = c(0,1) 手动设置限制,并使用 max_size 参数手动设置最大区域的大小,即 max_size = 20

希望这能满足您的需求。

    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

如果你愿意,你也可以添加你自己的breaks喜欢c(0.1, 0.2, 0.5, etc) 或进行一系列休息:seq(from = 0.1, to = max(data2$value), by = 0.1)

如果你不仅要设置最大尺寸还要设置最小尺寸,可以切换到scale_size而不是scale_size_area,其中range(min, max)设置的尺寸天平两端

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