Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7739107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:26:19+00:00 2026-06-01T08:26:19+00:00

In Hadley Wickham’s ggplot2 book in chapter 10.3, he alludes to making plot functions.

  • 0

In Hadley Wickham’s ggplot2 book in chapter 10.3, he alludes to making plot functions. I want to make many similar plots that use faceting, but I cannot refer to a column. If all my references are in aesthetics then I can use aes_string and everything works. Facet_wrap seems not to have an analogue.

library(ggplot2)
data(iris)

This is the plot I want to functionalize.

pl.flower1 <- ggplot(data=iris, 
                    aes_string(x='Sepal.Length', y='Sepal.Width', color='Petal.Length')) +
                                 geom_point() +facet_wrap(~Species)

This works if I do not facet.

flowerPlot <- function(dat, sl, sw, pl, sp){
  ggplot(data=dat, aes_string(x=sl, y=sw, color=pl)) + geom_point()
}
pl.flower2 <- flowerPlot(iris, sl='Sepal.Length', sw='Sepal.Width', pl='Petal.Length')

What should “sp” be two lines below? A formula, a string? Maybe the whole aproach is wrong.

flowerPlotWrap <- function(dat, sl, sw, pl, sp){
      ggplot(data=dat, aes_string(x=sl, y=sw, color=pl)) + geom_point() +facet_wrap(sp)
    }
    pl.flower3 <- flowerPlotWrap(iris, sl='Sepal.Length', sw='Sepal.Width', pl='Petal.Length', sp= ?????)

In addition to an answer I would love pointer on how anyone approaches this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:26:20+00:00Added an answer on June 1, 2026 at 8:26 am

    facet_wrap expects a formula as its first argument, so I’d just coerce it with as.formula, and feed in my sp as a string:

    flowerPlotWrap <- function(dat, sl, sw, pl, sp){
          ggplot(data=dat, aes_string(x=sl, y=sw, color=pl)) + 
          geom_point() +facet_wrap(as.formula(sp)) # note the as.formula
    }
    pl.flower3 <- flowerPlotWrap(iris, sl='Sepal.Length', 
                                 sw='Sepal.Width', pl='Petal.Length', 
                                 sp= '~Species')
    

    Alternatively if my formula was always going to look like ~[columnname], I could just build that in to flowerPlotWrap and pass in the column name:

    flowerPlotWrap <- function(dat, sl, sw, pl, sp){
          ggplot(data=dat, aes_string(x=sl, y=sw, color=pl)) + 
          geom_point() +facet_wrap(as.formula(sprintf('~%s',sp)))
    }
    pl.flower3 <- flowerPlotWrap(iris, sl='Sepal.Length', 
                                 sw='Sepal.Width', pl='Petal.Length', 
                                 sp= 'Species')
    

    (kudos to the reproducible example in your question! If everyone asked questions as well as that they’d get answers much quicker).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using geom_smooth() from ggplot2 . In Hadley Wickham's book (ggplot2 - Elegant Graphics
Using Hadley's great ggplot2 and his book (pp. 78-79), I am able to produce
I am trying to replicate Hadley Wickham's example on page 86 of the ggplot2
Many data analysts that I respect use version control. For example: http://github.com/hadley/ See comments
EDIT: Hadley Wickham points out that I misspoke. R CMD check is throwing NOTES,
After studying Hadley's book and searching here on SO I have created a heatmap
This seems to be a similar example to some of Hadley's examples in his
Looking through Hadley Wickham's S4 wiki: https://github.com/hadley/devtools/wiki/S4 setClass(Person, representation(name = character, age = numeric),
Thanks to Hadley's plyr package ddply function we can take a dataframe, break it
This is a follow up question as hadley pointed out unless I fix the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.