0

Edited to Add: ** I ended up realizing it was a variable type issue, This here is a link to where I finally got it to work!!**

I'm trying to add the adjusted and unadjusted results from a regression onto the same graph, and I'm already using the "model" variable to indicate the number of weeks the regression considers. I'm looking for a way to do a three way distinction, 1= coefficients, 2= number of weeks in model, 3= adjusted/unadjusted. I've gotten it so that everything plots onto one graph, but the adjusted and unadjusted results don't have any legend and are both indicated with the same circle shape and color with no distinction between them. picture of the output without any distinction

I tagged this as ggplot because dwplot visualizes regression model objects or regression results saved in tidy data frames by, e.g., tidy as dot-and-whisker plots generated by ggplot. https://www.rdocumentation.org/packages/dotwhisker/versions/0.5.0/topics/dwplot

I've tried fixing this by having the shape of the plot determined by the third variable, but this failed. Then I tried simply plotting two dwplots on top of each other and failed using either add=TRUE or par(new=TRUE). Does anyone know if this is possible?

The below didn't work, it just resulted in only plotting the second graph.

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) 

The below didn't work either, it just resulted in the original graph without any distinction by 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)

I'm attaching the R code I ran, with all of the packages I have loaded in the environment though I didn't use them all for the dwplotting of course, as well as sample data from the regression results. Thanks in advance!

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) 
  • 2
    Where does `dwplot` come from, and why is this tagged ggplot? Keep in mind that we don't have access to your data, so we can't run your code, and we can't see any output, so the best anybody can do is guess until you add a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – camille Sep 06 '19 at 02:46
  • I tagged it as ggplot, because dwplot is not a separate tag and dwplot uses ggplot to form a dot and whiskers plot as stated in it's documentation "dwplot visualizes regression model objects or regression results saved in tidy data frames by, e.g., tidy as dot-and-whisker plots generated by ggplot." – Marissa James Sep 06 '19 at 20:43
  • I'm going to update the question above to clarify and add output. I didn't see how to add output the first time around. – Marissa James Sep 06 '19 at 20:43

1 Answers1

0

Actually, as I was working through to get a reproducible example I realized that the first method I tried actually was working on my sample data, and that it did work with dwplot. I ended up solving the issue by calling the third regression.model.type variable as a character variable. The variable type seemed to be causing issues with

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

so instead I did

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

and it worked perfectly. Thanks for looking at this!