77# ' @param \dots normal legend params should forward through
88# ' @return modified gsplot object
99# ' @export
10+ # ' @importFrom graphics par
1011# ' @examples
11- # ' bottom <- gsplot(list() ) %>%
12- # ' points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") %>%
13- # ' points(x=3, y=4, side=c(1,4), legend.name="Example Points 2", pch=5, col="red") %>%
12+ # ' bottom <- gsplot() %>%
13+ # ' points(x=1, y=2, side=c(3,2), legend.name="Points 1", pch=1, col="blue") %>%
14+ # ' points(x=3, y=4, side=c(1,4), legend.name="Points 2", pch=5, col="red") %>%
1415# ' legend(location="bottom")
1516# ' bottom
1617# '
17- # ' topright <- gsplot(list() ) %>%
18- # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Example Lines", lty=5, col="orange") %>%
19- # ' points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") %>%
20- # ' points(x=3, y=4, side=c(1,4), legend.name="Example Points 2", pch=5, col="red") %>%
18+ # ' topright <- gsplot() %>%
19+ # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Lines", lty=5, col="orange") %>%
20+ # ' points(x=1, y=2, side=c(3,2), legend.name="Points 1", pch=1, col="blue") %>%
21+ # ' points(x=3, y=4, side=c(1,4), legend.name="Points 2", pch=5, col="red") %>%
2122# ' legend(location="topright", title="LEGEND!!!")
2223# ' topright
2324# '
24- # ' defaultLegend <- gsplot(list() ) %>%
25+ # ' defaultLegend <- gsplot() %>%
2526# ' points(x=1, y=2, side=c(3,2)) %>%
2627# ' points(x=3, y=4, side=c(1,4)) %>%
2728# ' lines(x=c(3,4,3), y=c(2,4,6)) %>%
2829# ' lines(x=c(1,2,5), y=c(1,8,5)) %>%
2930# ' legend()
3031# ' defaultLegend
3132# '
32- # ' above <- gsplot(list() ) %>%
33- # ' points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") %>%
34- # ' points(x=3, y=4, side=c(1,4), legend.name="Example Points 2", pch=5, col="red") %>%
35- # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Example Lines 1", lty=5, col="orange") %>%
36- # ' lines(x=c(1,2,5), y=c(1,8,5), legend.name="Example Lines 2", lty=5, col="green") %>%
33+ # ' above <- gsplot() %>%
34+ # ' points(x=1, y=2, side=c(3,2), legend.name="Points 1", pch=1, col="blue") %>%
35+ # ' points(x=3, y=4, side=c(1,4), legend.name="Points 2", pch=5, col="red") %>%
36+ # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Lines 1", lty=5, col="orange") %>%
37+ # ' lines(x=c(1,2,5), y=c(1,8,5), legend.name="Lines 2", lty=5, col="green") %>%
3738# ' legend(location="above")
3839# ' above
3940# '
40- # ' below <- gsplot(list() ) %>%
41- # ' points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") %>%
42- # ' points(x=3, y=4, side=c(1,4), legend.name="Example Points 2", pch=5, col="red") %>%
43- # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Example Lines 1", lty=5, col="orange") %>%
44- # ' lines(x=c(1,2,5), y=c(1,8,5), legend.name="Example Lines 2", lty=5, col="green") %>%
41+ # ' below <- gsplot() %>%
42+ # ' points(x=1, y=2, side=c(3,2), legend.name="Points 1", pch=1, col="blue") %>%
43+ # ' points(x=3, y=4, side=c(1,4), legend.name="Points 2", pch=5, col="red") %>%
44+ # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Lines 1", lty=5, col="orange") %>%
45+ # ' lines(x=c(1,2,5), y=c(1,8,5), legend.name="Lines 2", lty=5, col="green") %>%
4546# ' legend(location="below")
4647# ' below
4748# '
48- # ' toright <- gsplot(list() ) %>%
49- # ' points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") %>%
50- # ' points(x=3, y=4, side=c(1,4), legend.name="Example Points 1", pch=1, col="blue") %>%
51- # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Example Lines 1", lty=5) %>%
52- # ' lines(x=c(1,2,5), y=c(1,8,5), legend.name="Example Lines 2", lty=5, col="green") %>%
49+ # ' toright <- gsplot() %>%
50+ # ' points(x=1, y=2, side=c(3,2), legend.name="Points 1", pch=1, col="blue") %>%
51+ # ' points(x=3, y=4, side=c(1,4), legend.name="Points 1", pch=1, col="blue") %>%
52+ # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Lines 1", lty=5) %>%
53+ # ' lines(x=c(1,2,5), y=c(1,8,5), legend.name="Lines 2", lty=5, col="green") %>%
5354# ' legend(location="toright")
5455# ' toright
5556# '
56- # ' toleft <- gsplot(list() ) %>%
57- # ' points(x=1, y=2, side=c(3,2), legend.name="Example Points 1", pch=1, col="blue") %>%
58- # ' points(x=3, y=4, side=c(1,4), legend.name="Example Points 2", pch=5, col="red") %>%
59- # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Example Lines 1", lty=5, col="orange") %>%
57+ # ' toleft <- gsplot() %>%
58+ # ' points(x=1, y=2, side=c(3,2), legend.name="Points 1", pch=1, col="blue") %>%
59+ # ' points(x=3, y=4, side=c(1,4), legend.name="Points 2", pch=5, col="red") %>%
60+ # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Lines 1", lty=5, col="orange") %>%
6061# ' lines(x=c(1,2,5), y=c(1,8,5), lty=5, col="green") %>%
6162# ' legend(location="below")
6263# ' toleft
64+ # '
65+ # ' usrDef <- gsplot() %>%
66+ # ' points(x=1, y=2, side=c(3,2), legend.name="Points 1", cex=3) %>%
67+ # ' points(x=3, y=4, side=c(1,4), legend.name="Points 2", pch=5, col="red") %>%
68+ # ' lines(x=c(3,4,3), y=c(2,4,6), legend.name="Lines 1", lty=5, col="orange") %>%
69+ # ' lines(x=c(1,2,5), y=c(1,8,5), legend.name="Lines 2", lwd=3) %>%
70+ # ' legend(x=3,y=4)
71+ # ' usrDef
6372legend <- function (object , ... ){
6473 overrideGraphics(" legend" , object , ... )
6574}
6675
6776
6877legend.gsplot <- function (object , location = " topright" , legend_offset = 0.3 , ... ) {
6978 arguments <- list (... )
79+
80+ if (" x" %in% names(arguments )){
81+ location <- arguments $ x
82+ }
83+
7084 gsConfig <- list (location = location , legend_offset = legend_offset )
7185
7286 arguments <- appendLegendPositionConfiguration(location , gsConfig , arguments )
@@ -88,6 +102,8 @@ appendLegendPositionConfiguration <- function(location, gsConfig, arguments) {
88102 return (append(arguments , list (x = " right" , y = NULL , inset = c(- legend_offset , 0 ), bty = " n" )))
89103 } else if (location == " toleft" ) {
90104 return (append(arguments , list (x = " left" , y = NULL , inset = c(- legend_offset , 0 ), bty = " n" )))
105+ } else if (" x" %in% names(arguments )){
106+ return (arguments )
91107 } else {
92108 return (append(arguments , list (x = location )))
93109 }
@@ -137,7 +153,7 @@ draw_legend <- function(gsplot) {
137153 }
138154
139155 # get legend entries for lines
140- lines_i <- which(names(gsplot ) %in% ' lines' )
156+ lines_i <- which(names(gsplot ) %in% c( ' lines' , ' abline ' ) )
141157 for (i in lines_i ){
142158 lines <- gsplot [[i ]]
143159 if (all((c(" lty" ," col" ) %in% names(lines [[' arguments' ]])))){
@@ -152,6 +168,11 @@ draw_legend <- function(gsplot) {
152168
153169 smartLegend <- unique(smartLegend )
154170
171+ lineTypes <- c(" blank" , " solid" , " dashed" , " dotted" , " dotdash" , " longdash" , " twodash" )
172+
173+ lineNums <- suppressWarnings(as.numeric(smartLegend $ line ))
174+ smartLegend $ line [! is.na(lineNums )] <- lineTypes [lineNums + 1 ][! is.na(lineTypes [lineNums + 1 ])]
175+
155176 if (nrow(smartLegend ) > 0 ){
156177
157178 # only include pch if we have a non-NA entry for points
0 commit comments