- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 R 编程语言。最近,我学习了如何使用 ggplot 库“绘制”水平线和垂直线:
library(ggplot2)
# Simple scatter plot
sp <- ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point()
# Add horizontal line at y = 2O
a = sp + geom_hline(yintercept=20)
b = sp + geom_vline(xintercept = 3)
现在,我尝试将这些陈述组合在一起 - 例如:
c = sp + geom_hline(yintercept=20) + geom_hline(yintercept=15) + geom_vline(xintercept = 3) +
geom_vline(xintercept = 5) + geom_vline(xintercept = 6) + geom_hline(yintercept=35)
问题:我正在尝试修改上面的图,使其看起来像这样:
有人可以告诉我该怎么做吗?或者您是否必须手动将此图导出到 Microsoft Paint 中并在那里进行更改?
谢谢
最佳答案
您可以使用annotate
添加图层。每个段都是下面四个向量的匹配元素。即第一段从 (-Inf,15) 开始,到 (3,15) 结束。
ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point() +
annotate("segment",
x = c(-Inf, 3, -Inf, 5, -Inf, 6),
y = c(15, 15, 20, 20, 35, 35),
xend = c(3, 3, 5, 5, 6, 6),
yend = c(15, -Inf, 20, -Inf, 35, -Inf))
或者也许定义一个函数,从一个点向轴绘制两条线(假设这里的点是正数,所以它应该朝向 -Inf)会更容易。要使函数输出多个 ggplot2 元素,请将它们放入列表中:
draw_guides<- function(x, y) {
list(geom_segment(aes(x = -Inf, xend = x, y = y, yend = y)),
geom_segment(aes(x = x, xend = x, y = y, yend = -Inf)))
}
然后您可以调用每一个来更轻松地重新创建所需的图形:
ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point() +
draw_guides(3,15) +
draw_guides(5,20) +
draw_guides(6,35)
关于R:将 "vline"和 "hline"语句组合在一起(ggplot2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68788647/
我想用该方面的人口中位数绘制一条水平方面的线。 我尝试了这种方法,但没有使用以下代码创建虚拟汇总表: require(ggplot2) dt = data.frame(gr = rep(1:2, ea
添加多条 hlines 进行绘图。 xstart 和 xfinsh 值位于 pandas 数据框中 xstart xfinish 0.00 6.30 37.45 43.95 57.16
我有一个带有两个静态 HLine 的条形图,我想为它们添加标签(或图例),以便在图上定义它们。我试过类似的东西: eq = ( sr2.hvplot( kind="bar",
我已经尝试了几个小时来完成这项工作。我尝试使用“python-gantt”包,但没有成功。我也尝试过 plotly(这很漂亮,但我不能在他们的网站上托管我的敏感数据,所以那行不通)。 我的起点是这里的
我有一个折线图,其中包含不同年份和月份的指标。我想为每个月添加一条中线。 library(dplyr) library(lubridate) library(ggplot2) set.seed(52)
假设我有一个简单的 xtable,如下所示: library(xtable) x % latex table generated in R 3.6.0 by xtable 1.8-4 package
我正在尝试以组织模式记录我的时间: ** Bob Johnson, Bob's SEO | subject | time
找了好久都没找到解决办法。我正在用 ggplot2 中的一些水平线循环绘制箱线图。我想出了将线条美学设置“颜色”映射为图例中我想要的名称,以获得线条和箱线图的图例。但是,线条索引值似乎没有更新,并且绘
我有一个看起来像这样的数据框。 > head(df) DGene JGene cdr3_len Sum 1 IGHD1 IGHJ1 0 22 2 IGHD1 IGHJ1
我正在尝试为 matplotlib.hlines 中的每一行添加标签: from matplotlib import pyplot as plt plt.hlines(y=1, xmin=1, xma
我想简单地拥有字符串 '\\ \hline' 在我的程序中。现在我使用了单引号,所以我希望 a = ' \\ \hline' 给我我想要的,反而我得到 irb(main):001:0> a =
我有一个问题。我正在尝试在我的 mplfinance 图表中画线。为此,我有以下代码: levels = [(8531.0, Timestamp('2020-02-27 00:00:00')), (8
使用 geom_tile 函数时,我无法删除矩形之间的空白区域。 df <- data.frame( x = c(seq(2,16,2),seq(17,39,2)), y = c(rep(c(
在一个简单的 latex .tex 文件,我如何为 hline 着色? 我试过了 \hline[color:red] ...和: \hline{color:red} ...但它们不起作用 同样
以 Iris 数据集为例,我可以生成一个带有 facet 的 ggplot。 代码是: library(ggplot2) data(iris) y=iris y$Petal.Width.Range=f
下面的代码是如何工作的: //@version=3 study("My Script", overlay=true) price = (ticker == "EURUSD" ? 1.1600 : 1.
我有一个 pm2_5 数据框数据,我使用 matplotlib 散点图 绘制了这些数据。我想在不同的 y 值 处插入多条水平线,我通过为 y 的每个不同值手动调用 '''ax.axhline''' 函
下面的代码是如何工作的: //@version=3 study("My Script", overlay=true) price = (ticker == "EURUSD" ? 1.1600 : 1.
我有一个 pm2_5 数据框数据,我使用 matplotlib 散点图 绘制了这些数据。我想在不同的 y 值 处插入多条水平线,我通过为 y 的每个不同值手动调用 '''ax.axhline''' 函
在图表中的 100 级添加 axline/hline 后,我正在努力调整我的情节图例。(已添加屏幕截图) 如果有一种方法可以正确运行它,那么图例中不会丢失任何信息,并且可以添加另一个 hline 并将
我是一名优秀的程序员,十分优秀!