Skip to content

Commit 8dd3be0

Browse files
committed
Improve legend
1 parent a556df8 commit 8dd3be0

1 file changed

Lines changed: 33 additions & 20 deletions

File tree

R/plot_dynamic.R

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222
#'
2323
plot_dynamic <- function(
2424
dpp, lakeName = "", ylab = "Index", pixelClusters = NULL, ylim = NULL,
25-
smallBandOnly = FALSE
25+
singlePixels = FALSE, smallBandOnly = FALSE
2626
){
2727

2828
cv <- lakeRS::tenClusterColors$color
29-
29+
3030
polygon_color <- apply(X = col2rgb(cv),
31-
MARGIN = 2,
32-
FUN = function(x){
33-
rgb(red = x["red"], green = x["green"], blue = x["blue"],
34-
alpha = 80, maxColorValue = 255)
35-
}
31+
MARGIN = 2,
32+
FUN = function(x){
33+
rgb(red = x["red"], green = x["green"], blue = x["blue"],
34+
alpha = 80, maxColorValue = 255)
35+
}
3636
)
3737

3838
all_values <- dpp$moving_averages[,-1]
@@ -56,31 +56,43 @@ plot_dynamic <- function(
5656

5757
if(is.null(ylim)){
5858
if(smallBandOnly){
59-
ylim = range(value_stats) * c(1, 1.1)
59+
yrange <- range(c(value_stats["25%",], value_stats["75%",]))
6060
} else {
61-
ylim = range(all_values, na.rm = TRUE) * c(1, 1.1)
61+
if(singlePixels){
62+
yrange <- range(all_values, na.rm = TRUE)
63+
} else {
64+
yrange <- range(c(value_stats["5%",], value_stats["95%",]))
65+
}
6266
}
67+
ylim <- c(yrange[1] - diff(yrange) * 0.1, yrange[2] + diff(yrange) * 0.1)
68+
6369
}
6470
plot(x = 1:365, y = all_values[,1], type = "n",
6571
ylab = ylab, xlab = "Day of the Year",
6672
ylim = ylim,
67-
main = lakeName, xaxs = "i")
73+
main = paste0(lakeName, ifelse(
74+
smallBandOnly,
75+
yes = " (Median and 50% Interval)",
76+
no = " (Median and 90% Interval)")),
77+
xaxs = "i")
6878

6979
abline(v = v_lines, lty = "dashed")
7080
mtext(text = m_text, side = 3, line = 0.5, at = m_position)
7181
if(is.null(pixelClusters)){
7282
polygon(x = c(1:365, 365:1),
7383
y = c(value_stats["5%",], rev(value_stats["95%",])),
7484
border = NA, col = polygon_color[1], )
75-
nLines <- ncol(all_values)
76-
i_cols <- 1:ncol(all_values)
77-
if(nLines > 5000){
78-
i_cols <- sample(nLines, size = 5000)
79-
nLines <- length(i_cols)
80-
}
81-
op <- 1 / (ifelse(nLines > 40, yes = nLines, no = 40)/40)
82-
for(i in i_cols){
83-
lines(x = 1:365, y = all_values[,i], col = rgb(0,0,0,op))
85+
if(singlePixels){
86+
nLines <- ncol(all_values)
87+
i_cols <- 1:ncol(all_values)
88+
if(nLines > 5000){
89+
i_cols <- sample(nLines, size = 5000)
90+
nLines <- length(i_cols)
91+
}
92+
op <- 1 / (ifelse(nLines > 40, yes = nLines, no = 40)/40)
93+
for(i in i_cols){
94+
lines(x = 1:365, y = all_values[,i], col = rgb(0,0,0,op))
95+
}
8496
}
8597

8698
lines(x = 1:365, y = value_stats["50%",], lwd = 2, col = cv[1])
@@ -110,7 +122,8 @@ plot_dynamic <- function(
110122
bg = "black",
111123
box.lwd = NA,
112124
cex = 0.9,
113-
text.col = "white"
125+
text.col = "white",
126+
ncol = 5
114127
)
115128
}
116129
}

0 commit comments

Comments
 (0)