Search code examples
rbubble-chart

how to centralize bubble in faced grid


I need help distributing the bubbles within each facet grid and removing the grid lines inside each facet grid in the graphic below.

graph <- ggplot(patog1, aes(x = Prevalence, y = Microorganism)) +
  geom_point(data=patog1,aes(x=JitCoOr, y=JitCoOrPow,size = Samples, colour = Study), alpha=.5)+
  geom_text(data=patog1,aes(x=JitCoOr, y=JitCoOrPow,label=Samples)) + 
  scale_size(range = c(1,30))+
  scale_x_continuous(breaks = scales::breaks_width(10))+ 
  facet_grid(Microorganism~.)+
  theme_bw()
graph

graph + theme(axis.text.y=element_blank(), 
             axis.ticks.y=element_blank())

I also would like to have the name of the Microorganisms - the faced in the left side.

Here is my dataframe

> dput(head(patog1, 20))
structure(list(Microorganism = c("Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci aureus", "Staphylococci aureus", "Staphylococci aureus", 
"Staphylococci aureus", "Staphylococci aureus", "Staphylococci aureus", 
"Staphylococci aureus"), Prevalence = c(23.9, 58.2, 9.3, 56, 
56, 45, 23.2, 55.7, 56.4, 15.3, 44, 38.9, 34.5, 1.6, 4.3, 10.3, 
3.9, 10, 8, 0.4), Samples = c(307, 424, 165, 150, 152, 59, 72, 
173, 191, 1077, 200, 185, 808, 307, 424, 165, 290, 59, 661, 1077
), Study = c("1", "3", "4", "6", "17", "10", "11", "13", "14", 
"15", "16", "20", "18", "1", "3", "4", "9", "10", "12", "15"), 
    radius = c(2.75818894925504, 4.30414165379076, 1.72054698904426, 
    4.22200824564475, 4.22200824564475, 3.78469878303024, 2.7174968922639, 
    4.21068410836495, 4.23706001618643, 2.20683965403289, 3.74241031850956, 
    3.5188427888369, 3.31386346630949, 0.713649646461108, 1.16992842113964, 
    1.81068821935005, 1.11418515342684, 1.78412411615277, 1.59576912160573, 
    0.356824823230554), JitCoOr = c(52.9537293797359, 62.0532565008849, 
    39.0785944228992, 59.9366020258516, 60.044047411345, 56.9612927732058, 
    51.9691243943758, 58.8738514398225, 60.8206247391179, 47.8702185059898, 
    56.0192426254973, 55.199258433003, 54.0486559784971, 5.81677224850282, 
    21.9080965784378, 42.9893445299938, 19.90970782144, 42.1101505364291, 
    36.0588588385843, 0.817146013118327), JitCoOrPow = c(9.03914307709783, 
    9.00067050699145, 9.1362363233231, 8.83267976436764, 9.05750821055844, 
    9.07773876339197, 9.19675751486793, 8.87710817120969, 8.98355117850006, 
    8.83994870837778, 8.99074420211837, 9.14479981157929, 8.90359472669661, 
    7.87938763340935, 7.89290500981733, 8.15278417477384, 8.00291525470093, 
    8.13870471678674, 7.93727878862992, 7.83678827155381)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))
