Skip to content
This repository

set_geom_params: set a geom's parameters for an existing ggplot2 object #750

Closed
wants to merge 1 commit into from

2 participants

Erik Shilts Hadley Wickham
Erik Shilts

New function to allow setting of geom parameters for existing ggplot2 objects.

Hadley Wickham
Owner

Could you please rebase/merge against master, re-document with the development version of roxygen2 (install_github("klutometis/roxygen) and resubmit?

Hadley Wickham hadley closed this February 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jan 03, 2013
set_geom_params: set a geom's parameters for an existing ggplot2 object 5ff24d8
This page is out of date. Refresh to see the latest.
1  DESCRIPTION
@@ -205,3 +205,4 @@ Collate:
205 205
     'translate-qplot-lattice.r'
206 206
     'annotation-logticks.r'
207 207
     'utilities-help.r'
  208
+    'set-geom-params.r'
1  NAMESPACE
@@ -356,6 +356,7 @@ export(scale_y_discrete)
356 356
 export(scale_y_log10)
357 357
 export(scale_y_reverse)
358 358
 export(scale_y_sqrt)
  359
+export(set_geom_params)
359 360
 export(should_stop)
360 361
 export(stat_abline)
361 362
 export(stat_bin)
3  NEWS
@@ -9,6 +9,9 @@ BUG FIXES
9 9
 ggplot2 0.9.3
10 10
 ----------------------------------------------------------------
11 11
 
  12
+* `set_geom_params` changes geom parameters for an existing ggplot2
  13
+  object.
  14
+
12 15
 * The `plotmatrix` function has been deprecated and prints a warning
13 16
   message.
14 17
 
28  R/set-geom-params.r
... ...
@@ -0,0 +1,28 @@
  1
+#' Set a Geom's Parameters of an Existing ggplot2 Object
  2
+#'
  3
+#' Changes or sets a geom's parameters after a ggplot2 object has been created. This
  4
+#' function applies to all layers with a given geom type.
  5
+#'
  6
+#' @param plot_object the ggplot2 object to work on
  7
+#' @param geom name of the geom to set the parameter for (e.g. "ribbon", "point")
  8
+#' @param param_name name of parameter to set
  9
+#' @param param_value desired value of the parameter
  10
+#' @examples
  11
+#' p <- ggplot(data.frame(a=1:10, b=1:10), aes(x=a, y=b)) + geom_point() + geom_line()
  12
+#' p_set <- set_geom_params(p, "point", 'size', 10)
  13
+#' stopifnot(p_set$layers[[1]]$geom_params$size == 10)
  14
+#' @export
  15
+set_geom_params <- function(plot_object, geom, param_name, param_value) {
  16
+  
  17
+  plot_object$layers <- lapply(
  18
+    plot_object$layers, 
  19
+    function(x) {
  20
+      if(x$geom$objname == geom) 
  21
+        x$geom_params[param_name] <- param_value
  22
+
  23
+      return(x)
  24
+    }
  25
+  )
  26
+
  27
+  plot_object
  28
+}
28  man/set_geom_params.Rd
... ...
@@ -0,0 +1,28 @@
  1
+\name{set_geom_params}
  2
+\alias{set_geom_params}
  3
+\title{Set a Geom's Parameters of an Existing ggplot2 Object}
  4
+\usage{
  5
+  set_geom_params(plot_object, geom, param_name,
  6
+    param_value)
  7
+}
  8
+\arguments{
  9
+  \item{plot_object}{the ggplot2 object to work on}
  10
+
  11
+  \item{geom}{name of the geom to set the parameter for
  12
+  (e.g. "ribbon", "point")}
  13
+
  14
+  \item{param_name}{name of parameter to set}
  15
+
  16
+  \item{param_value}{desired value of the parameter}
  17
+}
  18
+\description{
  19
+  Changes or sets a geom's parameters after a ggplot2
  20
+  object has been created. This function applies to all
  21
+  layers with a given geom type.
  22
+}
  23
+\examples{
  24
+p <- ggplot(data.frame(a=1:10, b=1:10), aes(x=a, y=b)) + geom_point() + geom_line()
  25
+p_set <- set_geom_params(p, "point", 'size', 10)
  26
+stopifnot(p_set$layers[[1]]$geom_params$size == 10)
  27
+}
  28
+
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.