0

Im trying to make a ggplot of a factor column and I want the plot to start from decreasing order of the highest count. I sorted my data before I plotted, but I still get the plot like this. Could someone please let me know how I can get a cleaner plot?enter image description here

library(viridis)
library(ggplot2)
library(tidyverse)

phenotype <- data.frame (table(tiering$Phenotype))
phenotype <- phenotype %>% rename(Phenotype = Varl, Total = Freg)
phenotype <- phenotype[order(phenotype$Total, decreasing = TRUE) ,]
P <- ggplot(phenotype, aes(Phenotype, Total, fill = Total)) + geom_col() + geom_text(aes(label = Total), vjust= +0.3, size = 3.5) + theme classic() + ggtitle("Main phenotypes stated") +
scale_fill_viridis(option = "viridis")

enter image description here

Andy Baxter
  • 5,833
  • 1
  • 8
  • 22
Ar1229
  • 131
  • 2
  • 9
  • 1
    Need to sort the `levels` attribute of the particular column. Sorting the integers in factors or sorting dataframe rownames are basically a meaningless processes – IRTFM Oct 04 '22 at 20:58
  • The phenotype column has 86 levels and the Total column has none. How do I sort it in such a way that the Total column order is plotted? – Ar1229 Oct 04 '22 at 21:04
  • Hi Andy, I did try that solution before I posted on here. The aes(factor(Phenotype) was something I tried and it didn't work for me. Which is why I made the data frame again – Ar1229 Oct 04 '22 at 21:33

1 Answers1

2

Here's a very quick way of doing it (where multiple solutions exist). Use factor to turn Phenotype into a factor and forcats::fct_inorder to arrange by the order you've already put them in.

On generated data:

library(tidyverse)
library(viridis)
#> Loading required package: viridisLite

tibble(Phenotype = sample(50:99, 50, replace = FALSE),
       Total = exp(rpois(50, 3))) |> 
  arrange(desc(Total)) |> 
  ggplot(aes(fct_inorder(factor(Phenotype)), Total, fill = Total)) +
  geom_col() + 
  theme_classic() + 
  ggtitle("Main phenotypes stated") +
  scale_fill_viridis(option = "viridis")

Your question will perhaps be marked as a duplicate of Order discrete x scale by frequency/value asked 12 years ago - which posits quite a few ways of getting the outcome you need. Found by searching SO for ggplot2 tag

You can also arrange your columns within the ggplot(aes()) call with fct_reorder(factor(Phenotype), ...) like so:

phenotype |>
  ggplot(aes(fct_reorder(factor(Phenotype), Total, .desc = TRUE), Total, fill = Total)) +
  geom_col()
Andy Baxter
  • 5,833
  • 1
  • 8
  • 22
  • HI, thank you for taking the time to provide a solution. I did try the questions you have alluded to in your answer, I couldn't fix mine. I tried your code and I got an error which said : "Error in fortify()" – Ar1229 Oct 04 '22 at 21:31
  • I'm afraid I can't replicate the `fortify` error, as I cant see any place where it might be called in the code example! You may have to try updating `ggplot2` and restarting a fresh R session maybe? – Andy Baxter Oct 04 '22 at 21:39
  • 1
    I did exactly that and it worked. Thank you so much for you time and effort. – Ar1229 Oct 04 '22 at 21:39