Skip to content

Commit

Permalink
package removed gbm.auto alias. factorplot add ggplot2 replace, attem…
Browse files Browse the repository at this point in the history
…pt to address no visible binding for Category and ycentred, add automatic ordering of integer variables
  • Loading branch information
SimonDedman committed Aug 31, 2023
1 parent e539152 commit cb97b0b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 63 deletions.
102 changes: 51 additions & 51 deletions .Rhistory
Original file line number Diff line number Diff line change
@@ -1,54 +1,3 @@
axis.title = ggplot2::element_text(size = rel(ggplot2axistitle)),
legend.text = ggplot2::element_text(size = rel(ggplot2legendtext)),
legend.title = ggplot2::element_text(size = rel(ggplot2legendtitle)),
legend.title.align = ggplot2legendtitlealign, # otherwise effect type title centre aligned for some reason
plot.background = ggplot2::element_rect(fill = ggplot2plotbackgroundfill,
colour = ggplot2plotbackgroundcolour), # white background
strip.text.x = ggplot2::element_text(size = rel(ggplot2striptextx)),
panel.border = ggplot2::element_rect(colour = ggplot2panelbordercolour,
fill = ggplot2panelborderfill,
linewidth = ggplot2panelborderlinewidth),
legend.spacing.x = ggplot2legendspacingx, # compress spacing between legend items, this is min
legend.background = ggplot2legendbackground,
panel.background = ggplot2::element_rect(fill = ggplot2panelbackgroundfill,
colour = ggplot2panelbackgroundcolour),
panel.grid = ggplot2::element_line(colour = ggplot2panelgridcolour),
legend.key = ggplot2legendkey)
ggplot2labsx = "",
ggplot2labsx = ""
ggplot2labsy = "Marginal Effect"
ggplot2axistext = 1.5
ggplot2axistitle = 2
ggplot2legendtext = 1
ggplot2legendtitle = 1.5
ggplot2legendtitlealign = 0
ggplot2plotbackgroundfill = "white"
ggplot2plotbackgroundcolour = "grey50"
ggplot2striptextx = 2
ggplot2panelbordercolour = "black"
ggplot2panelborderfill = NA
ggplot2panelborderlinewidth = 1
ggplot2legendspacingx = unit(0, "cm")
ggplot2legendbackground = ggplot2::element_blank()
ggplot2panelbackgroundfill = "white"
ggplot2panelbackgroundcolour = "grey50"
ggplot2panelgridcolour = "grey90"
ggplot2legendkey = ggplot2::element_blank()
ggsavefilename = paste0(saveloc, lubridate::today(), "_SankeyAlluvial_EMT.SoEv-EfTyp_Col-EfSz.png")
ggsaveplot = last_plot()
ggsavedevice = "png"
ggsavepath = ""
ggsavescale = 2
ggsavewidth = 10
ggsaveheight = 4
ggsaveunits = "in"
ggsavedpi = 300
ggsavelimitsize = TRUE
ggplot2::ggplot(x) +
ggplot2::geom_col(aes(x = Category,
y = ycentred)) +
# alter axis labels
ggplot2::labs(x = "",
y = "Marginal Effect") +
ggplot2::scale_x_discrete(guides(x = guide_axis(angle = 90))) +
ggplot2::theme_minimal() %+replace% ggplot2::theme(
Expand Down Expand Up @@ -510,3 +459,54 @@ legendposition = c(0.05, 0.18),
savedir = "/home/simon/Documents/Si Work/PostDoc Work/Gbmauto help/2022-12-06 Cat Wells/CPUESpawnVS/gbmmapsftest"
)
devtools::document()
devtools::check()
?rename
data("iris")
tmp <- iris |> dplyr::rename("MYSPECIES" = "Species")
devtools::document()
devtools::check()
View(iris)
tmp <- iris |> dplyr::arrange("Sepal.Length")
View(tmp)
tmp <- iris |> dplyr::arrange(Sepal.Length)
View(tmp)
tmp <- iris |> dplyr::arrange("Petal.Length")
View(tmp)
tmp <- iris |> dplyr::mutate(Petal.Length = ordered("Petal.Length"))
View(tmp)
?ordered
x <- readr@@read_csv("/home/simon/Documents/Si Work/PostDoc Work/Kroetz & Dedman Sawfish BRT/P.Pectinata.CPUE.m2/AllExpvars/P.Pectinata.CPUE.m2/Bin_Best_line_Mesh.Inch.csv")
x <- readr::read_csv("/home/simon/Documents/Si Work/PostDoc Work/Kroetz & Dedman Sawfish BRT/P.Pectinata.CPUE.m2/AllExpvars/P.Pectinata.CPUE.m2/Bin_Best_line_Mesh.Inch.csv")
x <- x |>
# dplyr::mutate(ycentred = y - mean(y)) |> # make mean column
# The above exists already in gbm.auto L843
# Also it uses preset colname y produced by gbm.auto.
dplyr::rename("Category" = tidyselect::last_col(offset = 2), # no first_col option
"ycentred" = tidyselect::last_col()) |>
# 2023-08-30 quoted Category to hopefully address gbm.factorplot: no visible binding for global variable ‘Category’
# also need to do for ycentred
dplyr::arrange(ycentred) # re-order the x axis for categorical variables in order from high to low value
View(x)
x <- readr::read_csv("/home/simon/Documents/Si Work/PostDoc Work/Kroetz & Dedman Sawfish BRT/P.Pectinata.CPUE.m2/AllExpvars/P.Pectinata.CPUE.m2/Bin_Best_line_Year.csv")
View(x)
x <- x |>
# dplyr::mutate(ycentred = y - mean(y)) |> # make mean column
# The above exists already in gbm.auto L843
# Also it uses preset colname y produced by gbm.auto.
dplyr::rename("Category" = tidyselect::last_col(offset = 2), # no first_col option
"ycentred" = tidyselect::last_col()) |> # attempt to address no visible binding for global variable ‘Category’
# 2023-08-30 quoted Category to hopefully address gbm.factorplot: no visible binding for global variable ‘Category’
# also need to do for ycentred
dplyr::arrange("ycentred") # re-order the x axis for categorical variables in order from high to low value
View(x)
x <- x |>
# dplyr::mutate(ycentred = y - mean(y)) |> # make mean column
# The above exists already in gbm.auto L843
# Also it uses preset colname y produced by gbm.auto.
dplyr::rename("Category" = tidyselect::last_col(offset = 2), # no first_col option
"ycentred" = tidyselect::last_col()) |> # attempt to address no visible binding for global variable ‘Category’
# 2023-08-30 quoted Category to hopefully address gbm.factorplot: no visible binding for global variable ‘Category’
# also need to do for ycentred
dplyr::arrange(ycentred) # re-order the x axis for categorical variables in order from high to low value
devtools::document()
devtools::check()
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ importFrom(gbm,predict.gbm)
importFrom(ggmap,get_map)
importFrom(ggmap,ggmap)
importFrom(ggmap,register_google)
importFrom(ggplot2,"%+replace%")
importFrom(ggplot2,aes)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_line)
Expand Down
4 changes: 2 additions & 2 deletions R/gbm.auto-package.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @keywords internal
"_PACKAGE"
#' @aliases gbm.auto-package
#'
"_PACKAGE"

