现有问题询问有关在以下位置标记单个geom_abline()内容的问题ggplot2:
这些都没有达到我想要向散点图添加多条参考线的用例,目的是允许对斜率范围内的点进行轻松分类。这是该图的可重现示例:
library(ggplot2)
set.seed(123)
df <- data.frame(
x = runif(100, 0, 1),
y = runif(100, 0, 1))
lines <- data.frame(
intercept = rep(0, 5),
slope = c(0.1, 0.25, 0.5, 1, 2))
p <- ggplot(df, aes(x = x, y = y)) +
geom_point() +
geom_abline(aes(intercept = intercept, slope = slope),
linetype = "dashed", data = lines)
p
Run Code Online (Sandbox Code Playgroud)
由于我发现无法通过其他问题以编程方式执行此操作,因此我通过数据框“缩放”手动方法,使用反复试验来找出合理的标签位置。
labels <- data.frame(
x = c(rep(1, 3), 0.95, 0.47), …Run Code Online (Sandbox Code Playgroud) 我需要一条穿过该图原点的对角线
类似于 ggplot2 的东西geom_abline(intercept = 0 , slope = 1)
,但是对于 R 中的plotly
library(plotly)
fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
fig
Run Code Online (Sandbox Code Playgroud)