【问题标题】:Plotting two dwplots on one graph as you can normally do using add=TRUE or par(new=TRUE)像通常使用 add=TRUE 或 par(new=TRUE) 一样在一个图形上绘制两个 dwplot
【发布时间】:2019-09-07 05:36:20
【问题描述】:

编辑添加:**我最终意识到这是一个变量类型问题,This here is a link to where I finally got it to work!!**

我正在尝试将回归的调整和未调整结果添加到同一个图表中,并且我已经在使用“模型”变量来指示回归考虑的周数。我正在寻找一种方法来进行三向区分,1= 系数,2= 模型中的周数,3= 调整/未调整。我已经得到它,以便所有内容都绘制在一个图表上,但是调整和未调整的结果没有任何图例,并且都用相同的圆形和颜色表示,它们之间没有区别。 picture of the output without any distinction

我将其标记为 ggplot,因为 dwplot 将回归模型对象或回归结果可视化,例如通过 ggplot 生成的 tidy 作为点和须图保存在整洁的数据框中。 https://www.rdocumentation.org/packages/dotwhisker/versions/0.5.0/topics/dwplot

我已尝试通过使绘图的形状由第三个变量确定来解决此问题,但失败了。然后我尝试简单地在彼此之上绘制两个 dwplot,但使用 add=TRUE 或 par(new=TRUE) 失败。有谁知道这是否可行?

下面没有用,它只是导致只绘制了第二张图。

fig.cur <- dwplot(regression.unadjusted.df,                 
                  vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2), 
                  dot_args = aes(shape = "triangle")) 

plot(fig.cur) 
par(new = FALSE) #tried with just this line first

#tried with just the add=TRUE second
plot(dwplot(Oregression.adjusted.df, dot_args = aes(shape = "circle")),add=TRUE) 

以下也不起作用,它只是导致原始图形与model.adjustment.type没有任何区别

fig.cur <- dwplot(regression.df,
                  vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2), 
                  dot_args = list(aes(shape = model.adjustment.type))) 

 plot(fig.cur)

我附上了我运行的 R 代码,以及我在环境中加载的所有包,当然我并没有将它们全部用于 dwplotting,以及来自回归结果的示例数据。提前致谢!

library(tidyverse)
library(magrittr)
library(forcats)
library(lubridate)
library(dotwhisker)
library(ggplot2)
library(zoo)


regression.df <- ( read.csv( 'regression-R-time-series-EXAMPLE.csv', row.names=NULL ))


fig.cur <- dwplot(regression.df,
                  vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2), 
                  dot_args = list(aes(shape = model.adjustment.type))) 

plot(fig.cur)



regression.unadjusted.df <- regression.df %>% filter(model.adjustment.type=="unadjusted")
regression.adjusted.df <- regression.df %>% filter(model.adjustment.type=="adjusted")


fig.cur <- dwplot(regression.unadjusted.df,                 
                  vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2), 
                  dot_args = aes(shape = "triangle")) 

plot(fig.cur) 
par(new = FALSE) #tried with just this line first

#tried with just the add=TRUE second
plot(dwplot(regression.unadjusted.df, dot_args = aes(shape = "circle")),add=TRUE) 

【问题讨论】:

  • dwplot 来自哪里,为什么这个标签是 ggplot?请记住,我们无权访问您的数据,因此我们无法运行您的代码,也看不到任何输出,因此任何人都只能猜测,直到您添加 reproducible example
  • 我将其标记为 ggplot,因为 dwplot 不是一个单独的标签,并且 dwplot 使用 ggplot 形成点和须图,如其文档中所述“dwplot 可视化回归模型对象或保存在整齐数据中的回归结果框架由,例如,整洁的 ggplot 生成的点须图。”
  • 我将更新上面的问题以澄清和添加输出。我第一次没有看到如何添加输出。

标签: r ggplot2 plot


【解决方案1】:

实际上,当我努力获得一个可重现的示例时,我意识到我尝试的第一种方法实际上是处理我的样本数据,并且它确实适用于 dwplot。我最终通过将第三个 regression.model.type 变量称为字符变量来解决问题。变量类型似乎导致

出现问题

dot_args = list(aes(shape = model.adjustment.type)))

所以我做了

dot_args = list(aes(shape = as.character(model.adjustment.type))))

而且效果很好。感谢您查看此内容!

【讨论】:

    猜你喜欢
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多