将框和线组合成单R图表图例

时间:2018-05-31 15:56:32

标签: r

我正在尝试用R中的两个叠加曲线拟合绘制直方图。到目前为止,一切都在运作,但传说。我想要一个包含直方图框和拟合曲线的单个图例。我可以使用下面的代码,但这会创建两个单独的图例。是否可以将这些图例组合成一个盒子?

I want a single legend, not two

truehist(
    data=DismissalRateData$DismissalRate,
    breaks=seq(.6, 1, by=.025),
    col="lightgreen",
    prob=TRUE, 
    ylab="Density",
    xlab="Claim Dismissal Rate", 
    main="Histogram of Historical Claim Dismissal Rates \n(With Superimposed Beta Curve Fits)", 
    xaxt='n',
    yaxt='n',
    ylim=c(0,14)
    )

curve(
    dbeta(x, FittedAlphaParameter, FittedBetaParameter),
    add=TRUE,
    col="red", 
    lwd=3
    )

curve(
    dbeta(x, 100.2753, 11.5809),
    add=TRUE,
    col="blue", 
    lwd=3
    )

axis(
    side=1, 
    at=seq(.6, 1, by=.025),
    labels=formattable::percent(seq(.6, 1, by=.025),digits=0)
    )

legend(x="topleft", legend="Actual Data", fill="lightgreen", col="black")

legend(x="left" ,
    legend=c("Beta fitted with all data 2006-2017", "Beta fitted excluding 2006-2008"),
    col=c("red", "blue"), 
    lty=1, lwd=3)

0 个答案:

没有答案