如何将指数曲线添加到 ggplot2 中的分类直方图?

How do I add an exponential curve to a categorical histogram in ggplot2?

我有以下数据集:

Type <- c("Type 2", "Type 2", "Type 3", "Type 3", "Type 3", "Type 2", "Type 2", "Type 3", "Type 1", "Type 2", "Type 2", "Type 3", "Type 3", "Type 1", "Type 2", "Type 3", "Type 3", "Type 1", "Type 2", "Type 1", "Type 3", "Type 2", "Type 2", "Type 2", "Type 3", "Type 2", "Type 3", "Type 3", "Type 3", "Type 2", "Type 3", "Type 3", "Type 1", "Type 1", "Type 3", "Type 2", "Type 3", "Type 2", "Type 2", "Type 1", "Type 2", "Type 1")
Group <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP")
Abundance <- c(79, 76, 66, 58, 51, 36, 35, 29, 26, 25, 24, 21, 9, 8, 8, 6, 6, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
Abundance_data <- data.frame (Type, Group, Abundance)

我使用以下代码绘制了针对组的丰度条形图:

# Create a vector to specify the colour and shape for each type
colours_by_type <- c("Type 1"="orange", "Type 2"="chartreuse4", "Type 3"="brown2")

# Reorder the data so that it goes from largest to smallest
Abundance_data_reordered <- transform (Abundance_data, Group = reorder(Group, order(Abundance, decreasing=TRUE))) # The data here is ordered already but my real data is not

# Create an ordered barplot
abundance_plot <- ggplot(data=Abundance_data_reordered, aes(x=Group, y=Abundance)) + 
  geom_bar(stat = "identity", aes(fill = Type))+ 
  theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5))+ 
  scale_fill_manual(values = colours_by_type)+ 
  theme(axis.title.x=element_blank())+ 
  labs(y="Abundance")+ # 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))

abundance_plot

由此产生了以下情节:

我想为此添加一条曲线。我认为指数曲线是最合适的,但我不知道该怎么做,因为我见过的所有示例在 x 轴上都有数字数据。

有什么方法可以将回归曲线或指数曲线拟合到我的图中?

如果要对rank进行回归,很简单:

df <- cbind(Abundance_data_reordered, index = 1:nrow(Abundance_data_reordered))
fit <- lm(log(Abundance) ~ index, data = df)
abundance_plot + 
  stat_function(fun = function(x) exp(fit$coefficients[1] + x*fit$coefficients[2]))

请注意,这条线看起来有点偏离,但它是适合您的数据的正确指数。