- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找一种在不使用 coord_cartesian(clip = "off")
的情况下向 ggplots 添加小刻度线的方法.或者一种可重现的方式将裁剪应用到 x 轴而不是 y 轴,反之亦然。
到目前为止,我一直在使用 annotation_ticks()
函数和 GeomTicks
geom 在这个优秀的答案中定义 here (稍作修改以使其与 ggplot2 v3.3.0
一起使用)。不幸的是,要使刻度线注释出现在绘图的外部,必须使用 coord_cartesian(clip = "off")
这意味着位于绘图区域之外的任何其他东西也会被暴露(参见下面的 reprex)。
或者,也许有一种方法可以利用 ggplot2 v3.3.0
的任何新功能。绘制小刻度不是作为注释而是作为轴/绘图的实际部分,以便可以将它们绘制在绘图区域之外。
我不是软件开发人员,但也许可以使用 register_theme_elements
定义一个新的主题元素。叫 axis.minor.ticks
行为类似于 axis.ticks
但从 panel_params$y$break_positions_minor
获取次要刻度的适当位置而不是 panel_params$y$break_positions
.或者以某种方式使用新的 guide_x()
S3 功能。
任何帮助将非常感激!
注解函数和ggproto对象annotation_ticks()
功能(包含 this fix 用于刻面问题):
annotation_ticks <- function(sides = "b",
scale = "identity",
scaled = TRUE,
ticklength = unit(0.1, "cm"),
colour = "black",
size = 0.5,
linetype = 1,
alpha = 1,
color = NULL,
ticks_per_base = NULL,
data = data.frame(x = NA),
...) {
if (!is.null(color)) {
colour <- color
}
# check for invalid side
if (grepl("[^btlr]", sides)) {
stop(gsub("[btlr]", "", sides), " is not a valid side: b,t,l,r are valid")
}
# split sides to character vector
sides <- strsplit(sides, "")[[1]]
if (length(sides) != length(scale)) {
if (length(scale) == 1) {
scale <- rep(scale, length(sides))
} else {
stop("Number of scales does not match the number of sides")
}
}
base <- sapply(scale, function(x) switch(x, "identity" = 10, "log10" = 10, "log" = exp(1)), USE.NAMES = FALSE)
if (missing(ticks_per_base)) {
ticks_per_base <- base - 1
} else {
if ((length(sides) != length(ticks_per_base))) {
if (length(ticks_per_base) == 1) {
ticks_per_base <- rep(ticks_per_base, length(sides))
} else {
stop("Number of ticks_per_base does not match the number of sides")
}
}
}
delog <- scale %in% "identity"
layer(
data = data,
mapping = NULL,
stat = StatIdentity,
geom = GeomTicks,
position = PositionIdentity,
show.legend = FALSE,
inherit.aes = FALSE,
params = list(
base = base,
sides = sides,
scaled = scaled,
ticklength = ticklength,
colour = colour,
size = size,
linetype = linetype,
alpha = alpha,
ticks_per_base = ticks_per_base,
delog = delog,
...
)
)
}
v3.3.0
一起使用):
GeomTicks <- ggproto(
"GeomTicks", Geom,
extra_params = "",
handle_na = function(data, params) {
data
},
draw_panel = function(data,
panel_scales,
coord,
base = c(10, 10),
sides = c("b", "l"),
scaled = TRUE,
ticklength = unit(0.1, "cm"),
ticks_per_base = base - 1,
delog = c(x = TRUE, y = TRUE)) {
ticks <- list()
for (s in 1:length(sides)) {
if (grepl("[b|t]", sides[s])) {
# for ggplot2 < 3.3.0 use: xticks <- panel_params$x.minor
if (utils::packageVersion("ggplot2") >= "3.2.1.9000") {
x_minor_breaks <- panel_scales$x$break_positions_minor()
x_major_breaks <- panel_scales$x$break_positions()
} else {
x_minor_breaks <- panel_scales$x.minor
x_major_breaks <- panel_scales$x.major
}
xticks <- setdiff(x_minor_breaks, x_major_breaks)
# Make the grobs
if (grepl("b", sides[s])) {
ticks$x_b <- with(
data,
segmentsGrob(
x0 = unit(xticks, "npc"),
x1 = unit(xticks, "npc"),
y0 = unit(0, "npc"),
y1 = ticklength,
gp = gpar(
col = alpha(colour, alpha),
lty = linetype,
lwd = size * .pt
)
)
)
}
if (grepl("t", sides[s])) {
ticks$x_t <- with(
data,
segmentsGrob(
x0 = unit(xticks, "npc"),
x1 = unit(xticks, "npc"),
y0 = unit(1, "npc"),
y1 = unit(1, "npc") - ticklength,
gp = gpar(
col = alpha(colour, alpha),
lty = linetype,
lwd = size * .pt
)
)
)
}
}
if (grepl("[l|r]", sides[s])) {
# for ggplot2 < 3.3.0 use: yticks <- panel_params$y.minor
if (utils::packageVersion("ggplot2") >= "3.2.1.9000") {
y_minor_breaks <- panel_scales$y$break_positions_minor()
y_major_breaks <- panel_scales$y$break_positions()
} else {
y_minor_breaks <- panel_scales$y.minor
y_major_breaks <- panel_scales$y.major
}
yticks <- setdiff(y_minor_breaks, y_major_breaks)
# Make the grobs
if (grepl("l", sides[s])) {
ticks$y_l <- with(
data,
segmentsGrob(
y0 = unit(yticks, "npc"),
y1 = unit(yticks, "npc"),
x0 = unit(0, "npc"),
x1 = ticklength,
gp = gpar(
col = alpha(colour, alpha),
lty = linetype, lwd = size * .pt
)
)
)
}
if (grepl("r", sides[s])) {
ticks$y_r <- with(
data,
segmentsGrob(
y0 = unit(yticks, "npc"),
y1 = unit(yticks, "npc"),
x0 = unit(1, "npc"),
x1 = unit(1, "npc") - ticklength,
gp = gpar(
col = alpha(colour, alpha),
lty = linetype,
lwd = size * .pt
)
)
)
}
}
}
gTree(children = do.call("gList", ticks))
},
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = 1)
)
coord_cartesian(clip = "on")
library(ggplot2)
library(grid)
ggplot(mpg, aes(x = class, y = displ, fill = class)) +
stat_summary(fun = mean, geom = "col", colour = "black", size = 1) +
theme_classic(base_size = 8) +
scale_y_continuous(limits = c(0, 8), expand = c(0, 0)) +
annotation_ticks(sides = "l", ticklength = -1 * unit(0.2, "cm")) +
coord_cartesian(clip = "on")
ggsave("clip_on.png", device = "png", width = 4, height = 3)
coord_cartesian(clip = "off")
ggplot(mpg, aes(x = class, y = displ, fill = class)) +
stat_summary(fun = mean, geom = "col", colour = "black", size = 1) +
theme_classic(base_size = 8) +
scale_y_continuous(limits = c(0, 8), expand = c(0, 0)) +
annotation_ticks(sides = "l", ticklength = -1 * unit(0.2, "cm")) +
coord_cartesian(clip = "off")
ggsave("clip_off.png", device = "png", width = 4, height = 3)
最佳答案
这段代码对我来说似乎很熟悉,所以我想权衡一下。
是的,使用 ggplot v3.3.0 指南已经变得可扩展,尽管我怀疑它们会在很长一段时间内保持当前的形式,因为通过小道消息我听说他们也想将指南切换到 ggproto 系统。
没有太多花里胡哨的方法来做你所要求的最便宜的方法是调整指南的指南培训部分。由于这是一个 S3 方法,我们需要一个新的指南类来编写自定义方法:
library(ggplot2)
library(rlang)
#> Warning: package 'rlang' was built under R version 3.6.3
library(glue)
guide_axis_minor <- function(
title = waiver(), check.overlap = FALSE, angle = NULL,
n.dodge = 1, order = 0, position = waiver()
) {
structure(list(title = title, check.overlap = check.overlap,
angle = angle, n.dodge = n.dodge, order = order, position = position,
available_aes = c("x", "y"), name = "axis"),
class = c("guide", "axis_minor", "axis"))
}
guide_axis()
相同,除了一个额外的类。类的顺序在这里很重要,因为我们正在继承
axis
类,这样我们就可以偷懒,只使用所有已经存在的方法。
guide_train.axis
相同内部功能。简而言之,我们将小中断视为带有空标签的主要中断。
guide_train.axis_minor <- function(guide, scale, aesthetic = NULL) {
aesthetic <- aesthetic %||% scale$aesthetics[1]
# Seperately define major and minor breaks
major_breaks <- scale$get_breaks()
minor_breaks <- scale$get_breaks_minor()
# We set the actual breaks to be both major and minor
breaks <- union(major_breaks, minor_breaks)
# We keep track of what breaks were the major breaks
is_major <- breaks %in% major_breaks
empty_ticks <- ggplot2:::new_data_frame(
list(aesthetic = numeric(), .value = numeric(0), .label = character())
)
if (length(intersect(scale$aesthetics, guide$available_aes)) == 0) {
warn(glue("axis guide needs appropriate scales: ",
glue_collapse(guide$available_aes, ", ", last = " or ")))
guide$key <- empty_ticks
} else if (length(breaks) == 0) {
guide$key <- empty_ticks
} else {
mapped_breaks <- if (scale$is_discrete()) {
scale$map(breaks)
} else {
breaks
}
ticks <- ggplot2:::new_data_frame(setNames(list(mapped_breaks),
aesthetic))
ticks$.value <- breaks
ticks$.label <- scale$get_labels(breaks)
# Now this is the bit where we set minor breaks to have empty labls
ticks$.label[!is_major] <- ""
guide$key <- ticks[is.finite(ticks[[aesthetic]]), ]
}
guide$name <- paste0(guide$name, "_", aesthetic)
guide$hash <- digest::digest(list(guide$title, guide$key$.value,
guide$key$.label, guide$name))
guide
}
axis
类,为该类编写的所有函数也适用于我们的
axis_minor
类,所以我们完成了。现在,您可以按名称从任何连续位置比例调用指南:
ggplot(mpg, aes(x = class, y = displ, fill = class)) +
stat_summary(fun = mean, geom = "col") +
scale_y_continuous(limits = c(0, 8),
guide = "axis_minor")
关于r - ggplot2 在绘图区域外添加小刻度线而不关闭剪辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61087231/
如何使用 plot() 在 XY 图中绘制第二条线,以不同的比例(如本例(紫色线))? 第一行(红色)我的 R 代码是这样的: p <- sqlQuery(ch,"SELECT wl,param1 F
我正在寻找一种将折线图步骤设置为仅完整数字步骤的方法,例如 1,2,3,4,5..... 目前它看起来像这样: 这些是我的选择。我搜索了很多但找不到解决方案。 let options = {
我的问题是如何将 Y 轴刻度的间隔从 0.2 更改为 0.1?我已经尝试了一切,但我很困!帖子底部堆叠条形图的图片 dpi=600 #pixels per square inch A <- c(
有没有办法根据当前数据集在 D3 轴上动态设置 ticks。我遇到的问题是我的时间范围(x 轴)可能变化很大。对于一些数据集来说,它是几年的值(value),对于其他数据集来说是几个月,在某些情况下只
我已经使用默认参数创建了一个 dojo 蜘蛛图。我需要隐藏轴标签(刻度上的数据值,而不是轴标题)。 我已经尝试过'ticks: false',但是它不适用于蜘蛛图。 require(["dojo/_b
有没有一种简单的方法可以在绘图 Axis 上获得自定义缩放比例? 例如,semilogy 函数提供 {x, log10(y)} 缩放比例,这样就可以自动放大/缩小并自动调整刻度和标签。我想对 {x,
如何修改图形和图表的 y Axis 刻度?我想要这样的东西:my_figure.y_range.end = my_figure.y_range.end * 1.3 所以我想要更高一点的y轴。谢谢! 最
我正在尝试使用 recharts 添加图表一些货币的最新汇率。数据显示正确,但图表始终从 0 开始并略高于最大值。 图表是正确的,但它不需要从 0 开始,因为这样做,它几乎是一条线。 这是图表的图片:
我有一个 SWT Scale 小部件,我需要在刻度上的刻度上添加标签。 有没有办法做到这一点?也许在 slider 小部件上? 谢谢 最佳答案 这可能不是您所要求的,但您可以在 Scale 小部件旁边
这是一个从 .xlsx 文件中读取数据并利用 Plotly 库绘制气泡图的程序。这是 .xlsx 文件的原始数据: 1991 1992 1993 1994 1995
我这里有一个代码笔 - https://codepen.io/anon/pen/yvgJKB 我有一个简单的堆积条形图。 我想将其制作成一个组件,因此我需要传入值以使其可重用 x 缩放函数返回d.da
让我们看一个基本的演示折线图: jsfiddle $(function () { $('#container').highcharts({ //all the code there 我想强制
是否可以在 t-sql 中获得像 DateTime.Ticks 这样的 C# 内容? 感谢帮助 最佳答案 您不太可能从 SQL 中获得与 DateTime.Ticks 相同的精度,因为 SQL 不能以
我正在使用 Bokeh 生成散点图,每个 X 值具有不同的 Y 值。当 Bokeh 生成绘图时,它会根据绘制的值的数量自动填充 x 轴间距。我希望 x 轴上的所有值均匀分布,无论单个数据点的数量如何。
我使用下面的代码使用谷歌图表生成折线图 google.charts.load('current', {packages: ['corechart', 'line']});
我正在尝试在 d3 中构建堆积条形图。 这是我的 jsfiddle: http://jsfiddle.net/maneesha/gwjkgruk/4/ 我正在尝试修复它,使 y 轴从零开始。 我认为这
我有一个 d3 时间刻度图表。目前,轴刻度为每个数据对象呈现一个日期。例如,数据的范围可以是 1 天的数据、1 个月内 2 周的数据、5 个月的数据甚至更多。 理想情况下,我们希望显示带有周数的刻度,
当使用“Cassic”折线图时,我可以为 hAxis 分配刻度,还有这样的格式和标题: var options = { hAxis: { format:'h a', title
我的累积折线图没有正确构建 yScale。 给定要构建图表的数据: var data = [{ key: "Page view", values: [ ["2013-07-01 00:
在 y 轴上使用两个具有正常整数值的 DateAxisSeries(x 轴)我的 jqPlot 将 y 刻度呈现两次。 像这样: 2| 2| 1| 1| 0|_____________________
我是一名优秀的程序员,十分优秀!