【问题标题】:Plot Compound Annual Growth Rate (3 independent variables) in R在 R 中绘制复合年增长率(3 个自变量)
【发布时间】:2014-11-24 16:53:29
【问题描述】:

我有一个特定品牌在 10 个月内在 8 个不同国家/地区产生的收入。 data.frame 看起来与此类似:

Month    Country    Revenue
1         Fr         1100
1         Uk          900
1         It          800
2         Fr         1200
2         Uk         1050
2         It          900
3         Fr         1350
3         Uk         1200
3         It         1000
4         Fr         1300
4         Uk         1250
4         It          950

# Code to replicate the above mentioned data frame   
data.Frame <- data.frame(Month = c(1,1,1,2,2,2,3,3,3,4,4,4),Country = c("Fr","Uk","It"),
                         Revenue = c(1100,900,800,1200,1050,900,1350,1200,1000,1300,1250,950))

现在我有一个每个国家/地区每个国家/地区收入的条形图

plot_rev <- ggplot(data.Frame, aes(Month, Revenue, fill = Country, ymax = max(Revenue)+100,
                   title = "Revenue (in 1000€) per Country per Month in 2014"))
plot_rev+geom_bar(stat = "identity", position = "dodge")

我还使用函数计算了复合年增长率 (CAGR)

annual.growth.rate <- function(a){

  T1 <- max(a$Month) - min(a$Month)+1
  FV <- a[which(a$Month == max(a$Month)),"Revenue"]
  SV <- a[which(a$Month == min(a$Month)),"Revenue"]
  cagr <- ((FV/SV)^(1/T1)) -1

}

gr <- data.Frame(x = unique(data.Frame$Country),y= 1:length(unique(data.Frame$Country)))

colnames(gr) <- c("country","cagr")

for(i in 1: nrow(gr)){
    gr[i,2] <- round(annual.growth.rate(data.Frame[which(data.Frame$Country == gr[i,1]),]),digits = 2)
}

# Growth Curve Function
FV = PV*(1+cagr)^T

我的问题是,是否可以在现有条形图上绘制生长曲线层?如果是,那么如何?如果没有,那么可以单独做吗? 我试图查看 emdbook 包中的 curve3d 但它只需要 2 个自变量。

提前感谢您的帮助!

【问题讨论】:

  • 您始终可以预先计算(拟合)值并使用geom_line 将其合并。我很好奇,你为什么在aes 中使用ymax
  • 我是 R 的新手,所以我不知道如何在单个图中添加多个图层。你能帮我解决这个问题吗?如果我不使用 ymax,我会收到此警告“未定义 ymax:使用 y 调整位置”
  • 什么是 PV?另外,我不完全理解您为什么要提供不使用的功能(annual.growth.rate)?能否请您提供一个包含 FV、PV、cagr 和 T1 的框架?我自己是新手,所以在我看来(也许是错误的)你试图在原始图的顶部绘制完全不同的 x 和 y 值?
  • @Wave PV 是特定国家/地区任何给定月份的收入。函数,year.growth.rate,用于计算 gr 数据框中的“cagr”值,我试图为相同的 x 值绘制不同的 y 值。我希望,我已经回答了你的问题。

标签: r plot


【解决方案1】:

我不确定我的答案,但你可能只是想要这个吗?

#your function
  annual.growth.rate <- function(a){

  T1 <- max(a$Month) - min(a$Month)+1
  FV <- a[which(a$Month == max(a$Month)),"Revenue"]
  SV <- a[which(a$Month == min(a$Month)),"Revenue"]
  cagr <- ((FV/SV)^(1/T1)) -1
}

# recalculate gr, but faster and more efficient (no loop)
library(plyr)
gr=ddply(data.frame,c("Country"),function(x) cagr=round(annual.growth.rate(x),digits = 2))
#get T as well (although it miht even be shorter to put into your funtion return(list(T1=T1,...))
grT=ddply(data.frame,c("Country"),function(x) T1=(max(x$Month) - min(x$Month)+1))

# Growth Curve Function => just get the values and plot these??
data.frame$FV = data.frame$Revenue*(1+gr$V1)^grT$V1

# plot values with line?
ggplot(data.frame, aes(Month, Revenue, fill = Country, ymax = max(Revenue)+100,title = "Revenue (in 1000€) per Country per Month in 2014"))+
geom_bar(stat = "identity", position = "dodge")+
  geom_line(aes(Month,FV,col=Country),size=2)

【讨论】:

  • 剧情正是我想要的样子。感谢那!但是,计算部分不正确,但这不是这个问题的关注点:)
猜你喜欢
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
  • 2020-07-21
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
相关资源
最近更新 更多