【问题标题】:R: Combine 2 plot with different x lengthR:结合2个不同x长度的图
【发布时间】:2019-02-11 07:49:49
【问题描述】:

我试图结合直方图和伽马分布计算的最大似然估计,而直方图表示 x 的观察值,一条线表示伽马分布计算的最大似然估计。示例图如下所示:

但是,直方图在 x 轴上的长度为 13,而 gamma 分布在 x 轴上的长度为 512,它一直抱怨`

错误:美学长度必须为 1 或与数据相同 (13): x, y

den <- density(x) # x's length is 13, however den's length is 512

dat <- data.frame(x = den$x, y = den$y)
dataframe = data.frame(days = days, x = x)

ggplot(data = dat) +
  geom_line(aes(x=dat$x, y=dgamma(dat$x,shape, rate)), color="red", size = 1) + 
  theme_classic()

bplot = ggplot(data = dataframe,aes(x = dataframe$days, y = (dataframe$x)/sum(dataframe$x)));
bplot + 
  geom_bar(stat="identity", width=0.5)+
  geom_line(aes(x=dat$x, y=dgamma(dat$x,shape, rate)), color="blue", size = 1) 

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您必须在您的geom_linecall 中将data 参数设置为dat,以便将美学映射到您的新dat 而不是dataframe

    bplot + 
      geom_bar(stat="identity", width=0.5)+
      geom_line(data = dat, aes(x=dat$x, y=dgamma(dat$x,shape, rate)), color="blue", size = 1) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多