将SMA回归线(SMATR包)绘制到ggplot中 [英] Plotting SMA regressions lines (smatr package) into ggplot

查看:147
本文介绍了将SMA回归线(SMATR包)绘制到ggplot中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于如何将标准化主轴(SMA)回归线绘制到多面ggplot中,我将不胜感激.我使用了以下代码:

运行SMA分析并使用我要绘制的SMA reg线系数(截距和斜率)创建数据框

  smaReg = sma(Y〜X *类型,数据= ExampleData)摘要(smaReg)smaSummary<-data.frame(Type = 1:6,coef(smaReg)) 

使用geom_abline绘制ggplot代码以绘制SMA回归

  ModFit<-ggplot(ExampleData,aes(y = Y,x = X,color = Level))+geom_point()+theme_bw()+theme_classic()+facet_wrap(〜类型,nrow = 2,ncol = 3)+主题(strip.background = element_blank(),strip.text = element_text(face ='bold',size = 12))+annotate("segment",x = -Inf,xend = Inf,y = -Inf,yend = -Inf,color ='black',size = 1)+annotate("segment",x = -Inf,xend = -Inf,y = -Inf,yend = Inf,color ='black',size = 1)+scale_x_continuous(breaks = seq(从= 0到= 60,乘= 20))+scale_y_continuous(breaks = seq(从= 0到= 120,乘= 20))+geom_abline(数据= sma摘要,aes(截距=海拔,坡度=坡度))+labs(x = expression(paste("Predicted(",mu,"mol m" ^ {-2},"s" ^ {-1},)"))),y = expression(paste("Observed(",mu,"mol m" ^ {-2},"s" ^ {-1},)"))))+ModFit 

此代码还有两个尚待解决的问题,但是我的初学者编码技能尚不足以成功解决它们:

  1. 我使用annotate()和scale_x_continuous在所有多面图中绘制了相同的轴和比例,但是,此解决方案无法绘制X轴刻度线,因此我没有找到一种无需其他方法即可完成此操作的方法我进行更改时出了错.

  2. 运行此绘图代码时,出现以下错误消息:

wrap_dims(n,params $ nrow,params $ ncol)中的错误:nrow * ncol> = n不是真

在尝试以不同方式解决此错误时,我注意到如果将labs()层更改为如下所示的非常简化的版本:

  labs(x =表达式(X),y =表达式(Y),颜色="Level")+ 

此更改将生成多面图,但每个图上均具有所有SMA回归.我不知道为什么更改labs()层可以生成图!我对如何仅为每个图绘制相应的SMA reg线(同时还添加我需要的详细轴标签而又没有其他问题)的想法(和Google搜索)用光了.

(v0.3.0)于2019-11-19创建sup>

@ravic_感谢您的建议,现在我可以使用以下修改后的代码制作带有正确标签的多面图:

我更新了我的ggplot代码:

  bq_x<-bquote("Predicted("〜mu〜"mol"〜m ^ -2〜s ^ -1〜)")bq_y<-bquote("Observed("〜mu〜"mol"〜m ^ -2〜s ^ -1〜)")#1a.通过PFT绘制的图ModFit<-ggplot(ExampleData,aes(y = Y,x = X,color = Level))+geom_point()+theme_bw()+theme_classic()+facet_wrap(〜类型,nrow = 2,ncol = 3)+#自定义构面标签主题(strip.background = element_blank(),strip.text = element_text(face ='bold',size = 12))+annotate("segment",x = -Inf,xend = Inf,y = -Inf,yend = -Inf,color ='black',size = 1)+annotate("segment",x = -Inf,xend = -Inf,y = -Inf,yend = Inf,color ='black',size = 1)+scale_x_continuous(breaks = seq(从= 0到= 60,乘= 20))+scale_y_continuous(breaks = seq(从= 0到= 120,乘= 20))+实验室(x = bq_x,y = bq_y)+geom_abline(数据= sma摘要,aes(截距=海拔,坡度=坡度)) 

现在剩下的两个问题是:1.在所有多面图上添加带有刻度线的Y和X轴线2.在每个图中添加相应的SMA reg单线

再次感谢您对@ravic_的所有帮助!

I would be very grateful for advise on how to plot Standardised Major Axis (SMA) regressions lines into a faceted ggplot. I used the following code:

Run the SMA analysis and create a data frame with the SMA reg line coefficients (intercept and slope) I want to plot

smaReg = sma(Y ~ X * Type, data = ExampleData)
summary(smaReg)
smaSummary <- data.frame(Type = 1:6,coef(smaReg))

