[英]Ballon plot by using ggplot2
I made a ballon plot using ggplot2.我使用 ggplot2 制作了一个气球图。
Here is the code:这是代码:
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"))
Now I want to change the color of smaller circles (<1000 counts) with grey and the rest circles would be in the same colors as they are.现在我想用灰色更改较小圆圈(<1000 计数)的颜色,其余圆圈的颜色将与它们相同。
I tried this:我试过这个:
balloon$new= if(balloon$counts <1000) {
"grey"
} else {
balloon$Clusters
}}
This is my 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
The dataset you have posted is not enough to replicate you plot;您发布的数据集不足以复制您的绘图; however, I will show you a solution wherein I modify the
mtcars
dataset to give us data that can be used to generate a similar plot to you own.但是,我将向您展示一个解决方案,其中我修改了
mtcars
数据集,为我们提供可用于生成与您拥有的相似图的数据。
The idea is to replicate rows randomly in the mtcars
dataset, and then have the count for each replication relate to the size of the point geom.这个想法是在
mtcars
数据集中随机复制行,然后让每次复制的计数与点几何的大小相关。 Here's the generation of the data and base plot code:这是数据和基本绘图代码的生成:
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
Based on the dataset df
, it seems that 150 is a reasonable cutoff for n
in order to see some points gray, some note.根据数据集
df
,似乎 150 是n
的合理截止值,以便看到一些灰色点,请注意。 We'll color any values where n <= 150
to be gray, but keep their sizes different according to the level of df$n
.我们会将
n <= 150
任何值着色为灰色,但根据df$n
的级别保持它们的大小不同。
To do this, the simplest approach is to simply use two geom_point()
layers: one layer will be applied to the dataset where df$n > 150
and the other will apply to the dataset where df$n <= 150
.为此,最简单的方法是简单地使用两个
geom_point()
层:一层将应用于df$n > 150
的数据集,另一层将应用于df$n <= 150
的数据集。 It makes the most sense to do it this way, since we want the aesthetics value of fill=
mapped to a portion of the dataset and the aesthetic for fill=
to be a constant for the other portion.这是很有道理的这种方式来做到这一点,因为我们希望的美学价值
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
You may want to do a few optional things related to the legends.您可能想做一些与图例相关的可选操作。 What I would like to change include:
我想改变的包括:
Increase the size of the legend key glyphs (symbols).增加图例键字形(符号)的大小。 These are a bit small for my taste, but can be increased by overriding the aesthetics within
guide_legend(override.aes=list(size=...
.这些对我来说有点小,但可以通过覆盖
guide_legend(override.aes=list(size=...
.
Include a label for our "gray" dots.包括我们的“灰色”点的标签。 Since the
color=
aesthetic is not mapped to anything in our dataset, we can map a character string to aes(color=
within the "gray" geom_point()
call. This adds that string to the legend, and it will separate that out from the legend for fill (since it's associated with the color). We will then need to provide the values=
for that color (hint: it's gray), or we'll have gray circles with red outlines and remove the title of the legend.由于
color=
美学没有映射到我们数据集中的任何东西,我们可以将一个字符串映射到aes(color=
在 "gray" geom_point()
调用中。这会将该字符串添加到图例中,并将其与填充的图例(因为它与颜色相关联)。然后我们需要为该颜色提供values=
(提示:它是灰色的),或者我们将使用带有红色轮廓的灰色圆圈并删除图例的标题。
Adjust direction of the size legend.调整尺寸图例的方向。 To "fit" better, I'll adjust using
guide_legend()
the direction and title placement for the size aesthetic legend.为了更好地“适应”,我将使用
guide_legend()
调整尺寸美学图例的方向和标题位置。 This way things fit a bit nicer on the plot.这样事情就更适合情节了。
Here's those adjustments within the code and the resulting plot.这是代码中的那些调整和结果图。 I'm happy with this one:
我对这个很满意:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.