如何在 ggplot 气泡图中添加两个不同大小的点?

How to add two different magnitudes of point size in a ggplot bubbles chart?

我刚刚遇到这样的图表,其中使用了geom_point的两种颜色(我相信它是由ggplot2制作的)。同样,我想要一种颜色的点,范围从 1 到 5,一系列点的范围为 10 到 50 的另一种颜色。但是我不知道如何添加两个不同范围的点一张图。

我的基本步骤是:

a <- c(1,2,3,4,5)
b <- c(10,20,30,40,50)
Species <- factor(c("Species1","Species2","Species3","Species4","Species5"))
bubba <- data.frame(Sample1=a,Sample2=b,Species=Species)
bubba$Species=factor(bubba$Species, levels=bubba$Species)
xm=melt(bubba,id.vars = "Species", variable.name="Samples", value.name = "Size")
str(xm)
ggplot(xm,aes(x= Samples,y= fct_rev(Species)))+geom_point(aes(size=Size))+scale_size(range = range(xm$Size))+theme_bw()

任何人都可以提供我应该调查的线索吗?谢谢!

我认为您正在寻找 R

中的气泡图

https://www.r-graph-gallery.com/bubble-chart/

也就是说,您可能想分别构建图形的右侧和左侧,然后组合起来。

a <- c(1, 2, 3, 4, 5)
b <- c(10, 20, 30, 40, 50)

Species <- factor(c("Species1", "Species2", "Species3", "Species4", "Species5"))

bubba <- data.frame(Sample1 = a, Sample2 = b, Species = Species)
bubba$Species <- factor(bubba$Species, levels = bubba$Species)
xm <- reshape2::melt(bubba, id.vars = "Species", variable.name = "Samples", value.name = "Size")

ggplot(xm, aes(x = Samples, y = fct_rev(Species))) +
  geom_point(aes(size = Size, color = Size)) +
  scale_color_continuous(breaks = c(1,2,3,10,20,30), guide = guide_legend()) +
  scale_size(range = range(xm$Size), breaks = c(1,2,3,10,20,30)) +
  theme_bw()

这里有一个问题。我现在没有时间弄清楚这个传说。请注意,1 和 10 大小相同,但颜色不同,3 和 40 也是如此。

# Create data frame
a <- c(1, 2, 3, 4, 5)
b <- c(10, 20, 30, 40, 50)
Species <- factor(c("Species1", "Species2", "Species3", "Species4", "Species5"))
bubba <- data.frame(Sample1 = a, Sample2 = b, Species = Species)

# Restructure data
xm <- reshape2::melt(bubba, id.vars = "Species", variable.name = "Samples", value.name = "Size")

# Calculate bubble size
bubble_size <- function(val){
  ifelse(val > 3, (1/15) * val + (1/3), val)
}

# Calculate bubble colour
bubble_colour <- function(val){
  ifelse(val > 3, "A", "B")
}

# Calculate bubble size and colour
xm %<>% 
  mutate(bub_size = bubble_size(Size),
         bub_col = bubble_colour(Size))

# Plot data
ggplot(xm, aes(x = Samples, y = fct_rev(Species))) +
  geom_point(aes(size = bub_size, fill = bub_col), shape = 21, colour = "black") +
  theme(panel.grid.major = element_line(colour = alpha("gray", 0.5), linetype = "dashed"),
        text = element_text(family = "serif"),
        legend.position = "none") + 
  scale_size(range = c(1, 20)) +
  scale_fill_manual(values = c("brown", "pink")) +
  ylab("Species")

我的方法已经完成了 90%,但我不确定如何完成任务。为了获得尺寸的单个图例,我使用了一个转换将输入尺寸转换为显示尺寸。这使得图例外观符合显示。我还没有弄清楚的是如何对 fill 应用类似的转换,以便将两者整合到同一个图例中。

这是转换,在本例中将所有内容缩小 10 或更多:

library(scales)
shrink_10s_trans = trans_new("shrink_10s",
          transform = function(y){
            yt = if_else(y >= 10, y*0.1, y)
            return(yt)
          },
          inverse = function(yt){
            return(yt) # Not 1-to-1 function, picking one possibility
          }
)

然后我们可以在大小上使用这个转换来有选择地仅缩小 10 或更大的点。除了将填充编码与大小编码集成之外,这对于图例来说效果很好。

ggplot(xm,aes(x= Samples,y= fct_rev(Species), fill = Size < 10))+
  geom_point(aes(size=Size), shape = 21)+
  scale_size_area(trans = shrink_10s_trans, max_size = 10,
                  breaks = c(1,2,3,10,20,30,40),
                  labels = c(1,2,3,10,20,30,40)) +
  scale_fill_manual(values = c(rgb(136,93,100, maxColorValue = 255),
                               rgb(236,160,172, maxColorValue = 255))) +
  theme_bw()