Skip to content

Commit cc104fa

Browse files
Adding gg options, cf_grid contour options
1 parent acc4c9d commit cc104fa

File tree

7 files changed

+61
-56
lines changed

7 files changed

+61
-56
lines changed

R/cf.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#' @param ... Arguments to be passed to cf_func or cf_data based on
55
#' data type of first argument. If D is given as argument, then it
66
#' is passed to cf_highdim.
7+
#' @param gg Should ggplot2 be used instead of base graphics?
78
#'
8-
#' @return Whatever is returned from other function, probably nothing
9+
#' @return Whatever is returned from other function, probably nothing.
10+
#' Will be a ggplot2 object if using gg=TRUE.
911
#' @export
1012
#'
1113
#' @examples
@@ -15,16 +17,16 @@
1517
#' z <- exp(-(x-.5)^2-5*(y-.5)^2)# + rnorm(20,0,.05)
1618
#' cf(x,y,z)
1719
#' cf(function(x){x[1]^2 - x[2]}, D=3)
18-
cf <- function(...) {
20+
cf <- function(..., gg=FALSE) {
1921
dots <- list(...)
2022
if (is.function(dots[[1]])) {
2123
if ("D" %in% names(dots)) {
2224
cf_highdim(...)
2325
} else {
24-
cf_func(...)
26+
cf_func(..., gg=gg)
2527
}
2628
} else if (is.numeric(dots[[1]])) {
27-
cf_data(...)
29+
cf_data(..., gg=gg)
2830
} else {
2931
stop("Data not recognized. Use cf_func for function or
3032
cf_data for data or cf_grid for full grid of data.")

R/cf_func.R

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
#' #@param title Title for the plot
1818
#' #@param mainminmax_minmax Whether [min,max]= should be shown in title or just the numbers
1919
#' @param pts Points to plot on top of contour
20-
#' @param use_lines If the contour should be made with lines. Otherwise is made
21-
#' using colors. Defaults to colors.
20+
#' @param gg Should ggplot2 be used? Will use gcf_grid() instead of cf_grid().
2221
#' @param ... Passed to cf_grid
2322
#' @examples
2423
#' cf_func(function(x){x[1]*x[2]})
2524
#' cf_func(function(x)(exp(-(x[1]-.5)^2-5*(x[2]-.5)^2)))
2625
#' cf_func(function(xx){exp(-sum((xx-.5)^2/.1))}, bar=TRUE)
2726
#' cf_func(function(xx){exp(-sum((xx-.5)^2/.1))}, bar=TRUE, mainminmax=TRUE)
28-
#' cf_func(function(x)(exp(-(x[1]-.5)^2-5*(x[2]-.5)^2)), use_lines=TRUE)
27+
#' cf_func(function(x)(exp(-(x[1]-.5)^2-5*(x[2]-.5)^2)), with_lines=TRUE)
2928
#' @references
3029
#' [1] filled.contour R function, copied function but removed part for sidebar
3130
#' @references
@@ -36,7 +35,7 @@ cf_func <- function(fn0, n=100,
3635
batchmax=1, out.col.name=NULL,
3736
out.name=NULL,
3837
pts=NULL,
39-
use_lines=FALSE,
38+
gg=FALSE,
4039
...) {
4140
if(!is.null(out.col.name)) {
4241
fn <- function(xx){fn0(xx)[,out.col.name]}
@@ -49,32 +48,9 @@ cf_func <- function(fn0, n=100,
4948
x <- seq(xlim[1],xlim[2],length.out = n)
5049
y <- seq(ylim[1],ylim[2],length.out = n)
5150
z <- eval_over_grid_with_batch(x, y, fn, batchmax)
52-
# z <- matrix(NA,n,n)
53-
# if(batchmax<=1) { # calculate single Z value at a time
54-
# #for(xi in 1:n) for(yi in 1:n) {z[xi,yi] <- fn(c(x[xi],y[yi]))}
55-
# fn_outer <- Vectorize(function(xi, yi) {fn(c(x[xi], y[yi]))})
56-
# z <- outer(1:n, 1:n, fn_outer)
57-
# } else {
58-
# inbatch <- 0
59-
# for(xi in 1:n) {
60-
# for(yi in 1:n) {
61-
# if(inbatch==0) XYi <- matrix(c(xi,yi),ncol=2)
62-
# else XYi <- rbind(XYi,matrix(c(xi,yi),ncol=2))
63-
# inbatch <- inbatch + 1
64-
# if(inbatch == batchmax | (xi==n & yi==n)) {
65-
# Zbatch <- fn(matrix(c(x[XYi[,1]],y[XYi[,2]]),ncol=2,byrow=F))
66-
# for(rowbatch in 1:length(Zbatch)) {
67-
# z[XYi[rowbatch,1],XYi[rowbatch,2]] <- Zbatch[rowbatch]
68-
# }
69-
# inbatch <- 0
70-
# rm(XYi)
71-
# }
72-
# }
73-
# }
74-
# }
75-
if (use_lines) {
76-
contour(x, y, z, ...)
77-
points(pts, pch=19)
51+
52+
if (gg) {
53+
gcf_grid(x,y,z, pts=pts, ...)
7854
} else {
7955
cf_grid(x,y,z, pts=pts, ...)
8056
}

R/cf_grid_screen.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#' @param xlim x limits for the plot.
1515
#' @param ylim y limits for the plot.
1616
#' @param zlim z limits for the plot.
17+
#' @param with_lines Should lines be added on top of
18+
#' contour to show contours?
19+
#' @param lines_only Should no fill be used, only contour lines?
1720
#' @param levels a set of levels which are used to partition the range of z. Must be strictly increasing (and finite). Areas with z values between consecutive levels are painted with the same color.
1821
#' @param nlevels if levels is not specified, the range of z, values is divided into approximately this many levels.
1922
#' @param color.palette a color palette function to be used to assign colors
@@ -81,6 +84,7 @@ cf_grid <-
8184
cex.main=par()$cex.main,
8285
par.list=NULL,
8386
xaxis=TRUE, yaxis=TRUE,
87+
with_lines=FALSE, lines_only=FALSE,
8488
...)
8589
{#browser()
8690
# filled.contour gives unnecessary legend, this function removes it
@@ -201,8 +205,10 @@ cf_grid <-
201205
storage.mode(z) <- "double"
202206
#.Internal(filledcontour(as.double(x), as.double(y), z, as.double(levels),
203207
# col = col))
204-
.filled.contour(as.double(x), as.double(y), z, as.double(levels),
205-
col = col)
208+
if (!lines_only) {
209+
.filled.contour(as.double(x), as.double(y), z, as.double(levels),
210+
col = col)
211+
}
206212
# Something like this will remove axis numbers and ticks
207213
# Axis(x, side=1, labels=F, tick=F)
208214
if (missing(plot.axes)) {
@@ -227,6 +233,12 @@ cf_grid <-
227233
cex.main=cex.main)
228234
}
229235

236+
# Add contour lines if required
237+
if (with_lines || lines_only) {
238+
contour(x=x, y=y, z=z, add=T)
239+
}
240+
241+
# Add points (pts) if given
230242
if (!is.null(pts)) {
231243
if (!is.matrix(pts)) { # if not a matrix, make it a matrix by row
232244
if (is.numeric(pts) && (length(pts)%%2==0)) {

R/gcf_func.R

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,25 @@ gcf_func <- function(fn0, n=100,
3131
out.name=NULL,
3232
pts=NULL,
3333
...) {
34-
if(!is.null(out.col.name)) {
35-
fn <- function(xx){fn0(xx)[,out.col.name]}
36-
} else if (!is.null(out.name)) {
37-
fn <- function(xx){fn0(xx)[[out.name]]}
38-
} else {
39-
fn <- fn0
40-
}
41-
if (!is.null(xylim)) {xlim <- ylim <- xylim}
42-
x <- seq(xlim[1],xlim[2],length.out = n)
43-
y <- seq(ylim[1],ylim[2],length.out = n)
44-
z <- eval_over_grid_with_batch(x, y, fn, batchmax)
45-
46-
gcf_grid(x,y,z, pts=pts, ...)
34+
cf_func(fn0=fn0, n=n,
35+
xlim=xlim, ylim=ylim, xylim=xylim,
36+
batchmax=batchmax,
37+
out.col.name=out.col.name,
38+
out.name=out.name,
39+
pts=pts,
40+
gg=TRUE,
41+
...)
42+
# if(!is.null(out.col.name)) {
43+
# fn <- function(xx){fn0(xx)[,out.col.name]}
44+
# } else if (!is.null(out.name)) {
45+
# fn <- function(xx){fn0(xx)[[out.name]]}
46+
# } else {
47+
# fn <- fn0
48+
# }
49+
# if (!is.null(xylim)) {xlim <- ylim <- xylim}
50+
# x <- seq(xlim[1],xlim[2],length.out = n)
51+
# y <- seq(ylim[1],ylim[2],length.out = n)
52+
# z <- eval_over_grid_with_batch(x, y, fn, batchmax)
53+
#
54+
# gcf_grid(x,y,z, pts=pts, ...)
4755
}

man/cf.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cf_func.Rd

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cf_grid.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)