0

I made a ballon plot using 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"))

p6

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.

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
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57

1 Answers1

1

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.

Example Plot

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. 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

enter image description here

Color n < 150 gray

Based on the dataset df, it seems that 150 is a reasonable cutoff for n in order to see some points gray, some note. We'll color any values where n <= 150 to be gray, but keep their sizes different according to the level of 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. 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.

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

enter image description here

Fixing Legends to make the plot look nicer

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=....

  • 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.

  • 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. 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

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32