【发布时间】:2018-02-16 02:44:10
【问题描述】:
我正在复制粘贴我正在使用的自定义函数的简化版本。该函数工作正常,但它产生的warnings 让我无法摆脱。这个函数将成为一个包的一部分,因此对于用户来说不要被这些神秘的错误所抛弃是很重要的,因此我希望以一种不再产生这些警告的方式来更改脚本。
警告是(请参阅下面完全可重现的示例):
position_jitterdodge requires non-overlapping x intervals
我已经检查过这个关于position_jitterdodge (Position-dodge warning with ggplot boxplot?) 的问题,但没有太大帮助,因为该图中没有箱线图。
对于问题和代码的冗长性质深表歉意。希望提供所有可能的详细信息来帮助诊断。
# loading needed libraries
library(ggplot2)
library(dplyr)
library(devtools)
devtools::install_github("daattali/ggExtra")
library(ggExtra) # attach the development version
library(rlang)
# defining the custom function
ggscatterstats <-
function(data = NULL,
x,
y,
xfill = "orange",
yfill = "green",
marginal = NULL,
marginaltype = "histogram",
jitter.width = NULL,
jitter.height = 0.2,
dodge.width = 0.75) {
# preparing a dataframe out of provided inputs
if (!is.null(data)) {
# if dataframe is provided
data <-
dplyr::select(
.data = data,
x = !!rlang::enquo(x),
y = !!rlang::enquo(y)
)
} else {
# if vectors are provided
data <-
base::cbind.data.frame(x = x,
y = y)
}
# preparing the scatterplotplot
plot <-
ggplot2::ggplot(data = data,
mapping = aes(x = x,
y = y)) +
geom_count(
show.legend = FALSE,
colour = "black",
size = 3,
alpha = 0.5,
position = position_jitterdodge(
jitter.width = jitter.width,
jitter.height = jitter.height,
dodge.width = dodge.width
)
) +
geom_smooth(method = "lm",
se = TRUE,
size = 1.5) +
theme_grey()
# marginal plot will be shown by default
if (is.null(marginal))
marginal <- TRUE
if (isTRUE(marginal)) {
# creating the ggMarginal plot of a given marginaltype
plot <-
ggExtra::ggMarginal(
p = plot,
type = marginaltype,
size = 5,
xparams = list(fill = xfill,
col = "black"),
yparams = list(fill = yfill,
col = "black")
)
}
return(plot)
}
# using the function
ggscatterstats(data = iris, x = Sepal.Length, y = Petal.Width)
#> Warning: position_jitterdodge requires non-overlapping x intervals
#> Warning: position_jitterdodge requires non-overlapping x intervals
由reprex package (v0.1.1.9000) 于 2018 年 2 月 15 日创建。
【问题讨论】:
标签: r ggplot2 dplyr plyr rlang