Skip to content

Commit 94a77c9

Browse files
author
Jordan S Read
committed
Merge pull request #67 from ldecicco-USGS/master
Legend NULL fix
2 parents 99b850d + 0050da5 commit 94a77c9

File tree

9 files changed

+119
-50
lines changed

9 files changed

+119
-50
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
S3method(gsplot,list)
44
S3method(print,gsplot)
55
export("%>%")
6+
export(abline)
67
export(axis)
78
export(calc_views)
89
export(gsplot)

R/abline.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' gsplot abline
2+
#'
3+
#' abline stuff
4+
#'
5+
#' @details Add additional functionality to points.
6+
#'
7+
#' @param object gsplot object
8+
#' @param \dots Further graphical parameters may also be supplied as arguments. See 'Details'.
9+
#' @return modified gsplot object
10+
#' @export
11+
abline <- function(object, ...) {
12+
overrideGraphics("abline", object, ...)
13+
}
14+
15+
16+
abline.gsplot <- function(object, x, y=NULL, ..., legend.name=NULL, side=c(1,2)){
17+
current_list <- config("abline")
18+
arguments <- list(x=x, y=y, ...)
19+
20+
indicesToAdd <- !(names(current_list) %in% names(arguments))
21+
arguments <- append(arguments, current_list[indicesToAdd])
22+
23+
object <- append(object, list(points = list(arguments = arguments,
24+
gs.config=list(legend.name = legend.name,
25+
side = side))))
26+
return(gsplot(object))
27+
}

R/calc_views.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
#' calc_view
2+
#'
3+
#' calc_view
4+
#'
5+
#'
6+
#' @param gsplot object
17
#' @export
8+
#' @keywords internal
29
calc_views <- function(gsplot){
310

411
views <- group_views(gsplot)

R/legend.R

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Example Lines", lty=5, col="orange") %>%
1919
#' points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") %>%
2020
#' 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!!!")
2222
#' topright
2323
#'
2424
#' defaultLegend <- gsplot(list()) %>%
@@ -57,8 +57,8 @@
5757
#' points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") %>%
5858
#' points(x=3, y=4, side=c(1,4), legend.name="Example Points 2", pch=5, col="red") %>%
5959
#' 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")
6262
#' toleft
6363
legend <- function(object, ...){
6464
overrideGraphics("legend", object, ...)
@@ -110,20 +110,15 @@ draw_legend <- function(gsplot) {
110110
line = numeric(),
111111
stringsAsFactors = FALSE)
112112
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)
121121
}
122-
return(data.frame(text = newText,
123-
symbol = newSymbol,
124-
color = newColor,
125-
line = newLine,
126-
stringsAsFactors = FALSE))
127122
}
128123

129124
#get legend entries for points
@@ -157,38 +152,41 @@ draw_legend <- function(gsplot) {
157152

158153
smartLegend <- unique(smartLegend)
159154

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){
166156

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+
))
186169
}
170+
187171
legendParams <- append(legendParams, list(
188-
ncol=cols
172+
legend=smartLegend$text,
173+
col=smartLegend$color
189174
))
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)
190189
}
191-
legend(legendParams)
192190
}
193191
}
194192

R/points.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#' gsplot points
22
#'
33
#' Point stuff
4-
#' @usage
5-
#' points(object, legend.name=NULL, side=c(1,2), ...)
64
#'
75
#' @details Add additional functionality to points.
86
#'
97
#' @param object gsplot object
108
#' @param \dots Further graphical parameters may also be supplied as arguments. See 'Details'.
119
#' @return modified gsplot object
12-
#' @rdname points
1310
#' @examples
1411
#' gs <- gsplot(list())
1512
#' gsNew <- points(gs, y=1, x=2, col="blue", pch=18)

man/abline.Rd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/abline.R
3+
\name{abline}
4+
\alias{abline}
5+
\title{gsplot abline}
6+
\usage{
7+
abline(object, ...)
8+
}
9+
\arguments{
10+
\item{object}{gsplot object}
11+
12+
\item{\dots}{Further graphical parameters may also be supplied as arguments. See 'Details'.}
13+
}
14+
\value{
15+
modified gsplot object
16+
}
17+
\description{
18+
abline stuff
19+
}
20+
\details{
21+
Add additional functionality to points.
22+
}
23+

man/calc_views.Rd

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/calc_views.R
3+
\name{calc_views}
4+
\alias{calc_views}
5+
\title{calc_view}
6+
\usage{
7+
calc_views(gsplot)
8+
}
9+
\arguments{
10+
\item{gsplot}{object}
11+
}
12+
\description{
13+
calc_view
14+
}
15+
\keyword{internal}
16+

man/legend.Rd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ topright <- gsplot(list()) \%>\%
2929
lines(x=c(3,4,3), y=c(2,4,6), legend.name="Example Lines", lty=5, col="orange") \%>\%
3030
points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") \%>\%
3131
points(x=3, y=4, side=c(1,4), legend.name="Example Points 2", pch=5, col="red") \%>\%
32-
legend(location="topright")
32+
legend(location="topright", title="LEGEND!!!")
3333
topright
3434

3535
defaultLegend <- gsplot(list()) \%>\%
@@ -68,8 +68,8 @@ toleft <- gsplot(list()) \%>\%
6868
points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") \%>\%
6969
points(x=3, y=4, side=c(1,4), legend.name="Example Points 2", pch=5, col="red") \%>\%
7070
lines(x=c(3,4,3), y=c(2,4,6), legend.name="Example Lines 1", lty=5, col="orange") \%>\%
71-
lines(x=c(1,2,5), y=c(1,8,5), legend.name="Example Lines 2", lty=5, col="green") \%>\%
72-
legend(location="toleft")
71+
lines(x=c(1,2,5), y=c(1,8,5), lty=5, col="green") \%>\%
72+
legend(location="below")
7373
toleft
7474
}
7575

man/points.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
\alias{points}
55
\title{gsplot points}
66
\usage{
7-
points(object, legend.name=NULL, side=c(1,2), ...)
7+
points(object, ...)
88
}
99
\arguments{
1010
\item{object}{gsplot object}

0 commit comments

Comments
 (0)