相关疑难解决方法(0)

在ggplot上添加回归线

我正努力在ggplot上添加回归线.我首先尝试使用abline,但我没有设法让它工作.然后我尝试了这个......

data = data.frame(x.plot=rep(seq(1,5),10),y.plot=rnorm(50))
ggplot(data,aes(x.plot,y.plot))+stat_summary(fun.data=mean_cl_normal) +
   geom_smooth(method='lm',formula=data$y.plot~data$x.plot)
Run Code Online (Sandbox Code Playgroud)

但它也没有用.

regression r linear-regression ggplot2

101
推荐指数
4
解决办法
24万
查看次数

如何在R中为我的数据拟合平滑曲线?

我试图绘制一条平滑的曲线R.我有以下简单的玩具数据:

> x
 [1]  1  2  3  4  5  6  7  8  9 10
> y
 [1]  2  4  6  8  7 12 14 16 18 20
Run Code Online (Sandbox Code Playgroud)

现在,当我使用标准命令绘制它时,它看起来很崎岖和前卫,当然:

> plot(x,y, type='l', lwd=2, col='red')
Run Code Online (Sandbox Code Playgroud)

如何使曲线平滑,以便使用估计值对3条边进行舍入?我知道有很多方法可以拟合平滑曲线,但我不确定哪种方法最适合这种类型的曲线以及如何编写它R.

plot r curve-fitting

78
推荐指数
7
解决办法
22万
查看次数

将三阶多项式及其方程加到r中的ggplot中

我绘制了以下数据并添加了黄土更平滑.我想在图中添加一个三阶多项式及其方程(包括残差).有什么建议?

set.seed(1410)
dsmall<-diamonds[sample(nrow(diamonds), 100), ]
df<-data.frame("x"=dsmall$carat, "y"=dsmall$price)

p <-ggplot(df, aes(x, y)) 
p <- p + geom_point(alpha=2/10, shape=21, fill="blue", colour="black", size=5)

#Add a loess smoother
p<- p + geom_smooth(method="loess",se=TRUE)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如何添加三阶多项式?我试过了:

p<- p + geom_smooth(method="lm", se=TRUE, fill=NA,formula=lm(y ~ poly(x, 3, raw=TRUE)),colour="red")
Run Code Online (Sandbox Code Playgroud)

最后,如何将三阶多项式方程和残差加到图中?我试过了:

 lm_eqn = function(df){
    m=lm(y ~ poly(x, 3, df))#3rd degree polynomial
    eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
    list(a = format(coef(m)[1], digits = 2),
    b = format(coef(m)[2], digits = 2),
    r2 = format(summary(m)$r.squared, digits = 3)))
    as.character(as.expression(eq))
}


data.label …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

28
推荐指数
2
解决办法
2万
查看次数

是否可以使用ggplot2绘制gam拟合的平滑组件?

我正在gammgcv包中使用模型并存储结果,model到目前为止,我一直在使用平滑组件plot(model).我最近开始使用ggplot2并喜欢它的输出.所以我想知道,是否可以使用ggplot2绘制这些图表?

这是一个例子:

x1 = rnorm(1000)
x2 = rnorm(1000)
n = rpois(1000, exp(x1) + x2^2)

model = gam(n ~ s(x1, k=10) + s(x2, k=20), family="poisson")
plot(model, rug=FALSE, select=1)
plot(model, rug=FALSE, select=2)
Run Code Online (Sandbox Code Playgroud)

我感兴趣s(x1, k=10)s(x2, k=20)不是合适.

部分答案:

我更深入地挖掘plot.gammgcv:::plot.mgcv.smooth和建立了自己的功能,提取从平滑部件的预测效果和标准误差.它没有处理所有选项和案例,plot.gam因此我只将其视为部分解决方案,但它对我来说效果很好.

