I have faced a similar problem as it already has been asked here and no proper answer has been shared yet. So basically the order of y-axis should change based on the size of the dots. Unfortunately the facet_grid is not helpful and changes the original dot plot. It would be great if you could help with this.
            Asked
            
        
        
            Active
            
        
            Viewed 238 times
        
    -3
            
            
         
    
    
        user438383
        
- 5,716
- 8
- 28
- 43
 
    
    
        Apex
        
- 1,055
- 4
- 22
- 
                    Does this answer your question? [Order Bars in ggplot2 bar graph](https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph) – SamR Jul 19 '22 at 09:13
- 
                    1No data & no code gives us nothing to work with. Please include both (sample data & code attempt) including a mock-up of your expected output. – Maurits Evers Jul 19 '22 at 09:46
- 
                    1Right now the y axis contain two values per variable (A and B for each GOs). To order them like the plot you expect, you would need to have one entry per GOs. Would a solution that concatenates GOs and Condition work for you? i.e. in the y axis you would have one entry for "GO1308_A" and another for "GO1308_B". – guasi Jul 19 '22 at 09:55
- 
                    Dear @guasi - thank you for your answer. Yes, that can help. – Apex Jul 19 '22 at 09:59
- 
                    Dear @MauritsEvers I have the exact code as the post I share in my question. That is why I did not put my code here. – Apex Jul 19 '22 at 10:00
- 
                    I posted an answer to the [previous question](https://stackoverflow.com/questions/69883865/change-order-y-axis-of-dotplot-in-ggplot2) since that one has the code and the graph of what you expect. Note that my answer is specific to your previous question on how to order the y axis according to the x axis, not according to size as you specify in this question. – guasi Jul 19 '22 at 10:11
1 Answers
1
            To get the y-axis in the same order as the dot size, I did the following:
- sort the data in ascending order of Countwhich determines dot size
- add a column orderequal to the row number
- user orderas the y-axis but usescale_y_continuousto take the labels from theGOscolumn
(This uses the sample code from the question you linked to.)
library("ggplot2")
# create fake data
set.seed(1024) # keep reproducibility
go <- paste0("GO", sample(1000:2000, 5))
Count <- sample(1:20, 10)
data <- data.frame("GOs" = rep(go, 2), 
                   "Condition" = rep(c("A", "B"), each = 5),
                   "GeneRatio" = 1 / sample(10, 10), 
                   "p.adjust" = 0.05 / sample(10, 10),
                   "Count" = Count)
# sort the data by Count (Count determines dot size)
data <- data[order(data$Count),]
   
# add a row number variable `order`
data <- cbind(data,order=1:nrow(data))
# plot: dot plot
ggplot(data = data, aes(x = GeneRatio, y = order, # use `order` as the y-axis
                        color = `p.adjust`, size = Count)) + 
  geom_point() +
  scale_color_gradient(low = "red", high = "blue") +
  theme_bw() + 
  ylab("") +
  xlab("") + 
  scale_y_continuous(breaks=1:nrow(data),labels=data$GOs) + # use scale_y_continuous to use GOs for labels
  ggtitle("GO enrichment analysis")
 
    
    
        shaun_m
        
- 1,213
- 3
- 13