【问题标题】:Ballon plot by using ggplot2使用 ggplot2 的气球图
【发布时间】:2020-08-27 11:21:01
【问题描述】:

我使用 ggplot2 做了一个气球图。

代码如下:

p6 <- ggplot(balloon, aes(x = factor(Clusters,level = level_cluster), y =  factor(Gene_names, level = level_gene), size = log(counts), fill= Clusters)) + scale_fill_manual(values = col_val) + geom_point(shape = 21) + theme_bw() + theme() + scale_size_area(max_size=18) + labs(x = "Cell clusters", y = "Genes") + theme(axis.text.x = element_text(color = "black", size = 15, angle = 90, hjust = .5, vjust = .5, face = "plain"),axis.text.y = element_text(color = "black", size = 15, angle = 0, hjust = 1, vjust = 0, face = "italic"),axis.title.x = element_text(color = "black", size = 18, angle = 0, hjust = .5, vjust = 0, face = "bold"),axis.title.y = element_text(color = "black", size = 18, angle = 90, hjust = .5, vjust = .5, face = "bold"))+ theme(plot.title = element_text(color = "black", size = 20, hjust = .5,face = "bold"))

现在我想用灰色更改较小圆圈(

我试过了:

balloon$new= if(balloon$counts <1000) {
"grey"
} else {
balloon$Clusters
}}

这是我的input matrix

