Skip to content

Commit 3875d7e

Browse files
authored
Merge pull request #132 from MathMarEcol/devel
Devel
2 parents 3a1ef34 + 134b53b commit 3875d7e

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

R/splnr_featureRep.R

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,14 @@ splnr_get_featureRep <- function(soln, pDat, targets = NA,
6767

6868
ns1 <- dplyr::left_join(area_feature, selected_feature, by = "feature") %>%
6969
dplyr::mutate(
70-
relative_held = (.data$absolute_held / .data$total_amount),
71-
incidental = TRUE
70+
relative_held = (.data$absolute_held / .data$total_amount)
7271
)
7372
} else {
7473
ns1 <- tibble::tibble(
7574
feature = "DummyVar",
7675
total_amount = 0,
7776
absolute_held = 0,
78-
relative_held = 0,
79-
incidental = TRUE
77+
relative_held = 0
8078
)
8179
}
8280

@@ -121,8 +119,7 @@ splnr_get_featureRep <- function(soln, pDat, targets = NA,
121119

122120
s1 <- s1 %>%
123121
dplyr::mutate(
124-
relative_held = .data$relative_held,
125-
incidental = FALSE
122+
relative_held = .data$relative_held
126123
) %>%
127124
stats::na.omit()
128125

@@ -134,6 +131,10 @@ splnr_get_featureRep <- function(soln, pDat, targets = NA,
134131
df <- s1
135132
}
136133

134+
# Now add in incidental for 0 and NA targets
135+
df <- df %>%
136+
dplyr::mutate(incidental = dplyr::if_else(target > 0 & absolute_held > 0, FALSE, TRUE, missing = TRUE))
137+
137138
return(df)
138139
}
139140

@@ -257,9 +258,12 @@ splnr_plot_featureRep <- function(df,
257258
) %>%
258259
tibble::deframe()
259260

261+
260262
gg_target <- ggplot2::ggplot() +
261-
ggplot2::geom_bar(data = df, stat = "identity", ggplot2::aes(x = .data$feature, y = .data$relative_held, fill = .data$category), na.rm = TRUE) +
262-
ggplot2::geom_bar(data = df %>% dplyr::filter(.data$incidental == TRUE), stat = "identity", ggplot2::aes(x = .data$feature, y = .data$relative_held), na.rm = TRUE, fill = "NA", colour = "black") +
263+
ggplot2::geom_bar(data = df %>% dplyr::mutate(relative_held = dplyr::if_else(incidental == TRUE, NA, relative_held)), stat = "identity", position = "identity", ggplot2::aes(x = .data$feature, y = .data$relative_held, fill = .data$category), na.rm = TRUE) +
264+
ggplot2::geom_bar(data = df %>% dplyr::mutate(relative_held = dplyr::if_else(incidental == FALSE, NA, relative_held)),
265+
stat = "identity", position = "identity",
266+
ggplot2::aes(x = .data$feature, y = .data$relative_held), na.rm = TRUE, fill = "NA", colour = "black") +
263267
ggplot2::labs(title = plotTitle, x = "Feature", y = "Representation of features \nin total selected area (%)") +
264268
ggplot2::theme_bw() +
265269
ggplot2::scale_y_continuous(limits = c(0, ymax <- max(df$relative_held, na.rm = TRUE) + 10), expand = c(0, 0)) + # only works for min shortfall without incidental yet
@@ -275,7 +279,9 @@ splnr_plot_featureRep <- function(df,
275279
legend.title = ggplot2::element_blank(),
276280
legend.text = ggplot2::element_text(size = 16),
277281
legend.position = "inside",
278-
legend.position.inside = c(0.5, 0.95),
282+
# legend.margin = ggplot2::margin(0, 0, 0, 0),
283+
# legend.justification.top = "centre",
284+
legend.position.inside = c(0.5, 0.92),
279285
legend.direction = "horizontal",
280286
legend.background = ggplot2::element_rect(fill = "NA"),
281287
title = ggplot2::element_text(size = 16),

R/splnr_plot.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ splnr_plot <- function(df,
155155
} else if (is_continuous) {
156156

157157
gg <- gg +
158-
ggplot2::geom_sf(data = df, ggplot2::aes(fill = .data[[col_names]]), colour = "grey80", linewidth = 0.01) +
159-
ggplot2::scale_fill_viridis_c(name = legend_title) +
160-
ggplot2::guides(fill = ggplot2::guide_colourbar(order = 1))
158+
ggplot2::geom_sf(data = df, ggplot2::aes(fill = .data[[col_names]], colour = .data[[col_names]])) +
159+
ggplot2::scale_fill_viridis_c(name = legend_title, aesthetics = c("colour", "fill")) +
160+
ggplot2::guides(fill = ggplot2::guide_colourbar(order = 1),
161+
colour = "none")
161162

162163
} else if (is.null(col_names)){ # No column to plot by
163164

0 commit comments

Comments
 (0)