|
18 | 18 | #' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Example Lines", lty=5, col="orange") %>% |
19 | 19 | #' points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") %>% |
20 | 20 | #' points(x=3, y=4, side=c(1,4), legend.name="Example Points 2", pch=5, col="red") %>% |
21 | | -#' legend(location="topright") |
| 21 | +#' legend(location="topright", title="LEGEND!!!") |
22 | 22 | #' topright |
23 | 23 | #' |
24 | 24 | #' defaultLegend <- gsplot(list()) %>% |
|
57 | 57 | #' points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") %>% |
58 | 58 | #' points(x=3, y=4, side=c(1,4), legend.name="Example Points 2", pch=5, col="red") %>% |
59 | 59 | #' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Example Lines 1", lty=5, col="orange") %>% |
60 | | -#' lines(x=c(1,2,5), y=c(1,8,5), legend.name="Example Lines 2", lty=5, col="green") %>% |
61 | | -#' legend(location="toleft") |
| 60 | +#' lines(x=c(1,2,5), y=c(1,8,5), lty=5, col="green") %>% |
| 61 | +#' legend(location="below") |
62 | 62 | #' toleft |
63 | 63 | legend <- function(object, ...){ |
64 | 64 | overrideGraphics("legend", object, ...) |
@@ -110,20 +110,15 @@ draw_legend <- function(gsplot) { |
110 | 110 | line = numeric(), |
111 | 111 | stringsAsFactors = FALSE) |
112 | 112 | getLegendItem <- function(newText, newSymbol, newColor, newLine) { |
113 | | - if(is.null(newText)) { |
114 | | - if(!is.na(newSymbol)) { |
115 | | - newText <- "Plotted Points" |
116 | | - } |
117 | | - |
118 | | - if(!is.na(newLine)) { |
119 | | - newText <- "Plotted Line" |
120 | | - } |
| 113 | + if(!is.null(newText)) { |
| 114 | + return(data.frame(text = newText, |
| 115 | + symbol = newSymbol, |
| 116 | + color = newColor, |
| 117 | + line = newLine, |
| 118 | + stringsAsFactors = FALSE)) |
| 119 | + } else { |
| 120 | + return(NULL) |
121 | 121 | } |
122 | | - return(data.frame(text = newText, |
123 | | - symbol = newSymbol, |
124 | | - color = newColor, |
125 | | - line = newLine, |
126 | | - stringsAsFactors = FALSE)) |
127 | 122 | } |
128 | 123 |
|
129 | 124 | #get legend entries for points |
@@ -157,38 +152,41 @@ draw_legend <- function(gsplot) { |
157 | 152 |
|
158 | 153 | smartLegend <- unique(smartLegend) |
159 | 154 |
|
160 | | - #only include pch if we have a non-NA entry for points |
161 | | - if(length(pts_i) > 0) { |
162 | | - legendParams <- append(legendParams, list( |
163 | | - pch=smartLegend$symbol |
164 | | - )) |
165 | | - } |
| 155 | + if(nrow(smartLegend) > 0){ |
166 | 156 |
|
167 | | - #only include lty if we have a non-NA entry for lines |
168 | | - if(length(lines_i) > 0) { |
169 | | - legendParams <- append(legendParams, list( |
170 | | - lty=smartLegend$line |
171 | | - )) |
172 | | - } |
173 | | - |
174 | | - legendParams <- append(legendParams, list( |
175 | | - legend=smartLegend$text, |
176 | | - col=smartLegend$color |
177 | | - )) |
178 | | - |
179 | | - #for above/below, dynamically set the number of columns |
180 | | - location <- gsplot[['legend']][['gs.config']][['location']] |
181 | | - if(location == "below" || location == "above") { |
182 | | - itemsPerCol <- 3 #TODO load this from config |
183 | | - cols <- NROW(smartLegend) %/% 3; |
184 | | - if(NROW(smartLegend) %% 3 > 0) { |
185 | | - cols <- cols + 1 |
| 157 | + #only include pch if we have a non-NA entry for points |
| 158 | + if(length(pts_i) > 0) { |
| 159 | + legendParams <- append(legendParams, list( |
| 160 | + pch=smartLegend$symbol |
| 161 | + )) |
| 162 | + } |
| 163 | + |
| 164 | + #only include lty if we have a non-NA entry for lines |
| 165 | + if(length(lines_i) > 0) { |
| 166 | + legendParams <- append(legendParams, list( |
| 167 | + lty=smartLegend$line |
| 168 | + )) |
186 | 169 | } |
| 170 | + |
187 | 171 | legendParams <- append(legendParams, list( |
188 | | - ncol=cols |
| 172 | + legend=smartLegend$text, |
| 173 | + col=smartLegend$color |
189 | 174 | )) |
| 175 | + |
| 176 | + #for above/below, dynamically set the number of columns |
| 177 | + location <- gsplot[['legend']][['gs.config']][['location']] |
| 178 | + if(location == "below" || location == "above") { |
| 179 | + itemsPerCol <- 3 #TODO load this from config |
| 180 | + cols <- NROW(smartLegend) %/% 3; |
| 181 | + if(NROW(smartLegend) %% 3 > 0) { |
| 182 | + cols <- cols + 1 |
| 183 | + } |
| 184 | + legendParams <- append(legendParams, list( |
| 185 | + ncol=cols |
| 186 | + )) |
| 187 | + } |
| 188 | + legend(legendParams) |
190 | 189 | } |
191 | | - legend(legendParams) |
192 | 190 | } |
193 | 191 | } |
194 | 192 |
|
|
0 commit comments