ENSORLG00000003553  lhb 1. Red blood cells  34.74336723
ENSORLG00000013460  smtla   1. Red blood cells  3.848007571
ENSORLG00000016928  prl 1. Red blood cells  51.87633598
ENSORLG00000019556  gh1 1. Red blood cells  15.49006269
ENSORLG00000025908  pomc    1. Red blood cells  82.35566565
ENSORLG00000029237  fshb    1. Red blood cells  8.976657006
ENSORLG00000029251  tshb    1. Red blood cells  1.263917951
ENSORLG00000003553  lhb 2. Lhb gonadotropes 638.014105
ENSORLG00000013460  smtla   2. Lhb gonadotropes 1.035214771
ENSORLG00000016928  prl 2. Lhb gonadotropes 17.50443047
ENSORLG00000019556  gh1 2. Lhb gonadotropes 7.964696992
ENSORLG00000025908  pomc    2. Lhb gonadotropes 35.79923337
ENSORLG00000029237  fshb    2. Lhb gonadotropes 13.19827063
ENSORLG00000029251  tshb    2. Lhb gonadotropes 8.964227329
ENSORLG00000003553  lhb 3. Fshb gonadotropes    112.997567
ENSORLG00000013460  smtla   3. Fshb gonadotropes    4.246600527
ENSORLG00000016928  prl 3. Fshb gonadotropes    23.9549137
ENSORLG00000019556  gh1 3. Fshb gonadotropes    7.855706367
ENSORLG00000025908  pomc    3. Fshb gonadotropes    26.71335932
ENSORLG00000029237  fshb    3. Fshb gonadotropes    768.3662698
ENSORLG00000029251  tshb    3. Fshb gonadotropes    3.223137519
ENSORLG00000003553  lhb 4. Gonadotropes 184.5634708
ENSORLG00000013460  smtla   4. Gonadotropes 1.289546532
ENSORLG00000016928  prl 4. Gonadotropes 20.10841968
ENSORLG00000019556  gh1 4. Gonadotropes 8.591053124
ENSORLG00000025908  pomc    4. Gonadotropes 28.51698806
ENSORLG00000029237  fshb    4. Gonadotropes 18.26448329
ENSORLG00000029251  tshb    4. Gonadotropes 11.53072354
ENSORLG00000003553  lhb 5. Thyrotropes  113.4819895
ENSORLG00000013460  smtla   5. Thyrotropes  1.63480032
ENSORLG00000016928  prl 5. Thyrotropes  49.81194451
ENSORLG00000019556  gh1 5. Thyrotropes  8.206770961
ENSORLG00000025908  pomc    5. Thyrotropes  105.7635502
ENSORLG00000029237  fshb    5. Thyrotropes  6.854720252
ENSORLG00000029251  tshb    5. Thyrotropes  799.7511818
ENSORLG00000003553  lhb 6. Somatotrope  21.50675227
ENSORLG00000013460  smtla   6. Somatotrope  1.068697038
ENSORLG00000016928  prl 6. Somatotrope  22.46420742
ENSORLG00000019556  gh1 6. Somatotrope  2120.237219
ENSORLG00000025908  pomc    6. Somatotrope  23.02674117
ENSORLG00000029237  fshb    6. Somatotrope  2.993651099
ENSORLG00000029251  tshb    6. Somatotrope  1.715546068
ENSORLG00000003553  lhb 7. Somatolactotrope 41.54099806
ENSORLG00000013460  smtla   7. Somatolactotrope 682.2629265
ENSORLG00000016928  prl 7. Somatolactotrope 53.59281521
ENSORLG00000019556  gh1 7. Somatolactotrope 15.34521368
ENSORLG00000025908  pomc    7. Somatolactotrope 29.28258596
ENSORLG00000029237  fshb    7. Somatolactotrope 5.234986568
ENSORLG00000029251  tshb    7. Somatolactotrope 0.364096592
ENSORLG00000003553  lhb 8. Melanotrope  48.98548937
ENSORLG00000013460  smtla   8. Melanotrope  0.830762765
ENSORLG00000016928  prl 8. Melanotrope  15.55076759
ENSORLG00000019556  gh1 8. Melanotrope  8.122592754
ENSORLG00000025908  pomc    8. Melanotrope  5989.114015
ENSORLG00000029237  fshb    8. Melanotrope  5.783475514
ENSORLG00000029251  tshb    8. Melanotrope  4.953669347
ENSORLG00000003553  lhb 9. Corticotrope 34.1794879
ENSORLG00000013460  smtla   9. Corticotrope 11.95184071
ENSORLG00000016928  prl 9. Corticotrope 16.34014418
ENSORLG00000019556  gh1 9. Corticotrope 7.093359738
ENSORLG00000025908  pomc    9. Corticotrope 2559.13673
ENSORLG00000029237  fshb    9. Corticotrope 4.47597778
ENSORLG00000029251  tshb    9. Corticotrope 1.09924108
ENSORLG00000003553  lhb 10. Uncharacterized 38.32091755
ENSORLG00000013460  smtla   10. Uncharacterized 1.639358903
ENSORLG00000016928  prl 10. Uncharacterized 18.8924994
ENSORLG00000019556  gh1 10. Uncharacterized 8.50135886
ENSORLG00000025908  pomc    10. Uncharacterized 24.48569194
ENSORLG00000029237  fshb    10. Uncharacterized 2.61133141
ENSORLG00000029251  tshb    10. Uncharacterized 0.892928105
ENSORLG00000003553  lhb 11. Lactotrope(a)   16.21510607
ENSORLG00000013460  smtla   11. Lactotrope(a)   1.309625312
ENSORLG00000016928  prl 11. Lactotrope(a)   3283.647179
ENSORLG00000019556  gh1 11. Lactotrope(a)   8.109442712
ENSORLG00000025908  pomc    11. Lactotrope(a)   27.9230953
ENSORLG00000029237  fshb    11. Lactotrope(a)   2.786889655
ENSORLG00000029251  tshb    11. Lactotrope(a)   6.190816972
ENSORLG00000003553  lhb 12. Lactotrope(b)   15.33239486
ENSORLG00000013460  smtla   12. Lactotrope(b)   1.086630029
ENSORLG00000016928  prl 12. Lactotrope(b)   1535.65747
ENSORLG00000019556  gh1 12. Lactotrope(b)   16.96554153
ENSORLG00000025908  pomc    12. Lactotrope(b)   24.35316354
ENSORLG00000029237  fshb    12. Lactotrope(b)   3.039600622
ENSORLG00000029251  tshb    12. Lactotrope(b)   0.823361197
ENSORLG00000003553  lhb 13. Uncharacterized 20.29313474
ENSORLG00000013460  smtla   13. Uncharacterized 1.661208282
ENSORLG00000016928  prl 13. Uncharacterized 21.93434899
ENSORLG00000019556  gh1 13. Uncharacterized 9.657225297
ENSORLG00000025908  pomc    13. Uncharacterized 34.16894514
ENSORLG00000029237  fshb    13. Uncharacterized 10.40412352
ENSORLG00000029251  tshb    13. Uncharacterized 1.58639062
ENSORLG00000003553  lhb 14. Uncharacterized 16.95617254
ENSORLG00000013460  smtla   14. Uncharacterized 1.416815915
ENSORLG00000016928  prl 14. Uncharacterized 35.01516622
ENSORLG00000019556  gh1 14. Uncharacterized 8.4452641
ENSORLG00000025908  pomc    14. Uncharacterized 28.82699429
ENSORLG00000029237  fshb    14. Uncharacterized 3.603384629
ENSORLG00000029251  tshb    14. Uncharacterized 2.736829128
ENSORLG00000003553  lhb 15. Uncharacterized 34.74308946
ENSORLG00000013460  smtla   15. Uncharacterized 1.125770862
ENSORLG00000016928  prl 15. Uncharacterized 34.93068863
ENSORLG00000019556  gh1 15. Uncharacterized 9.752369461
ENSORLG00000025908  pomc    15. Uncharacterized 28.89137686
ENSORLG00000029237  fshb    15. Uncharacterized 4.028765498
ENSORLG00000029251  tshb    15. Uncharacterized 14.89174878