> dput(head(patog1, 100))
structure(list(Microorganism = c("Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci coagulase negative", "Staphylococci coagulase negative", 
"Staphylococci aureus", "Staphylococci aureus", "Staphylococci aureus", 
"Staphylococci aureus", "Staphylococci aureus", "Staphylococci aureus", 
"Staphylococci aureus", "Staphylococci aureus", "Staphylococci aureus", 
"Staphylococci aureus", "Klebsiella pneumoniae", "Klebsiella pneumoniae", 
"Klebsiella pneumoniae", "Klebsiella pneumoniae", "Klebsiella pneumoniae", 
"Klebsiella pneumoniae", "Escherichia coli", "Escherichia coli", 
"Escherichia coli", "Escherichia coli", "Escherichia coli", "Escherichia coli", 
"Escherichia coli", "Escherichia coli", "Escherichia coli", "Pseudomonas aeruginosa", 
"Pseudomonas aeruginosa", "Pseudomonas aeruginosa", "Pseudomonas aeruginosa", 
"Pseudomonas aeruginosa", "Pseudomonas aeruginosa", "Acinetobacter baumanni", 
"Acinetobacter baumanni", "Acinetobacter baumanni", "Acinetobacter baumanni", 
"Acinetobacter baumanni", "Acinetobacter baumanni", "Enterobacter spp", 
"Enterobacter spp", "Enterobacter spp", "Enterobacter spp", "Enterococcus spp", 
"Enterococcus spp", "Enterococcus spp", "Enterococcus spp", "Enterococcus spp", 
"Enterococcus spp", "Fungal", "Fungal", "Fungal", "Fungal", "Fungal", 
"Fungal", "Fungal", "Fungal", "Fungal", "Fungal", "Fungal", "Fungal", 
"Fungal", "Fungal", "Fungal", "Fungal"), Prevalence = c(23.9, 
58.2, 9.3, 56, 56, 45, 23.2, 55.7, 56.4, 15.3, 44, 38.9, 34.5, 
1.6, 4.3, 10.3, 3.9, 10, 8, 0.4, 7.2, 3.7, 2.4, 2.9, 15, 5, 4.1, 
2, 7.6, 6.5, 7.7, 9.6, 16.8, 22, 10, 1.7, 2, 4.5, 6.5, 4.9, 15, 
3, 1.5, 5.3, 3.2, 0.9, 1, 7.1, 2, 4.7, 7, 2.1, 1, 1.8, 5.9, 5, 
12, 22.4, 3.9, 3.78, 11.4, 0.86, 5.8, 3.6, 3.1, 48, 3.7, 8.6, 
5, 15, 2.6, 14.4, 8.1, 1.7, 2.1, 9.9), Samples = c(307, 424, 
165, 150, 152, 59, 72, 173, 191, 1077, 200, 185, 808, 307, 424, 
165, 290, 59, 661, 1077, 152, 185, 808, 290, 59, 661, 1077, 152, 
808, 307, 424, 165, 290, 661, 173, 1077, 152, 808, 307, 290, 
59, 661, 1077, 808, 307, 290, 661, 1077, 152, 808, 661, 1077, 
152, 808, 290, 59, 661, 1077, 808, 185, 307, 1273, 424, 165, 
397, 150, 214, 70, 59, 661, 191, 1077, 808, 474, 185, 290), Study = c("1", 
"3", "4", "6", "17", "10", "11", "13", "14", "15", "16", "20", 
"18", "1", "3", "4", "9", "10", "12", "15", "17", "29", "18", 
"9", "10", "12", "15", "17", "18", "1", "3", "4", "9", "12", 
"13", "15", "17", "18", "1", "9", "10", "12", "15", "18", "1", 
"9", "12", "15", "17", "18", "12", "15", "17", "18", "9", "10", 
"12", "15", "18", "20", "1", "2", "3", "4", "5", "6", "7", "8", 
"10", "12", "14", "15", "18", "21", "20", "9"), radius = c(2.75818894925504, 
4.30414165379076, 1.72054698904426, 4.22200824564475, 4.22200824564475, 
3.78469878303024, 2.7174968922639, 4.21068410836495, 4.23706001618643, 
2.20683965403289, 3.74241031850956, 3.5188427888369, 3.31386346630949, 
0.713649646461108, 1.16992842113964, 1.81068821935005, 1.11418515342684, 
1.78412411615277, 1.59576912160573, 0.356824823230554, 1.5138795132121, 
1.08524033231355, 0.874038744473663, 0.960780240186585, 2.18509686118416, 
1.26156626101008, 1.14239683707263, 0.797884560802865, 1.55536334500875, 
1.4384068479379, 1.5655625581928, 1.74807748894733, 2.31248915411244, 
2.64628371420061, 1.78412411615277, 0.735613218010963, 0.797884560802865, 
1.1968268412043, 1.4384068479379, 1.24888688130694, 2.18509686118416, 
0.97720502380584, 0.690988298942671, 1.29886196217077, 1.00925300880806, 
0.535237234845831, 0.564189583547756, 1.50332970166391, 0.797884560802865, 
1.22313387045892, 1.49270533036046, 0.817588381146626, 0.564189583547756, 
0.756939756606048, 1.37041173684567, 1.26156626101008, 1.95441004761168, 
2.67023247124982, 1.11418515342684, 1.09690991871472, 1.90492327994993, 
0.523207895695449, 1.35874844613195, 1.07047446969166, 0.993358267278101, 
3.90882009522336, 1.08524033231355, 1.65452864018143, 1.26156626101008, 
2.18509686118416, 0.909728368293446, 2.14094893938333, 1.60571170453749, 
0.735613218010963, 0.817588381146626, 1.77518108181096), JitCoOr = c(52.9537293797359, 
62.0532565008849, 39.0785944228992, 59.9366020258516, 60.044047411345, 
56.9612927732058, 51.9691243943758, 58.8738514398225, 60.8206247391179, 
47.8702185059898, 56.0192426254973, 55.199258433003, 54.0486559784971, 
5.81677224850282, 21.9080965784378, 42.9893445299938, 19.90970782144, 
42.1101505364291, 36.0588588385843, 0.817146013118327, 33.0946799211204, 
17.9733058359474, 10.9837412635796, 13.1981137231924, 46.8427434850484, 
25.8778913124464, 21.0825900368392, 8.82317104134709, 33.9438152883202, 
30.1352072700858, 34.9239843650721, 40.019158882089, 49.1305902745575, 
50.1613460198045, 42.1171545682475, 6.91049184789881, 9.04398515755311, 
22.9568010552786, 30.0390831185505, 24.844200709369, 47.1902362979949, 
13.9909223258495, 5.12252249410376, 26.8357132209465, 16.1405700452626, 
2.98700527055189, 3.95436205444857, 31.8687793305144, 9.10231541376561, 
23.8016606531106, 30.8522547448985, 10.0403863609768, 3.96650114795193, 
8.12822173433378, 29.0722099646926, 26.0967087958008, 44.9233711795881, 
50.8655211300589, 20.1719161574729, 19.0288566277362, 43.9595239535905, 
1.81553317187354, 27.9933262486011, 17.151779101789, 14.8274631401524, 
57.9104666036554, 17.9814745260403, 37.9474771942943, 26.0181835262105, 
46.9132438350469, 11.9454135867767, 45.8337836777791, 37.0391653109342, 
6.89612202439457, 9.85659030899405, 41.1932372558862), JitCoOrPow = c(9.03914307709783, 
9.00067050699145, 9.1362363233231, 8.83267976436764, 9.05750821055844, 
9.07773876339197, 9.19675751486793, 8.87710817120969, 8.98355117850006, 
8.83994870837778, 8.99074420211837, 9.14479981157929, 8.90359472669661, 
7.87938763340935, 7.89290500981733, 8.15278417477384, 8.00291525470093, 
8.13870471678674, 7.93727878862992, 7.83678827155381, 8.0910329354927, 
8.12914157127961, 7.80564800221473, 6.13817321993411, 5.90554165812209, 
5.82447278564796, 5.95850338637829, 5.84010715493932, 5.97392952665687, 
4.16093768849969, 3.86766330143437, 4.12012600041926, 4.07185018146411, 
4.04207222368568, 3.91818653987721, 3.88875154945999, 4.10133589757606, 
4.04486549412832, 7.08866870719939, 7.1921757302247, 7.09146734382957, 
6.84114244831726, 7.1966904586181, 6.90712704788893, 1.14620719207451, 
0.880739877559245, 0.83798291767016, 1.08403717856854, 0.876667608134449, 
1.06033392539248, 1.95018634768203, 1.90881395954639, 2.05503583196551, 
2.00373990433291, 3.04555658176541, 2.84971538251266, 3.0246863745153, 
3.17671390501782, 3.10960682872683, 2.81259256033227, 4.99736648378894, 
5.17767235841602, 4.97637547263876, 4.90573497125879, 4.84745719758794, 
5.02040644623339, 4.90035738507286, 5.14835271695629, 4.84300309810787, 
4.97609931230545, 4.96553065059707, 5.02887920197099, 5.17221848322079, 
5.07821689154953, 5.17920738002285, 5.10063524739817)), row.names = c(NA, 
-76L), class = c("tbl_df", "tbl", "data.frame"))

