0

I have this plot in R, which is currently unordered. How can I automatically order the plot by gene density without having to specify the order manually? Is there a command to do that?

species_name <- c("hepatitis D","HIV-1","influenza A","pandoravirus")
species_name
genome_size <- c(1.7, 9.7, 14, 2800)
genome_size
gene_counts <- c(1,9,11 ,2500)
gene_counts
gene_density = gene_counts / (genome_size/1000)
gene_density
expn <- data.frame (species_name, genome_size, gene_counts, gene_density)
barplot(gene_density ,names.arg=species_name,ylim=c(0,1000),xlab="species_name",ylab="gene_density",col="coral",main="Gene Density",border="coral2")
Tina J
  • 4,983
  • 13
  • 59
  • 125
  • 4
    You just need to order the data frame (and use that to plot). `expn <- expn[order(expn$gene_density), ]; with(expn, barplot(gene_density, names.arg=species_name, ylim=c(0,1000), xlab="species_name", ylab="gene_density",col="coral", main="Gene Density",border="coral2"))`. – Ritchie Sacramento Sep 15 '21 at 00:57
  • To follow up on the above point, be careful in your original post that you created `expn` but then used `gene_density` in the `barplot` line, instead of `expn$gene_density`. If you keep it all to references to the `data.frame` then you can order without losing the relationships between the columns. – thelatemail Sep 15 '21 at 00:59
  • 2
    (Or perhaps `barplot(gene_density ~ species_name, data = expn, ...)`.) – r2evans Sep 15 '21 at 00:59
  • 1
    @r2evans - indeeeeeed. – thelatemail Sep 15 '21 at 01:00
  • (Though admittedly that works *here* even without `data=expn`, so your point is still rather salient @thelatemail. I generally dislike formation of frames in that fashion, for precisely this reason.) – r2evans Sep 15 '21 at 01:01
  • 1
    @r2evans - am I crazy or does the formula variation always sort alphabetically? – thelatemail Sep 15 '21 at 01:08
  • @27ϕ9 your code works. Can you submit it as an answer? – Tina J Sep 15 '21 at 01:08
  • 2
    @thelatemail - If you use the formula interface strings are converted to factors so you'd need to set factor levels appropriately before plotting. – Ritchie Sacramento Sep 15 '21 at 01:10
  • @27ϕ9 - there you go, I've never noticed that. – thelatemail Sep 15 '21 at 01:10
  • @thelatemail yes, the formulaic call does not fix the order, it was meant to address the fact that the OP creates `expn` yet still plots with the vectors. – r2evans Sep 15 '21 at 09:07

1 Answers1

1

Use reorder

library(ggplot2)

expn %>%
  ggplot()+
  geom_col(aes(x = reorder(species_name, gene_density), gene_density), fill = "coral") +
  ggtitle("Gene Density") + xlab("species_name") + ylab("gene_density")

ggplot

Park
  • 14,771
  • 6
  • 10
  • 29