Dear all,
I want to ask a very basic question. I am making a bubble plot using ggplot2 having table structure:

       GO term    Number    Class      Type
       1. Metabolism    5     start duf    BP
       2. Photosynthesis 10   hzs          BP
       3. Nucleus      15     hs           CC
       4. Kinase       16     hs           MF

I want to make a bubble plot having Number on x axis, GO term on y axis, bubble color should be based on Class and background color will be based on Type.
My R code is:

> ggplot(bubble_plot, aes(x=Number, y=GO term, size = Number, col =
> Class)) +   geom_point(alpha=0.7)

With this I am getting the desired plot except the background color, When I am using "fill = Type", I am not getting the background color based on the BP, CC or MF based on the " Type" column in the table.

The desired plot should look like:


<a href="https://ibb.co/TPDT8jD"><img src="https://i.ibb.co/h2xMWQx/Screen-Shot-2020-09-26-at-12-32-16-PM.png" alt="Screen-Shot-2020-09-26-at-12-32-16-PM" border="0" /></a>

Please help.

Thanks in advance
So with your data that seems to look something like this:

    structure(list(GO_term = structure(c(2L, 4L, 3L, 1L), .Label = c("Kinase", 
    "Metabolism", "Nucleus", "Photosynthesis"), class = "factor"), 
        Number = c(5L, 10L, 15L, 16L), Class = structure(c(3L, 2L, 
        1L, 1L), .Label = c("hs", "hzs", "start_duf"), class = "factor"), 
        Type = structure(c(1L, 1L, 2L, 3L), .Label = c("BP", "CC", 
        "MF"), class = "factor")), class = "data.frame", row.names = c(NA, 
    -4L))

You could work with `geom_tile` and set its `width = Inf` to get something akin to that plot you're trying to emulate. The use of `forcats::reorder()` within `aes()` is important in order to group the Y-axis values together on the basis of your `Type` column.

    library(ggplot2)
    library(forcats)
    
    ggplot(mydat, aes(y = reorder(GO_term, as.numeric(Type)), x = Number, size = Number)) + geom_point(aes(color = Class), alpha = 1.0) + 
      geom_tile(aes(width = Inf, fill = Type), alpha = 0.4) + 
      scale_fill_manual(values = c("green", "red", "blue"))

Which yields:
![col_by_bg2.png][1]

The problem you'd probably run into is having to pass the appropriate number of colors to `scale_fill_manual` for your actual dataset.


  [1]: https://raw.githubusercontent.com/vragh/share/master/col_by_bg2.png