Skip to content

Commit

Permalink
spider developments
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianHordyk committed Sep 26, 2024
1 parent 5e00558 commit 484e282
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 108 deletions.
5 changes: 3 additions & 2 deletions R/fct_plotQuilt.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ plotQuilt <- function(slick, MP_label='Code', minmax=FALSE, shading=TRUE,
shading_list[[i]]$cuts <- seq(minVal[i], maxVal[i], length.out=11)
shading_list[[i]]$levels <- cut(Values[,i],
breaks= shading_list[[i]]$cuts, include.lowest=TRUE) |> as.numeric()
shading_list[[i]]$values <- rev(colorRampAlpha(Color(quilt),
n=length( shading_list[[i]]$cuts)+1, alpha=alpha))
shading_list[[i]]$values <- colorRampAlpha(Color(quilt),
n=length( shading_list[[i]]$cuts)+1,
alpha=alpha)
}
}

Expand Down
101 changes: 101 additions & 0 deletions R/fct_plotSpider.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#' Plot `Spider`
#'
#' A Spider or Radar plot
#'
#'
#' @param slick A [Slick-class()] object
#'
#' @seealso [Slick-methods()], [Slick-class()]
#' @return A `ggplot2` object
#' @export
#'

plotSpider <- function(slick) {

if (!methods::is(slick, 'Slick'))
cli::cli_abort('`slick` must be an object of class `Slick`')

spider <- Spider(slick)
chk <- Check(spider)
if (chk@empty)
cli::cli_abort('`Spider` in this `Slick` object is empty. Use {.code Check(slick)}')

if (!chk@complete)
cli::cli_abort('`Spider` in this `Slick` object is incomplete. Use {.code Check(slick)}')


Values <- spider |>
Value() |> apply(2:3, mean, na.rm=TRUE)

nPI <- ncol(Values)
nMP <- nrow(Values)

mp_colors <- slick |> MPs() |> Color()


#######################

library(fmsb)

df <- rbind(rep(1, nPI),
rep(0, nPI),
Values) |> data.frame()
colnames(df) <- spider@Code



fmsb::radarchart(df, pcol=mp_colors)


#######################

# devtools::install_github("ricardo-bion/ggradar",
# dependencies = TRUE)

library(ggradar)

df <- data.frame(Values)
colnames(df) <- toupper(letters[seq_along(spider@Code)])
df <- df |> dplyr::mutate(MP=slick@MPs@Code) |>
dplyr::select(MP, toupper(letters[seq_along(spider@Code)]))

ggradar(df, group.point.size=0,
values.radar='',
plot.legend=FALSE) +
ggplot2::scale_color_manual(values=mp_colors)

axis.label.size = 12) +
ggplot2::theme(text=ggplot2::element_text(color='blue'))


ggplot2::theme(axis.title=ggplot2::element_text(color='blue'))


df1 <- df |> dplyr::filter(MP=='AvC')
df2 <- df |> dplyr::filter(MP=='Itarget1')
df3 <- df |> dplyr::filter(MP=='DD')

library(patchwork)
p1 <- ggradar(df1) + ggplot2::guides(color='none') +
ggplot2::scale_color_manual(values=mp_colors[1])
p2 <- ggradar(df2) + ggplot2::guides(color='none') +
ggplot2::scale_color_manual(values=mp_colors[2])
p3 <- ggradar(df3) + ggplot2::guides(color='none') +
ggplot2::scale_color_manual(values=mp_colors[3])

textDF <- data.frame(x=0, y=0, text='77.5')


p1 <- ggradar(df1, fill=TRUE,
group.point.size=0,
values.radar='') + ggplot2::guides(color='none', fill='none') +
ggplot2::scale_color_manual(values=mp_colors[1]) +
ggplot2::scale_fill_manual(values=mp_colors[1]) +
ggplot2::geom_text(data=textDF, ggplot2::aes(x=x, y=y, label=text))


p1

