admin管理员组

文章数量:1279089

I'm wanting to add an annotation/geom on my panel functions with the content dependent on the row and column for example I might want to print the text: "Row: i, Column: j" in addition to normal density plots.

...something like this...

  library(data.table)
  library(ggplot2)
  library(GGally)

  dt <- as.data.table(diamonds)[sample(.N, 50),(carat, depth, x, y, z)]
  
  # custom x-y scatter w/PC vectors
  custom_plot <- function(data, mapping, i, j, ...) {
    
    ggplot(data = data, mapping = mapping) + 
      geom_point(...) +
      annotate("text", x = -1, y = 1, label = as.character(i)) + 
      annotate("text", x = 1, y = -1, label = as.character(j)) + 
      theme_minimal()
  }
  
  ggpairs(dt,
                upper = list(continuous = wrap(pcplot)),
                diag = list(continuous = "blank"),
                lower = list(continuous = wrap(custom_plot))
  )

Edit: I've looked at the source - the row and column information is unavailable to the custom plotting function which gets wrapped with just the formal arguments that were specified

I'm wanting to add an annotation/geom on my panel functions with the content dependent on the row and column for example I might want to print the text: "Row: i, Column: j" in addition to normal density plots.

...something like this...

  library(data.table)
  library(ggplot2)
  library(GGally)

  dt <- as.data.table(diamonds)[sample(.N, 50),(carat, depth, x, y, z)]
  
  # custom x-y scatter w/PC vectors
  custom_plot <- function(data, mapping, i, j, ...) {
    
    ggplot(data = data, mapping = mapping) + 
      geom_point(...) +
      annotate("text", x = -1, y = 1, label = as.character(i)) + 
      annotate("text", x = 1, y = -1, label = as.character(j)) + 
      theme_minimal()
  }
  
  ggpairs(dt,
                upper = list(continuous = wrap(pcplot)),
                diag = list(continuous = "blank"),
                lower = list(continuous = wrap(custom_plot))
  )

Edit: I've looked at the source - the row and column information is unavailable to the custom plotting function which gets wrapped with just the formal arguments that were specified

Share Improve this question edited Feb 24 at 20:43 David McCabe asked Feb 24 at 19:35 David McCabeDavid McCabe 1371 silver badge8 bronze badges 2
  • 2 Your data generating code line does not look like valid R-/{data.table}-syntax. What is pcplot? Why do we need {data.table} here? Isn't your question about ggplot()? Not too sure what your panels are. Did you read the docs of geom_label(), geom_text(), annotate()? There are plenty of answerd questions on SO. Please provide a small reproducible example which is also minimal, see stackoverflow/questions/5963269/… Otherwise answers remain guesses. – Friede Commented Feb 24 at 20:05
  • i quickly threw an example together. fot the period : .(carat, depth, x, y, z) and you can change to upper = list(continuous = "density") if you want to run the example. The issue isn't the example, its the general question regarding access to row and column ordinals in the custom function – David McCabe Commented Feb 24 at 20:52
Add a comment  | 

1 Answer 1

Reset to default 1

You could unquote the current x and y axes to figure out your row and column using match(). Then place the labels depending on your current data min(data[[x_var]]). Note: since this is a bit hacky, this works so long you don't play around with the columns parameter in ggpairs.

library(data.table)
library(ggplot2)
library(GGally)    
dt <- as.data.table(diamonds)[sample(.N, 50), .(carat, depth, x, y, z)]

custom_plot <- function(data, mapping, ...) {
  x_var <- quo_name(mapping$x)
  y_var <- quo_name(mapping$y)
 
  gg <- ggplot(data = data, mapping = mapping) + 
    geom_point(...) +
    annotate("text", 
             x = min(data[[x_var]]), 
             y = min(data[[y_var]]), 
             label = paste("Row:", match(y_var, colnames(dt)), " Column:", match(x_var, colnames(dt))), vjust = 0.4, hjust = -0.4, size = 3) +
    theme_minimal()
}

ggpairs(dt, lower = list(continuous = wrap(custom_plot)))

giving

本文标签: raccessing panel specific variables in ggpairs custom functionStack Overflow