|
1 | | -#' @title Scatter Plot |
2 | | -#' @description |
3 | | -#' Produces a scatterplot depending of given dataset and optionally predictors. |
4 | | -#' @param d |
5 | | -#' A \code{string} representing the dataset name. |
6 | | -#' @param pred1 |
7 | | -#' A \code{list} for first predictor containing entries for type and value. |
8 | | -#' @param pred2 |
9 | | -#' A \code{list} for first predictor containing entries for type and value. |
10 | | -#' @return |
11 | | -#' returns a scatterplot for the given dataset and predictor variables. |
12 | | -#' @author Robert Studtrucker |
13 | | -#' @export |
14 | | -scatterPlot <- function(yi,d,pred1=NULL,pred2=NULL, effectName="Effect") { |
15 | | - requireNamespace("metafor") |
16 | | - requireNamespace("ggplot2") |
17 | | - requireNamespace("GGally") |
18 | | - |
19 | | - #load the in variable d defined dataset from the package |
20 | | - dat <- checkData(d) |
21 | | - |
22 | | - pred1<-unlist(pred1) |
23 | | - pred2<-unlist(pred2) |
24 | | - |
25 | | - if( !is.null(pred1) && !is.null(pred2)){ |
26 | | - |
27 | | - checkParameter(dat,c(yi,pred1["value"],pred2["value"])) |
28 | | - |
29 | | - #Es gibt zwei Prädiktoren |
30 | | - if(pred1["type"]=="num" && pred2["type"]=="num"){ |
31 | | - # Scatter, 2 predictors, num/num |
32 | | - GGally::ggscatmat(dat, columns=c(yi,pred1["value"],pred2["value"])) + |
33 | | - ggplot2::geom_point(colour="#34B4D8", shape=19) + |
34 | | - ggplot2::labs(title="scatterplot matrix") |
35 | | - |
36 | | - }else if(pred1["type"]=="cat" && pred2["type"]=="cat"){ |
37 | | - # Violin, 2 predictors, cat/cat |
38 | | - |
39 | | - ggplot2::ggplot(dat, ggplot2::aes(x=dat[,pred1["value"]], y=dat[,yi], color=dat[,pred2["value"]])) + |
40 | | - ggplot2::geom_violin(draw_quantiles = c(0.2,0.4,0.6,0.8)) + |
41 | | - ggplot2::geom_point(ggplot2::aes(x=dat[,pred1["value"]],color=dat[,pred2["value"]]),shape=19,size=2) + |
42 | | - ggplot2::labs(title="Violin Plot",x=pred1["label"], y=effectName, color=pred2["label"]) + |
43 | | - ggplot2::theme(legend.position = "right",plot.title = ggplot2::element_text(hjust = 0.5)) |
44 | | - }else { |
45 | | - numerical<-"" |
46 | | - categorical<-"" |
47 | | - if(pred1["type"]=="cat"){ |
48 | | - categorical<-pred1 |
49 | | - numerical<-pred2 |
50 | | - }else{ |
51 | | - categorical<-pred2 |
52 | | - numerical<-pred1 |
53 | | - } |
54 | | - # Scatter, 2 predictors, num/cat |
55 | | - ggplot2::ggplot(dat = dat, ggplot2::aes(x = dat[,numerical["value"]], y = dat[,yi])) + |
56 | | - ggplot2::geom_point(ggplot2::aes(color = dat[,categorical["value"]]),shape=19, size=3) + |
57 | | - ggplot2::theme(legend.position = "right",plot.title = ggplot2::element_text(hjust = 0.5)) + |
58 | | - ggplot2::labs(title="Scatter Plot",x=numerical["label"], y=effectName, color=categorical["label"]) |
59 | | - } |
60 | | - } |
61 | | - else if(!is.null(pred1) && is.null(pred2)){ |
62 | | - #Es gibt einen Prädiktor |
63 | | - |
64 | | - if(pred1["type"]=="cat"){ |
65 | | - # Violin, 1 predictor, cat |
66 | | - |
67 | | - ggplot2::ggplot(dat, ggplot2::aes(x=dat[,pred1["value"]], y=dat[,yi])) + |
68 | | - ggplot2::geom_violin(draw_quantiles = c(0.2,0.4,0.6,0.8), color='#0480a3') + |
69 | | - ggplot2::geom_point(size=2,color="#34b4d8") + |
70 | | - ggplot2::labs(title="Violin Plot",x=pred1["label"], y=effectName) + |
71 | | - ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) |
72 | | - |
73 | | - }else{ |
74 | | - # Scatter, 1 predictor, num |
75 | | - |
76 | | - ggplot2::ggplot(dat = dat, ggplot2::aes(x = dat[,pred1["value"]], y = dat[,yi])) + |
77 | | - ggplot2::geom_point(colour="#34B4D8", shape=19, size=3) + |
78 | | - ggplot2::labs(title="Scatter Plot",x=pred1["label"], y=effectName) + |
79 | | - ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) |
80 | | - } |
81 | | - |
82 | | - }else if(is.null(pred1)&&is.null(pred2)) { |
83 | | - # Scatter, no predictor |
84 | | - |
85 | | - dat.cleared<- subset(x = dat, subset = !is.na(dat[,yi])) |
86 | | - print(dat.cleared) |
87 | | - |
88 | | - |
89 | | - ggplot2::ggplot(dat = dat, ggplot2::aes(x=seq_along(dat[,yi]), y=dat[,yi])) + |
90 | | - ggplot2::geom_violin(draw_quantiles = c(0.2,0.4,0.6,0.8), color="#0480A3") + |
91 | | - ggplot2::geom_point(size=2, color="#34B4D8") + |
92 | | - ggplot2::labs(title="Violin Plot",x="", y=effectName) + |
93 | | - ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) |
94 | | - } |
95 | | -} |
96 | | - |
97 | | - |
98 | | - |
| 1 | +#' @title Scatter Plot |
| 2 | +#' @description |
| 3 | +#' Produces a scatterplot depending of given dataset and optionally predictors. |
| 4 | +#' @param d |
| 5 | +#' A \code{string} representing the dataset name. |
| 6 | +#' @param pred1 |
| 7 | +#' A \code{list} for first predictor containing entries for type and value. |
| 8 | +#' @param pred2 |
| 9 | +#' A \code{list} for first predictor containing entries for type and value. |
| 10 | +#' @return |
| 11 | +#' returns a scatterplot for the given dataset and predictor variables. |
| 12 | +#' @author Robert Studtrucker |
| 13 | +#' @export |
| 14 | +scatterPlot <- function(yi,d,pred1=NULL,pred2=NULL, effectName="Effect") { |
| 15 | + requireNamespace("metafor") |
| 16 | + requireNamespace("ggplot2") |
| 17 | + requireNamespace("GGally") |
| 18 | + |
| 19 | + #load the in variable d defined dataset from the package |
| 20 | + dat <- d |
| 21 | + |
| 22 | + pred1<-unlist(pred1) |
| 23 | + pred2<-unlist(pred2) |
| 24 | + |
| 25 | + if( !is.null(pred1) && !is.null(pred2)){ |
| 26 | + |
| 27 | + checkParameter(dat,c(yi,pred1["value"],pred2["value"])) |
| 28 | + |
| 29 | + #Es gibt zwei Prädiktoren |
| 30 | + if(pred1["type"]=="num" && pred2["type"]=="num"){ |
| 31 | + # Scatter, 2 predictors, num/num |
| 32 | + GGally::ggscatmat(dat, columns=c(yi,pred1["value"],pred2["value"])) + |
| 33 | + ggplot2::geom_point(colour="#34B4D8", shape=19) + |
| 34 | + ggplot2::labs(title="scatterplot matrix") |
| 35 | + |
| 36 | + }else if(pred1["type"]=="cat" && pred2["type"]=="cat"){ |
| 37 | + # Violin, 2 predictors, cat/cat |
| 38 | + |
| 39 | + ggplot2::ggplot(dat, ggplot2::aes(x=dat[,pred1["value"]], y=dat[,yi], color=dat[,pred2["value"]])) + |
| 40 | + ggplot2::geom_violin(draw_quantiles = c(0.2,0.4,0.6,0.8)) + |
| 41 | + ggplot2::geom_point(ggplot2::aes(x=dat[,pred1["value"]],color=dat[,pred2["value"]]),shape=19,size=2) + |
| 42 | + ggplot2::labs(title="Violin Plot",x=pred1["label"], y=effectName, color=pred2["label"]) + |
| 43 | + ggplot2::theme(legend.position = "right",plot.title = ggplot2::element_text(hjust = 0.5)) |
| 44 | + }else { |
| 45 | + numerical<-"" |
| 46 | + categorical<-"" |
| 47 | + if(pred1["type"]=="cat"){ |
| 48 | + categorical<-pred1 |
| 49 | + numerical<-pred2 |
| 50 | + }else{ |
| 51 | + categorical<-pred2 |
| 52 | + numerical<-pred1 |
| 53 | + } |
| 54 | + # Scatter, 2 predictors, num/cat |
| 55 | + ggplot2::ggplot(dat = dat, ggplot2::aes(x = dat[,numerical["value"]], y = dat[,yi])) + |
| 56 | + ggplot2::geom_point(ggplot2::aes(color = dat[,categorical["value"]]),shape=19, size=3) + |
| 57 | + ggplot2::theme(legend.position = "right",plot.title = ggplot2::element_text(hjust = 0.5)) + |
| 58 | + ggplot2::labs(title="Scatter Plot",x=numerical["label"], y=effectName, color=categorical["label"]) |
| 59 | + } |
| 60 | + } |
| 61 | + else if(!is.null(pred1) && is.null(pred2)){ |
| 62 | + #Es gibt einen Prädiktor |
| 63 | + |
| 64 | + if(pred1["type"]=="cat"){ |
| 65 | + # Violin, 1 predictor, cat |
| 66 | + |
| 67 | + ggplot2::ggplot(dat, ggplot2::aes(x=dat[,pred1["value"]], y=dat[,yi])) + |
| 68 | + ggplot2::geom_violin(draw_quantiles = c(0.2,0.4,0.6,0.8), color='#0480a3') + |
| 69 | + ggplot2::geom_point(size=2,color="#34b4d8") + |
| 70 | + ggplot2::labs(title="Violin Plot",x=pred1["label"], y=effectName) + |
| 71 | + ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) |
| 72 | + |
| 73 | + }else{ |
| 74 | + # Scatter, 1 predictor, num |
| 75 | + |
| 76 | + ggplot2::ggplot(dat = dat, ggplot2::aes(x = dat[,pred1["value"]], y = dat[,yi])) + |
| 77 | + ggplot2::geom_point(colour="#34B4D8", shape=19, size=3) + |
| 78 | + ggplot2::labs(title="Scatter Plot",x=pred1["label"], y=effectName) + |
| 79 | + ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) |
| 80 | + } |
| 81 | + |
| 82 | + }else if(is.null(pred1)&&is.null(pred2)) { |
| 83 | + # Scatter, no predictor |
| 84 | + |
| 85 | + dat.cleared<- subset(x = dat, subset = !is.na(dat[,yi])) |
| 86 | + print(dat.cleared) |
| 87 | + |
| 88 | + |
| 89 | + ggplot2::ggplot(dat = dat, ggplot2::aes(x=seq_along(dat[,yi]), y=dat[,yi])) + |
| 90 | + ggplot2::geom_violin(draw_quantiles = c(0.2,0.4,0.6,0.8), color="#0480A3") + |
| 91 | + ggplot2::geom_point(size=2, color="#34B4D8") + |
| 92 | + ggplot2::labs(title="Violin Plot",x="", y=effectName) + |
| 93 | + ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | + |
0 commit comments