## usethis namespace: start
#' @importFrom lifecycle deprecated
## usethis namespace: end
Expand Down
23 changes: 14 additions & 9 deletions R/gbm.factorplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#'
#' @export
#' @importFrom dplyr arrange mutate rename
#' @importFrom ggplot2 aes element_blank element_line element_rect element_text geom_col ggplot ggsave guide_axis guides labs last_plot rel theme theme_minimal
#' @importFrom ggplot2 aes element_blank element_line element_rect element_text geom_col ggplot ggsave guide_axis guides labs last_plot rel theme theme_minimal %+replace%
#' @importFrom grid unit
#' @importFrom lubridate today
#' @importFrom readr read_csv
Expand Down Expand Up @@ -112,24 +112,29 @@ gbm.factorplot <- function(x,
# dplyr::mutate(ycentred = y - mean(y)) |> # make mean column
# The above exists already in gbm.auto L843
# Also it uses preset colname y produced by gbm.auto.
dplyr::rename(Category = tidyselect::last_col(offset = 2)) |> # no first_col option
dplyr::rename("Category" = tidyselect::last_col(offset = 2), # no first_col option
"ycentred" = tidyselect::last_col()) |> # attempt to address no visible binding for global variable ‘Category’
# 2023-08-30 quoted Category to hopefully address gbm.factorplot: no visible binding for global variable ‘Category’
# also need to do for ycentred
dplyr::arrange(ycentred) # re-order the x axis for categorical variables in order from high to low value

# re-reorder by factorplotlevels if present
if (!is.null(factorplotlevels)) {
# check level number matches x categories
if (length(factorplotlevels) != length(x$Category)) stop("number of factorplotlevels doesn't match number of categories in x")
if (length(factorplotlevels) != length(x[, "Category"])) stop("number of factorplotlevels doesn't match number of categories in x") # x$Category
# check names match
if (!all(factorplotlevels %in% x$Category)) stop(paste0("The following level names not present in categories in x: ",
factorplotlevels[!factorplotlevels %in% x$Category]))
if (!all(factorplotlevels %in% x[, "Category"])) stop(paste0("The following level names not present in categories in x: ", # x$Category
factorplotlevels[!factorplotlevels %in% x[, "Category"]])) # x$Category
x <- x |>
dplyr::mutate(Category = ordered(Category, factorplotlevels = factorplotlevels)) |> # recreate Category as ordered factor with factorplotlevels
dplyr::arrange(Category) # arrange by labels (implicit)
dplyr::mutate(Category = ordered(Category, levels = factorplotlevels)) |> # recreate Category as ordered factor with factorplotlevels. Can't quote "Category" in ordered
dplyr::arrange(Category) # arrange by labels (implicit). Can't quote "Category"
} else {
if (is.integer(x$Category)) x <- x |> dplyr::mutate(Category = ordered(Category)) # make integers ordered factor to avoid defaulting to continuous
}

ggplot2::ggplot(x) +
ggplot2::geom_col(ggplot2::aes(x = Category,
y = ycentred)) +
ggplot2::geom_col(ggplot2::aes(x = Category, # x[, "Category"]
y = ycentred)) + # x[, "ycentred"]
# rotate x axis labels
ggplot2::guides(x = ggplot2::guide_axis(angle = ggplot2guideaxisangle)) +
# alter axis labels
Expand Down
2 changes: 1 addition & 1 deletion man/gbm.auto-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb97b0b

Please sign in to comment.