当尺寸不同时,我一直试图在 ggplot2 或网格中绘制线段之间具有相等间距的线。但是我没有成功,所以我向你寻求帮助。
在下面的示例中,如何在线条大小不同的情况下保持线段之间的绝对间距相等?
我想避免使用自定义makeContent.myclass方法来控制自己。
library(ggplot2)
library(grid)
df <- data.frame(
x = c(1:2, 1:2),
y = c(1:2, 2:1),
size = c(1,1,10,10)
)
# In ggplot2
ggplot(df, aes(x, y, size = size, group = size)) +
geom_line(linetype = 2)
Run Code Online (Sandbox Code Playgroud)

# In grid
lines <- polylineGrob(
x = scales::rescale(df$x),
y = scales::rescale(df$y),
id = c(1,1,2,2),
gp = gpar(lty = 2, lwd = c(1, 10))
)
grid.newpage(); grid.draw(lines)
Run Code Online (Sandbox Code Playgroud)

我想要类似于以下在 illustrator 中制作的东西。请注意,红线段的长度相等。
有任何想法吗?谢谢阅读!
当您使用深色主题时,指示您在终端中位置的光标颜色在深灰色背景下为黑色,这让我非常恼火。只是很难看出您在文本中的位置,请参见下面的示例:
我试图改变这一点但没有成功。我使用的主题是Chaos主题,我认为这是每个 RStudio 发行版都附带的标准主题。在上面链接的文件中,我尝试从此更改以下内容(L191-L194):
.terminal.xterm-cursor-style-block.focus:not(.xterm-cursor-blink-on) .terminal-cursor {
background-color: #CCC;
color: #1e1e1e;
}
Run Code Online (Sandbox Code Playgroud)
对此:
.terminal.xterm-cursor-style-block.focus:not(.xterm-cursor-blink-on) .terminal-cursor {
background-color: #CCC;
color: #FCE94F;
}
Run Code Online (Sandbox Code Playgroud)
将编辑后的文件保存为chaos2.rsthemes并使用 RStudio 中的工具 > 全局选项 > 外观 > 添加... 将新文档添加为主题。重新启动 RStudio 后,我看到光标颜色没有变化,而我希望它变成黄色。
一个相关的问题讨论的是鼠标光标而不是我要编辑的文本光标。
有谁知道编辑主题的正确方法是什么,以便我可以在深灰色背景上有一个浅色终端光标?我不想改变一个完全不同的主题,我只想做一个小小的改变。
我正在尝试使用 ggplot2 构建一个自定义 stat 函数,其中我想访问一个离散变量来计算每组的统计数据。但是,ggplot 层的默认行为是自动将隐式组分配给任何离散变量(主要是)。这意味着我的数据会通过自动分组进行拆分,这是我不想要的。
我可以显示如下;我有一个非常标准的构造函数:
library(ggplot2)
stat_example <- function(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
) {
layer(data = data,
mapping = mapping,
stat = StatExample,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(na.rm = na.rm))
}
Run Code Online (Sandbox Code Playgroud)
我有一个 Stat ggproto 对象,它只是传递数据,但出于说明目的打印数据的头部。我在这里调用了我感兴趣的部分来计算实际统计数据value。
StatExample <- ggproto(
"StatExample",
Stat,
required_aes = c("x", "y", "value"),
default_aes = …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来偏移通过 xy 坐标在一个方向(在 R 中)定义的任意曲线。我可以使用 {polyclip} 包在两个方向上偏移曲线。
library(polyclip)
#> polyclip 1.10-0 built from Clipper C++ version 6.4.0
# Make a curve
t <- seq(10, 0, by = -0.05)
curve <- data.frame(
x = t * cos(t), y = t * sin(t)
)
plot(curve, type = 'l')
# Find offset
offset <- polylineoffset(curve, delta = 0.5,
jointype = "round", endtype = "openbutt")[[1]]
offset <- as.data.frame(offset) # xy coordinates
lines(offset, col = "red")
Run Code Online (Sandbox Code Playgroud)

由于曲线上的点比偏移的delta参数间隔更近,因此我可以通过找出一个点与下一个点之间的距离最大的位置来启发式地分割偏移。
distance <- c(0, sqrt(diff(offset$x)^2 + sqrt(diff(offset$y)^2))) …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种通用方法来自动将 ggplot 的字体与 html 主题使用的字体进行匹配。在下面的代码中,我想检测“yeti”主题使用“Open Sans”字体作为正文。同样,如果主题是“united”,则应该检测到字体是“Ubuntu”。
---
title: "Untitled"
output:
html_document:
theme: yeti
date: "2022-09-14"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(dev = "ragg_png") # so that fonts render effortlessly
```
```{r plot}
library(ggplot2)
font <- magic_function_to_retrieve_current_font()
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme_gray(base_family = font)
```
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我想知道一个函数,这里称为magic_function_to_retrieve_current_font(),它执行以下操作:
现在(3)和(4)我可能自己弄清楚了,但我在步骤(1)和(2)上遇到了困难。是否有一些神奇的 knitr/rmarkdown/quarto 函数可以用来在编织文档时访问 YAML/主题样式表?
我需要将密度线与 geom_histogram 的高度对齐,并在 y 轴而不是密度上保留计数值。
我有这两个版本:
# Creating dataframe
library(ggplot2)
values <- c(rep(0,2), rep(2,3), rep(3,3), rep(4,3), 5, rep(6,2), 8, 9, rep(11,2))
data_to_plot <- as.data.frame(values)
Run Code Online (Sandbox Code Playgroud)
# Option 1 ( y scale shows frequency, but geom_density line and geom_histogram are not matching )
ggplot(data_to_plot, aes(x = values)) +
geom_histogram(aes(y = ..count..), binwidth = 1, colour= "black", fill = "white") +
geom_density(aes(y=..count..), fill="blue", alpha = .2)+
scale_x_continuous(breaks = seq(0, max(data_to_plot$values), 1))
Run Code Online (Sandbox Code Playgroud)
y 刻度显示频率,但 geom_密度线和 geom_histogram 不匹配

# Option 2 (geom_density line and geom_histogram …Run Code Online (Sandbox Code Playgroud) 我对在 ggplot2 中自动绘制模型感兴趣。根据对 ggplot2 问题跟踪器的讨论,我相信像下面这样的包装器应该可以工作。
geom_predict <- function(model, fixed_terms = list(), ...) {
force(model)
force(fixed_terms)
fun <- function(x) {
terms <- c(list(x = x), fixed_terms)
predict(model, newdata = do.call(data.frame, terms))
}
geom_function(fun = fun, ...)
}
Run Code Online (Sandbox Code Playgroud)
如果模型的形式为 ,则此方法非常有效y ~ x。
library(ggplot2)
library(tibble)
set.seed(0)
df <- tibble(
x = runif(20, max = 10),
y = 5 + 2 * x + 0.5 * x^2 + rnorm(20)
)
fit <- lm(y ~ poly(x, 2), data = df)
ggplot(df, …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚具有多项式项的逻辑回归系数与预测之间的关系。具体来说,我对 x 轴上预测最高的位置感兴趣。下面的例子:
set.seed(42)
# Setup some dummy data
x <- 1:200
y <- rep(0, length(x))
y[51:150] <- rbinom(100, 1, 0.5)
# Fit a model
family <- binomial()
model <- glm(y ~ poly(x, 2), family = family)
# Illustrate model
plot(x, y)
lines(x, family$linkinv(predict(model)), col = 2)
Run Code Online (Sandbox Code Playgroud)

上面的模型给了我这些系数:
coef(model)
#> (Intercept) poly(x, 2)1 poly(x, 2)2
#> -1.990317 -3.867855 -33.299893
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v1.0.0)于 2021 年 8 月 3 日创建
手册页poly()说明如下:
正交多项式由系数概括,可用于通过 Kennedy & Gentle (1980, pp. 343–4) 中给出的三项递归对其进行评估,并用于代码的预测部分。 …
我试图通过它不断向我显示“所有格式无法解析。未找到格式”来解析我的日期时间。
这些是我一直在尝试解析的时间和代码。
datetime_clean <- c("4:10 pm Sept 18, 2021", "12:06 pm Sept 18, 2021", "9:42 am Sept 18, 2021")
datetime_parse <- parse_date_time(
datetime_clean, "%I:%M %p %m/%d/%Y"
)
Run Code Online (Sandbox Code Playgroud)