EvaluateSmooths = function(model, select=NULL, x=NULL, n=100) {
  if (is.null(select)) {
    select = 1:length(model$smooth)
  }
  do.call(rbind, lapply(select, function(i) {
    smooth = model$smooth[[i]]
    data = model$model

    if (is.null(x)) { …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gam mgcv

23
推荐指数
2
解决办法
1万
查看次数

用ggpmisc显示nls模型的方程

Rggpmisc可用于显示lm模型和poly模型的方程ggplot2(参见此处参考).想知道如何使用nls模型方程结果.以下是我的MWE.ggplot2ggmisc

library(ggpmisc)
args <- list(formula = y ~ k * e ^ x,
             start = list(k = 1, e = 2))
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  stat_fit_augment(method = "nls",
                   method.args = args)
Run Code Online (Sandbox Code Playgroud)

r nls ggplot2 lm ggpmisc

14
推荐指数
2
解决办法
469
查看次数

R:source_gist无效

我试图source_gistdevtools包中使用但我遇到一个错误:

> library(devtools)
> source_gist("524eade46135f6348140")
Error in r_files[[which]] : invalid subscript type 'closure'
Run Code Online (Sandbox Code Playgroud)

谢谢你的建议.

r devtools

13
推荐指数
1
解决办法
1672
查看次数

ggplot2:在图中添加p值

我有这个情节

在此输入图像描述

使用下面的代码

library(dplyr) 
library(ggplot2)
library(ggpmisc)

df <- diamonds %>%
  dplyr::filter(cut%in%c("Fair","Ideal")) %>%
  dplyr::filter(clarity%in%c("I1" ,  "SI2" , "SI1" , "VS2" , "VS1",  "VVS2")) %>%
  dplyr::mutate(new_price = ifelse(cut == "Fair", 
                                   price* 0.5, 
                                   price * 1.1))

formula <- y ~ x    
ggplot(df, aes(x= new_price, y= carat, color = cut)) +
  geom_point(alpha = 0.3) +
  facet_wrap(~clarity, scales = "free_y") +
  geom_smooth(method = "lm", formula = formula, se = F) +
  stat_poly_eq(aes(label = paste(..rr.label..)), 
               label.x.npc = "right", label.y.npc = 0.15,
               formula = formula, parse = TRUE, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 p-value

12
推荐指数
1
解决办法
1万
查看次数

ggplot:使用Facet添加回归线方程和R2

我已经创建了一个多面散点图ggplot但我正在努力将回归线方程添加到每个方面.这里回答没有刻面的简单情况,但这种方法不会扩展到刻面图.

有什么想法如何以干净的方式实现这一目标?

r ggplot2

11
推荐指数
2
解决办法
4万
查看次数

使用facet在图形上添加R ^ 2

我正在多个方面绘制geom_points,并希望在每个方面上注释R ^ 2(最好是在facet_label而不是图形上).我在这里找到了一些代码,它会给出R ^ 2和完整数据的回归方程框架而不是子集.

我的data.frame被追加了.

基本上我想关联ln_x和ln_y(由于命名约定不正确,ln_x实际上在y轴上)由roi_size面对.这是我到目前为止:

lm_eqn = function(df){
    m = lm(ln_x ~ ln_y, df);
    eq <- substitute(~~R^2~"="~r2, 
                     list(r2 = format(summary(m)$r.squared, digits = 3)))
    as.character(as.expression(eq));                 
}
p2 <- ggplot(df, aes(x=ln_x, y=ln_y)) + geom_point(shape=19, aes(colour=factor(depth))) + geom_smooth(method="lm") + 
    facet_wrap(~roi_size) + scale_color_discrete("depth (mm)")

p2  + labs(y=expression(ln(frac(C[low]^air,C[low]^depth))),
           x=expression(ln(frac(C[low]^depth,C[high]^depth))) ) +
    theme(axis.title.x = element_text(colour='blue', size=16, hjust=0.9)) + 
    theme(axis.title.y = element_text(colour='blue', size=16, angle=0)) + 
    geom_text(aes(x=1.5,y=2.2,label=lm_eqn(df),family="serif"), 
       color='blue',  parse=TRUE)
Run Code Online (Sandbox Code Playgroud)

这将打印每个方面上的完整数据帧的R ^ 2.如何调整为每个方面的数据打印R ^ 2,因为df取决于facet变量(roi_size).另外,我如何在构面标签而不是图形上打印文本

structure(list(roi_size = c(54.11, 49.18, 41.06, 32.31, 23.71, 
13.85, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

10
推荐指数
1
解决办法
5442
查看次数

R中的幂回归类似于excel

我有一个简单的数据集,我正在尝试使用功率趋势来最好地拟合数据.样本数据非常小,如下:

structure(list(Discharge = c(250, 300, 500, 700, 900), Downstream = c(0.3, 
0.3, 0.3, 0.3, 0.3), Age = c(1.32026239202165, 1.08595138888889, 
0.638899189814815, 0.455364583333333, 0.355935185185185)), .Names = c("Discharge", 
"Downstream", "Age"), row.names = c(NA, 5L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

数据如下:

> new
  Discharge Downstream       Age
1       250        0.3 1.3202624
2       300        0.3 1.0859514
3       500        0.3 0.6388992
4       700        0.3 0.4553646
5       900        0.3 0.3559352
Run Code Online (Sandbox Code Playgroud)

我试图用上面的数据绘制 ggplot2

ggplot(new)+geom_point(aes(x=Discharge,y=Age))
Run Code Online (Sandbox Code Playgroud)

我可以添加线性线geom_smooth(method="lm")但我不知道我需要什么代码来显示电源线.

输出如下:

在此输入图像描述

如何在Excel中添加幂线性回归线?excel图如下所示:

在此输入图像描述

r ggplot2

10
推荐指数
3
解决办法
1万
查看次数

标签 统计

r ×10

ggplot2 ×8

curve-fitting ×1

devtools ×1

gam ×1

ggpmisc ×1

linear-regression ×1

lm ×1

mgcv ×1

nls ×1

p-value ×1

plot ×1

regression ×1