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 wrap
ped 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 wrap
ped with just the formal arguments that were specified
1 Answer
Reset to default 1You 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
版权声明:本文标题:r - accessing panel specific variables in ggpairs custom function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741245109a2364722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
R
-/{data.table}
-syntax. What ispcplot
? Why do we need{data.table}
here? Isn't your question aboutggplot()
? Not too sure what your panels are. Did you read the docs ofgeom_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.(carat, depth, x, y, z)
and you can change toupper = 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