【问题标题】:How to order a plot in R [duplicate]如何在R中订购一个图[重复]
【发布时间】:2021-09-15 00:47:23
【问题描述】:

我在R 中有这个情节,目前是无序的。如何按基因密度自动排序图,而无需手动指定顺序?有命令吗?

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")

【问题讨论】:

  • 您只需要订购数据框(并使用它来绘制)。 expn &lt;- 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")).
  • (或者barplot(gene_density ~ species_name, data = expn, ...)。)
  • @r2evans - indeeeeeed.
  • @r2evans - 我疯了还是公式变体总是按字母顺序排序?
  • @thelatemail - 如果您使用公式界面,字符串将转换为因子,因此您需要在绘图前适当地设置因子水平。

标签: r plot bar-chart


【解决方案1】:

使用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")

【讨论】:

  • 您愿意使用 OP 请求的同一个包吗?
  • 我们不能使用额外的库吗?
  • @TinaJ 我没有注意到不要使用额外的库。我很抱歉。
猜你喜欢
  • 2021-08-12
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 2023-03-10
  • 2019-09-27
相关资源
最近更新 更多