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

时间:2018-01-19 03:08:28

标签: r ggplot2 bar-chart curve-fitting

我有以下数据集:

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

这产生了以下情节:

enter image description here

我想为此添加一条曲线。我认为指数曲线是最合适的,但我无法弄清楚如何做到这一点,因为我看到的所有例子都有x轴上的数值数据。

有没有办法让回归曲线或指数曲线适合我的情节?

1 个答案:

答案 0 :(得分:0)

如果你想对排名进行回归,那就非常简单了:

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

enter image description here

请注意,该行看起来有点偏,但它是适合您数据的指数。