我已经开始扩展ggplot2
了,我仍然感觉到包是如何调用其所有内部功能的。
我有一个新的ggproto类,它扩展了当前的Geom
环境之一。这个类的当前模型将沿着离散的x轴绘制一些东西,理想的情况是接触x轴的滴答声。当y轴已经在离散尺度上时,这个模型工作得很好,因为默认的展开值只添加.6填充。然而,在连续的y尺度上,默认的填充可以使这些新绘制的对象看起来很远。总之,如何使我的Geom
类覆盖默认的扩展而不只是将scale_y_continuous(expand = c(0,0,.05,0)
或scale_y_discrete(expand = c(0, 0, .6,0)
添加到我的layer函数中.
请考虑以下可重复的示例
library(dplyr)
library(tidyr)
library(ggplot2)
library(stringr)
mtcars0 <- as_tibble(mtcars, rownames = "CarNames") %>%
gather(key = "qualities", value = "value", -CarNames) %>%
group_by(qualities) %>%
mutate(scaledValue = scale(value)) %>%
ungroup() %>%
mutate(carCase = case_when(str_detect(CarNames, "^[A-M]") ~ "A-M",
TRUE ~ "N-Z"))
"%||%" <- function(a, b) {
if (!is.null(a)) a else b
}
MyText <- ggproto("GeomMyText",
GeomText,
extra_params = c("na.rm","padDist"),
setup_data = function(data, params){
#find bottom of plot with sufficent space
minpadding <- params$padDist %||% diff(range(data$y))*.05
data$y <- min(data$y) - minpadding
data
})
geom_mytext <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
..., parse = FALSE, nudge_x = 0, nudge_y = 0, check_overlap = FALSE,
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, padDist = NULL)
{
if (!missing(nudge_x) || !missing(nudge_y)) {
if (!missing(position)) {
abort("You must specify either `position` or `nudge_x`/`nudge_y`.")
}
position <- position_nudge(nudge_x, nudge_y)
}
layer(data = data, mapping = mapping, stat = stat, geom = MyText,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(parse = parse, check_overlap = check_overlap,
na.rm = na.rm, padDist = padDist, ...))
}
result <- ggplot(mtcars0, aes(x = CarNames, value)) +
geom_point() +
geom_mytext(aes(label = carCase)) +
theme(axis.text.x = element_text(angle=90))
#Default
result
#Desired Result without having to call scale_y_continuous
result + scale_y_continuous(expand = c(0,0,0.05,0))
我假设我需要扩展ScaleContinuous
环境,但是我不知道如何将MyText
环境与它连接起来。
有什么建议吗?
编辑--编辑--
谢谢你的快速回复!几件事-
ggplot2
已经知道这一点时,我想可能有一个我缺少的技巧。就目前而言,我将继续发展,并提出建议。谢谢!编辑2
我又看了一眼solution given here。我需要修改的确切参数是
panel_params$y$continuous_range[1] <- panel_params$y$limits[1]
我需要在draw_panel
的某个地方做这个。似乎相关的尺度包含在那里,coord$transform(data, panel_params)
负责将填充包含在重新标度的轴上,这取决于为panel_params$y$limits
和panel_params$y$continuous_range
设置了什么。
再次感谢大家的贡献!
发布于 2020-06-25 18:18:22
很好的问题-谢谢你发帖。
这比你想象的要容易。您只需将所需的scale对象与从geom_mytext
函数返回的layer对象捆绑在一起,方法是将它们与c
连接起来。在本例中,我还捆绑了一个coord_cartesian
对象,这样我就可以关闭裁剪以正确显示文本。我还将默认的check.overlap
更改为TRUE
,因为您的标签被过度绘制。
注意:我根本没有更改您的ggplot
调用
geom_mytext <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
..., parse = FALSE, nudge_x = 0, nudge_y = 0, check_overlap = FALSE,
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, padDist = NULL)
{
if (!missing(nudge_x) || !missing(nudge_y)) {
if (!missing(position)) {
abort("You must specify either `position` or `nudge_x`/`nudge_y`.")
}
position <- position_nudge(nudge_x, nudge_y)
}
c(layer(data = data, mapping = mapping, stat = stat, geom = MyText,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(parse = parse, check_overlap = check_overlap,
na.rm = na.rm, padDist = padDist, ...)),
scale_y_continuous(expand = c(0,0,0.05,0)),
coord_cartesian(clip = "off"))
}
result <- ggplot(mtcars0, aes(x = CarNames, value)) +
geom_point() +
geom_mytext(label = "test") +
theme(axis.text.x = element_text(angle=90))
result
现在来点警告吧。因为您是在提供您自己的scale_y_continuous
对象,所以当用户试图添加自己的y缩放时,用户不会喜欢这个ggplot抱怨。您还需要一些逻辑来选择添加一个连续的或离散的y尺度。不过,我不认为这些是无法克服的问题。
发布于 2020-06-25 18:22:20
我一般不会推荐你正在采取的路线,以实现在底部的文本。原因是在图形范式的语法中,所有不同的图形元素(主题、和弦、统计、面、几何)都应该彼此独立地工作。当我将geom添加到一个地形图中时,我希望数据会影响到缩放,但不会影响到缩放。
这就是说,这里是用geom自动设置y展开的最简单的方法,也就是简单地从构造函数返回geom和scale的列表。这类似于geom_sf()
自动设置coord_sf()
的方式。我知道在你的问题中你提到这不是你喜欢的,但是除了数据之外,没有地理层与天平沟通的自然基础设施。
geom_mytext <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
..., parse = FALSE, nudge_x = 0, nudge_y = 0, check_overlap = FALSE,
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, padDist = NULL)
{
if (!missing(nudge_x) || !missing(nudge_y)) {
if (!missing(position)) {
abort("You must specify either `position` or `nudge_x`/`nudge_y`.")
}
position <- position_nudge(nudge_x, nudge_y)
}
layer <- layer(data = data, mapping = mapping, stat = stat, geom = MyText,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(parse = parse, check_overlap = check_overlap,
na.rm = na.rm, padDist = padDist, ...))
list(layer, scale_y_continuous(expand = c(0,0,0.05,0)))
}
我建议的是简单地设置data$y <- -Inf
,这将被规模培训忽略,保持默认的扩展因子不变,但无论如何将数据放在x轴上。
MyText <- ggproto("GeomMyText",
GeomText,
extra_params = c("na.rm","padDist"),
setup_data = function(data, params){
data$y <- -Inf
data
})
这给了我这个情节:
作为比较,这就是你对我的重装图谋:
顺便说一句,似乎有许多重复的标签,你可能想在你的最后的地理位置。
https://stackoverflow.com/questions/62580425
复制相似问题