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 9229305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:32:19+00:00 2026-06-18T05:32:19+00:00

Alright, this has got me stumped. I have this function: tf <- function(formula =

  • 0

Alright, this has got me stumped. I have this function:

tf <- function(formula = NULL, data = NULL) {
res <- as.character(formula[[2]])
fac2 <- as.character(formula[[3]][3])
fac1 <- as.character(formula[[3]][2])

# Aesthetic & Data 1
p <- ggplot(aes_string(x = fac1, y = res, color = fac1), data = data) +
    facet_grid(paste(".~", fac2)) + geom_point() # OK if we only go this far

facCounts <- count(data, vars = c(fac2, fac1))
facCounts$label <- paste("n = ", facCounts$freq , sep = "")
facCounts$y <- min(data$res) - 0.1*diff(range(data$res))
facCounts <- facCounts[,-3]
names(facCounts) <- c("f2", "f1", "lab", "y") # data frame looks correct

# Aesthetic & Data 2
p <- p + geom_text(aes(x = f1, y = y, label = lab),
        color = "black", size = 4.0, data = facCounts) + facet_grid(".~f2")
p
}

Which when run with this data and call:

set.seed(1234)
mydf <- data.frame(
    resp = rnorm(40),
    cat1 = sample(LETTERS[1:3], 40, replace = TRUE),
    cat2 = sample(letters[1:2], 40, replace = TRUE))

p <- tf(formula = resp~cat1*cat2, data = mydf); print(p)

Produces this picture:

enter image description here

If you look carefully, you’ll see that the data in the two facets are actually the same. The counts are correct for that data that should be displayed (and is stored in facCounts). If the call to geom_text is commented out, then the plot is correct. A variety of changes to the geom_text call leave me with either what you see above, or the correct data is present but the count texts overlap. I can’t find the way out of this labyrinth! An attempt with + annotate("text", ...) doesn’t work either. What change is needed to keep the data faceted and the counts correct? Thanks. This is ggplot 0.9.3 btw.

  • 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-18T05:32:20+00:00Added an answer on June 18, 2026 at 5:32 am

    Now that I’ve convinced myself that this will work:

    tf <- function(formula = NULL, data = NULL) {
      res <- as.character(formula[[2]])
      fac2 <- as.character(formula[[3]][3])
      fac1 <- as.character(formula[[3]][2])
    
      # Aesthetic & Data 1
      p <- ggplot(aes_string(x = fac1, y = res, color = fac1), data = data) +
        facet_grid(paste(".~", fac2)) + geom_point() # OK if we only go this far
    
      facCounts <- count(data, vars = c(fac2, fac1))
      facCounts$label <- paste("n = ", facCounts$freq , sep = "")
      facCounts$y <- min(data$res) - 0.1*diff(range(data$res))
      facCounts <- facCounts[,-3]
      names(facCounts) <- c("cat2", "f1", "lab", "y") # data frame looks correct
    
      # Aesthetic & Data 2
      p <- p + geom_text(aes(x = f1, y = y, label = lab),
                         color = "black", size = 4.0, data = facCounts)
      p
    }
    

    You were calling facet_grid a second time using a differently named faceting variable. Removing the second call and renaming f2 to `cat2 seems to work.

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

Sidebar

Related Questions

Alright, this one (3a; sample problem with provided answer) has got me scratching my
Alright, this has had me stumped on and off for a couple of months
Alright so this problem has been breaking my brain all day today. The Problem:
Alright, I know this question has been asked before, but mine has a different
Alright, I'm not sure if this question has been asked before so if it
Alright this is my first day with JQuery so have fun with these functions
Alright I know this is more than likely a amateur question but I have
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright, this problem is a little complicated, so bear with me. I have a
Alright, I'll try to explain this the best I can. I have an iPhone

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.