更改连续气泡图ggplot的填充颜色

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

我想改变图中气泡的颜色

ggplot(sum_length, 
       aes(x=vb_width, y=slope, size = length)) +
    geom_point(alpha=0.5)+
   theme_classic() +
  ylab("Slope category (%)") +
  xlab("Valley bottom width (m)") +
  scale_y_discrete(labels= slopebins)+
  scale_size(range = c(.1, 12), name="Amount of occupied\n habitat (km)")

所以,如果可能的话,我希望每个尺寸也有不同的颜色,在 viridis 调色板上。我无法使用此解决方案,因为我的数据的大小值是连续的,并且例如它们的数据是离散的:在 R 中编辑 ggplot2 气泡图:大小、颜色和标签

数据:

  slope vb_width MU      length
1   1to3     <35m 21  26.0568635
2  3to10     <35m 21 133.8732929
3   gt10     <35m 21  49.9302440
4    lt1     <35m 21  13.6335112
5   1to3 >285-427 21   9.5806268
6  3to10 >285-427 21   0.3888544
7   gt10 >285-427 21   0.0000000
8    lt1 >285-427 21  24.3259779
9   1to3   >35-70 21  66.0737107
10 3to10   >35-70 21 140.3568265
11  gt10   >35-70 21  23.9712848
12   lt1   >35-70 21  42.4018746
13  1to3     >427 21   9.0315019
14 3to10     >427 21   0.3993908
15  gt10     >427 21   0.0000000
16   lt1     >427 21 163.8209682
17  1to3  >70-285 21  72.3702302
18 3to10  >70-285 21  38.4964008
19  gt10  >70-285 21   7.9305055
20   lt1  >70-285 21  85.7479024
r ggplot2 fill bubble-chart
1个回答
0
投票

一些小调整:

  • fill
    美学添加变量(添加
    fill = length
  • geom_point()
    中的形状更改为可以填充的形状(添加
    shape = 21
  • 添加
    scale_fill_viridis_c()
    将气泡的颜色更改为 viridis 调色板

例如

library(tidyverse)

df <- read.table(text = "  slope vb_width MU      length
1   1to3     <35m 21  26.0568635
2  3to10     <35m 21 133.8732929
3   gt10     <35m 21  49.9302440
4    lt1     <35m 21  13.6335112
5   1to3 >285-427 21   9.5806268
6  3to10 >285-427 21   0.3888544
7   gt10 >285-427 21   0.0000000
8    lt1 >285-427 21  24.3259779
9   1to3   >35-70 21  66.0737107
10 3to10   >35-70 21 140.3568265
11  gt10   >35-70 21  23.9712848
12   lt1   >35-70 21  42.4018746
13  1to3     >427 21   9.0315019
14 3to10     >427 21   0.3993908
15  gt10     >427 21   0.0000000
16   lt1     >427 21 163.8209682
17  1to3  >70-285 21  72.3702302
18 3to10  >70-285 21  38.4964008
19  gt10  >70-285 21   7.9305055
20   lt1  >70-285 21  85.7479024
", header = TRUE)

df %>%
  ggplot(aes(x=vb_width, y=slope, size = length, fill = length)) +
  geom_point(alpha=0.5, shape = 21)+
  theme_classic() +
  ylab("Slope category (%)") +
  xlab("Valley bottom width (m)") +
  #scale_y_discrete(labels=slopebins)+
  scale_size(range = c(.1, 12), name="Amount of occupied\n habitat (km)") +
  scale_fill_viridis_c()

创建于 2023 年 11 月 29 日,使用 reprex v2.0.2

这是你想要的结果吗?

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