ggplot code using geom_abline to plot SMA regressions

ModFit <- ggplot(ExampleData, aes(y = Y, x = X, color = Level)) +
  geom_point() +
  theme_bw() +
  theme_classic() + 
  facet_wrap(~ Type, nrow = 2, ncol = 3) +
  theme(strip.background = element_blank(), strip.text = element_text(face = 'bold', size = 12)) +
  annotate("segment", x = -Inf, xend = Inf, y = -Inf, yend = -Inf, color = 'black', size = 1) +
  annotate("segment", x = -Inf, xend = -Inf, y = -Inf, yend = Inf, color = 'black', size = 1) +
  scale_x_continuous(breaks = seq(from = 0, to = 60, by = 20)) + 
  scale_y_continuous(breaks = seq(from = 0, to = 120, by = 20)) +
  geom_abline(data = smaSummary, aes(intercept = elevation, slope = slope)) +
  labs(x = expression(paste("Predicted (",mu,"mol m"^{-2},"s"^{-1},")")), y = expression(paste("Observed (",mu,"mol m"^{-2},"s"^{-1},")"))) +

ModFit

This code has two remaining issues I need to resolve but my beginner coding skills are not good enough yet to tackle them successfully:

  1. I used annotate() and scale_x_continuous to plot the same axes and scales across all faceted plots, however, this solution does not plot the X axis ticks and I haven't found a way to do this without something else going wrong when I make a change.

  2. When I run this plot code, I get the error message below:

Error in wrap_dims(n, params$nrow, params$ncol) : nrow * ncol >= n is not TRUE

In trying different ways of solving this error, I noticed that if I change the labs() layer to the very simplified version shown below:

labs(x = expression(X), y = expression(Y), color = "Level") +

This change produces a faceted plot but with all the SMA regressions on each plot. I have no idea why changing the labs() layer allows the plot to be produced! I have run out of ideas (and google searches) on how to plot only the corresponding SMA reg line for each plot while also adding the detailed axis labels I need without something else going wrong.

Faceted plot with simplified labels and all SMA reg lines on each plot

Many thanks in advance for any advise on how to solve these two remaining issues!

解决方案

(UPDATED: Added greek math symbols)

Let's tackle the labels, which are pretty thorny in your example. I'm using the iris dataset here as a sample, but using your axis titles.

The key is using bquote() to get the math, the dynamic variables and everything else ready.

library(tidyverse)
mu_val <- 5.1
bq_x <- bquote("Predicted (" ~ mu ~ "=" ~ .(mu_val) ~ " mol " ~ m^-2 ~ s^-1 ~ ")")
bq_y <- bquote("Observed (" ~ mu ~ "=" ~ .(mu_val) ~ "mol m" ~ m^-2 ~ s^-1 ~ ")")

iris %>%
  ggplot(aes(Sepal.Length, Petal.Length)) +
  geom_point() +
  labs(title = "test", x = bq_x, y = bq_y)

Created on 2019-11-19 by the reprex package (v0.3.0)

@ravic_ thanks to your advise I am now able to produce a faceted plot with the correct labels using the revised code below:

My updated my ggplot code:

bq_x <- bquote("Predicted (" ~mu~"mol" ~ m^-2 ~ s^-1 ~ ")")
bq_y <- bquote("Observed (" ~mu~"mol" ~ m^-2 ~ s^-1 ~ ")")

# 1a. Plots by PFTs
ModFit <- ggplot(ExampleData, aes(y = Y, x = X, color = Level)) +
  geom_point() +
  theme_bw() +
  theme_classic() + 
  facet_wrap(~ Type, nrow = 2, ncol = 3) +
  # customise facet labels
  theme(strip.background = element_blank(), strip.text = element_text(face = 'bold', size = 12)) +
  annotate("segment", x = -Inf, xend = Inf, y = -Inf, yend = -Inf, color = 'black', size = 1) +
  annotate("segment", x = -Inf, xend = -Inf, y = -Inf, yend = Inf, color = 'black', size = 1) +
  scale_x_continuous(breaks = seq(from = 0, to = 60, by = 20)) + 
  scale_y_continuous(breaks = seq(from = 0, to = 120, by = 20)) +
  labs(x = bq_x, y = bq_y) +
  geom_abline(data = smaSummary, aes(intercept = elevation, slope = slope))

The two remaining problems now are: 1. Add Y and X axis lines with ticks across all faceted plots 2. Add the corresponding single SMA reg line to each plot

Many thanks again for all your help @ravic_!

这篇关于将SMA回归线(SMATR包)绘制到ggplot中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