my graph after the help


Solution

  • Gridlines can be removed with theme(panel.grid.minor = element_blank()), possibly panel.grid.major= as well or just panel.grid=. See ?theme, namely:

    panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x,
              panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y:
              grid lines (element_line()). Specify major grid lines, or
              minor grid lines separately (using ‘panel.grid.major’ or
              ‘panel.grid.minor’) or individually for each axis (using
              ‘panel.grid.major.x’, ‘panel.grid.minor.x’,
              ‘panel.grid.major.y’, ‘panel.grid.minor.y’).  Y axis grid
              lines are horizontal and x axis grid lines are vertical.
              panel.grid.*.* inherits from panel.grid.* which inherits from
              ‘panel.grid’, which in turn inherits from ‘line’
    

    Switch facet strips from right to left is handled with facet_grid(..., switch=""y"). See ?facet_grid, namely

      switch: By default, the labels are displayed on the top and right of
              the plot. If ‘"x"’, the top labels will be displayed to the
              bottom. If ‘"y"’, the right-hand side labels will be
              displayed to the left. Can also be set to ‘"both"’.
    

    For "centering" the bubbles, it's really hard without sample data, but I suspect you can use scales="free_y".

    Perhaps this:

    graph <- ggplot(patog1, aes(x = Prevalence, y = Microorganism)) +
      geom_point(data=patog1,aes(x=JitCoOr, y=JitCoOrPow,size = Samples, colour = Study), alpha=.5)+
      geom_text(data=patog1,aes(x=JitCoOr, y=JitCoOrPow,label=Samples)) + 
      scale_size(range = c(1,30))+
      scale_x_continuous(breaks = scales::breaks_width(10))+ 
      facet_grid(Microorganism~., scales = "free_y", switch = "y")+
      theme_bw()
    graph +
      theme(
        axis.text.y=element_blank(), 
        axis.ticks.y=element_blank(),
        panel.grid = element_blank()
      )
    

    Update with sample data.

    We can add scale_y_continuous(expand = expansion(mult = 1.5)) to expand the y-axis a bit. The 1.5 is subjective, over to you. (There's also the add= argument, see ?expansion for details.)

    One tweak, since I find your long Microorganism names may be a pain: I'm switching from facet_grid to facet_wrap(..., ncol = 1), which means we need to remove switch= and instead use strip.position="top". It's just a suggestion, take it or leave it.

    Another tweak, though the effective change is only efficiency and code-golf: since you specify data=patog1 in the call to ggplot(.) and use the same data=patog1 in the geom_* calls, you can remove the geom-data calls, they will use the same data.

    ggplot(patog1, aes(x = Prevalence, y = Microorganism)) +
      geom_point(aes(x = JitCoOr, y = JitCoOrPow, size = Samples, colour = Study), alpha = 0.5) +
      geom_text(aes(x = JitCoOr, y = JitCoOrPow, label = Samples)) + 
      scale_size(range = c(1, 30)) +
      scale_x_continuous(breaks = scales::breaks_width(10)) + 
      scale_y_continuous(expand = expansion(mult = 1.5)) + 
      facet_wrap(Microorganism ~ ., ncol = 1, scales = "free_y", strip.position = "top") +
      theme_bw() +
      theme(
        axis.text.y=element_blank(), 
        axis.ticks.y=element_blank(),
        panel.grid = element_blank()
      )
    

    ggplot with centered bubbles

    Since you really want to use facet_grid, I think it can't work well without either (a) substantially shrinking the font, or (b) replace spaces with newlines. I'll demo #2:

    transform(patog1, Microorganism = gsub(" ", "\n", Microorganism)) |>
      ggplot(aes(x = Prevalence, y = Microorganism)) +
      geom_point(aes(x = JitCoOr, y = JitCoOrPow, size = Samples, colour = Study), alpha = 0.5) +
      geom_text(aes(x = JitCoOr, y = JitCoOrPow, label = Samples)) + 
      scale_size(range = c(1, 30)) +
      scale_x_continuous(breaks = scales::breaks_width(10)) + 
      scale_y_continuous(expand = expansion(mult = 1.5)) + 
      facet_grid(Microorganism ~ ., scales = "free_y", switch = "y") +
      theme_bw() +
      theme(
        axis.text.y=element_blank(), 
        axis.ticks.y=element_blank(),
        panel.grid = element_blank()
      )
    

    (and a slightly taller graphics window)

    same graphic, using facet_grid