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

问题描述 投票:1回答:4

我刚刚遇到这样的图形,其中使用了两种颜色的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()

任何人都应该有我应该研究的线索?谢谢!

enter image description here

r ggplot2 graph-visualization
4个回答
1
投票

我有一种方法可以获得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()

enter image description here


1
投票

这是一个淤泥。我此刻没有时间弄清楚这个传说。请注意,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")

enter image description here


0
投票
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()

enter image description here


-1
投票

我想你正在寻找R的泡沫情节

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

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

© www.soinside.com 2019 - 2024. All rights reserved.