p1 + p2 + p3

}
2 changes: 1 addition & 1 deletion R/mod_Quilt.R
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ mod_Quilt_server <- function(id, i18n, Slick_Object, window_dims, Report, home_s
output$colortext <- renderUI({
i18n <- i18n()
hightxt <- i18n$t("highest value")
lowtxt <- i18n$t("lowest value")
lowtxt <- i18n$t(" lowest value ")
HTML(paste(i18n$t('The color shading indicates values ranging between the'),
span(hightxt, style=highcolorstyle()),
i18n$t('to the'),
Expand Down
10 changes: 5 additions & 5 deletions R/mod_Timeseries.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ mod_Timeseries_ui <- function(id){
img(src='www/img/Line.jpg', width="100%"),
uiOutput(ns('picker')),
uiOutput(ns('y_range')),
fluidRow(
column(4,uiOutput(ns('includeQuantile'))),
column(4,uiOutput(ns('includeLabels'))),
column(4,uiOutput(ns('includeHist')))
),
mod_Page_Filter_ui(ns("timeseriesfilter"))
),
column(9,
Expand Down Expand Up @@ -98,6 +93,11 @@ mod_Timeseries_server <- function(id, i18n, Slick_Object, window_dims, Report,

output$plots <- renderUI({
tagList(
fluidRow(
column(4,uiOutput(ns('includeQuantile'))),
column(4,uiOutput(ns('includeLabels'))),
column(4,uiOutput(ns('includeHist')))
),
conditionalPanel("input.plotselect=='overall'", ns=ns,
mod_Timeseries_overall_ui(ns("Timeseries_overall_1"))
),
Expand Down
130 changes: 30 additions & 100 deletions R/mod_Timeseries_byOM.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,22 @@ mod_Timeseries_byOM_server <- function(id, i18n, filtered_slick,
paste0(val, 'px')
})

nMP <- reactive({
req(filtered_slick())
slick <- filtered_slick()
length(slick@MPs@Code)
})

plot_height_calc <- reactive({
nom <- nOM()
ncol <- min(nom, 4)

nrow <- ceiling(nom/ncol)

nmp <- nMP()
if (byMP()) {
ncol <- min(nmp, 4)
nrow <- ceiling(nmp/ncol)
} else {
ncol <- min(nom, 4)
nrow <- ceiling(nom/ncol)
}
paste0(nrow*300, 'px')

})
Expand Down Expand Up @@ -90,6 +100,7 @@ mod_Timeseries_byOM_server <- function(id, i18n, filtered_slick,

plotTimeseries(timeseries(),
pm_ind(),
byMP=byMP(),
byOM=TRUE,
includeQuants =includeQuants(),
includeLabels =includeLabels(),
Expand All @@ -99,6 +110,13 @@ mod_Timeseries_byOM_server <- function(id, i18n, filtered_slick,

})

byMP <- reactive({
out <- input$byMP
if (length(out)<1)
return(FALSE)
out
})

values <- reactive({
filtered_slick() |>
Timeseries() |>
Expand All @@ -114,14 +132,19 @@ mod_Timeseries_byOM_server <- function(id, i18n, filtered_slick,
i18n <- i18n()
tagList(
fluidRow(
column(3,
column(4,
radioButtons(ns('plotoption'),
i18n$t('Plot Option'),
choiceNames = i18n$t(c('Median', 'Individual Simulation')),
choiceValues= c('median', 'sim')
)
)
),
column(4,
checkboxInput(ns('byMP'),
i18n$t('by MP?'),
FALSE)
),
column(6,
column(4,
conditionalPanel("input.plotoption=='sim'", ns=ns,
column(6,
shinyWidgets::pickerInput(
Expand Down Expand Up @@ -168,99 +191,6 @@ mod_Timeseries_byOM_server <- function(id, i18n, filtered_slick,
}, ignoreInit = TRUE)


# timeseriesplot <- reactive({
# if (is.null(filtered_slick()))
# return(NULL)
# if (is.null(pm_ind()))
# return(NULL)
# if (is.null(yrange()))
# return(NULL)
#
#
# plotTimeseries(filtered_slick(), pm_ind(),
# byOM=TRUE,
# includeHist=TRUE,
# includeQuants =includeQuants(),
# includeLabels =includeLabels()) +
# ggplot2::coord_cartesian(ylim=yrange())
#
#
#
# })
#
#
# output$page <- renderUI({
# if (!is.null(make_plots())) {
# plot_output_list <- lapply(1:nOM(), function(mm) {
# plotname <- paste("plot", mm, sep="")
# loading_spinner(plotOutput(session$ns(plotname), width=plot_width(),
# height=plot_height()))
# })
# plot_output_list$cellArgs=list(style = plot_width_text())
# do.call(flowLayout, plot_output_list)
# }
# })
#
# plot_width_calc <- reactive({
# dd <- window_dims()
# val <- (dd[1] * 0.2) |> round(0)
# paste0(val, 'px')
# })
#
# plot_height_calc <- reactive({
# dd <- window_dims()
# val <- (dd[1] * 0.2) |> round(0)
# paste0(val, 'px')
# })
#
# plot_width <- plot_width_calc |> debounce(500)
#
# plot_height <- plot_height_calc |> debounce(500)
#
# plot_width_text <- reactive({
# paste0('width: ', plot_width(), '; height: ', plot_height())
# })
#
# make_plots <- reactive({
# if (is.null(timeseries()))
# return(NULL)
# if (is.null(pm_ind()))
# return(NULL)
# if (is.null(yrange()))
# return(NULL)
# dd <- timeseries() |> Timeseries() |> Value() |> dim()
# plot_list <- list()
# if (dd[2]==nOM()) {
# om_labels <- selected_oms()
# for (i in 1:nOM()) {
# plot_list[[i]] <- plotTimeseries(timeseries(),
# pm_ind(),
# MP_ind=NULL,
# i,
# OM_label = om_labels[i])
# }
# }
# plot_list
# })
#
#
# observeEvent(make_plots(), {
# thisplot <- make_plots()
# if (length(thisplot)>0) {
# for (i in 1:nOM()) {
# local({
# my_i <- i
# plotname <- paste("plot", my_i, sep="")
# output[[plotname]] <- renderPlot({
# thisplot[[my_i]] +
# ggplot2::coord_cartesian(ylim=yrange())
# })
# })
# }
# }
# })


})
}

Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ reference:
- plotBoxplot
- plotKobe
- plotQuilt
- plotSpider
- plotTimeseries
- title: 'Other'
desc: >
Expand Down
40 changes: 40 additions & 0 deletions vignettes/articles/DevGuide.R
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,43 @@ Quilt(slick) <- quilt

## ---- quilt_plot ----

plotQuilt(slick)
plotQuilt(slick, kable = TRUE, minmax = TRUE, shading=FALSE)


## ---- spider_create ----

spider <- Spider(Code=c('P100',
'P50',
'PNOF'),
Label=c('Prob SB>SBMSY',
'Prob SB>0.5SBMSY',
'Prob. Not Overfishing'),
Description = c('Probability spawning biomass is greater than SB_MSY over the projection period',
'Probability spawning biomass is greater than 0.5 SB_MSY over the projection period',
'Probability of not overfishing over the projection period'))


## ---- spider_value ----

nPI <- length(Code(spider))

Value(spider) <- array(NA, dim=c(nOM, nMP, nPI))

Value(spider)[1,,1] <- apply(MSE_Base@SB_SBMSY > 1 & MSE_Base@F_FMSY < 1, 2, mean)
Value(spider)[2,,1] <- apply(MSE_LowM@SB_SBMSY > 1 & MSE_LowM@F_FMSY < 1, 2, mean)
Value(spider)[3,,1] <- apply(MSE_HighM@SB_SBMSY > 1 & MSE_HighM@F_FMSY < 1, 2, mean)

Value(spider)[1,,2] <- apply(MSE_Base@SB_SBMSY > 0.5, 2, mean)
Value(spider)[2,,2] <- apply(MSE_LowM@SB_SBMSY > 0.5, 2, mean)
Value(spider)[3,,2] <- apply(MSE_HighM@SB_SBMSY > 0.5, 2, mean)

Value(spider)[1,,3] <- apply(MSE_Base@F_FMSY < 1, 2, mean)
Value(spider)[2,,3] <- apply(MSE_LowM@F_FMSY < 1, 2, mean)
Value(spider)[3,,3] <- apply(MSE_HighM@F_FMSY < 1, 2, mean)


## ---- spider_add ----

## ---- spider_plot ----

0 comments on commit 484e282

Please sign in to comment.