【问题讨论】:

    标签: r ggplot2 balloon


    【解决方案1】:

    您发布的数据集不足以复制您的绘图;但是,我将向您展示一个解决方案,其中我修改了 mtcars 数据集,为我们提供可用于生成与您自己的相似图的数据。

    示例图

    这个想法是在mtcars 数据集中随机复制行,然后让每个复制的计数与点几何的大小相关。下面是生成数据和基础图的代码:

    library(ggplot2)
    library(dplyr)
    library(tidyr)
    
    # dataset stuff
    set.seed(1234)
    mtcars_1 <- mtcars
    mtcars_1$model <- rownames(mtcars)
    df <- mtcars_1[sample(1:nrow(mtcars), 5000, replace=TRUE),]
    df <- df %>% group_by(model, disp, mpg) %>% tally()
    
    # basic plot
    p <- ggplot(df, aes(disp, mpg)) + theme_bw() +
      geom_point(aes(fill=model, size=n), shape=21)
    p
    

    颜色 n

    根据数据集df,似乎150 是n 的合理截止值,以便看到一些灰色点,请注意。我们会将n &lt;= 150 的任何值着色为灰色,但根据df$n 的级别保持它们的大小不同。

    为此,最简单的方法是简单地使用两个geom_point() 层:一层将应用于df$n &gt; 150 所在的数据集,另一层将应用于df$n &lt;= 150 所在的数据集。这样做最有意义,因为我们希望 fill= 的美学值映射到数据集的一部分,而 fill= 的美学值对于其他部分是一个常数。

    p1 <- ggplot(df, aes(disp, mpg)) + theme_bw() +
      geom_point(data=df[which(df$n <= 150),],
                 aes(size=n), fill='gray', color='gray', shape=21) +
      geom_point(data=df[which(df$n > 150),],
                 aes(fill=model, size=n), shape=21) +
      guides(fill=guide_legend(ncol=2))
    p1
    

    修复图例以使情节看起来更好

    您可能想做一些与图例相关的可选操作。我想改变的包括:

    • 增加图例键字形(符号)的大小。这些对我来说有点小,但可以通过覆盖guide_legend(override.aes=list(size=... 中的美学来增加。

    • 为我们的“灰色”点添加标签。 由于color= 美学没有映射到我们数据集中的任何内容,我们可以将字符串映射到“灰色”中的aes(color= geom_point() 打电话。这会将字符串添加到图例中,并将其与图例分开以进行填充(因为它与颜色相关联)。然后我们需要为该颜色提供values=(提示:它是灰色的),否则我们将使用带有红色轮廓的灰色圆圈并删除图例的标题。

    • 调整尺寸图例的方向。 为了更好地“适应”,我将使用guide_legend() 调整尺寸美学图例的方向和标题位置。这样一来,剧情就更贴合了。

    以下是代码中的这些调整以及结果图。我对这个很满意:

    p2 <- ggplot(df, aes(disp, mpg)) + theme_bw() +
      geom_point(data=df[which(df$n <= 150),],
                 aes(size=n, color="Lower than 150"), fill='gray', shape=21) +
      geom_point(data=df[which(df$n > 150),],
                 aes(fill=model, size=n), shape=21) +
      scale_color_manual(values='gray') +
      guides(
        fill=guide_legend(ncol=2, order=2, override.aes = list(size=4)),
        size=guide_legend(direction="horizontal", title.position="top", order=1),
        color=guide_legend(title=NULL, order=3, override.aes = list(size=4))
      )
    p2
    

    【讨论】:

    • 非常感谢您的详细解答。这真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多