在R中变化线条粗细

huangapple go评论70阅读模式
英文:

Varying line thickness in R

问题

在ggplot中,是否可以在两个点之间实现线条的变化厚度?例如,如果有两个点A和B,那么应该有一条线连接这两个点,从点A开始。唯一的条件是线条的厚度应该在开始时很薄,在结束时很粗,没有其他点位于A和B之间。

英文:

Is it possible to have a varying line thickness between two points using ggplot in R ie If there are 2 points A & B, a line should be connecting those 2 points starting from A. Only condition is the thickness of the line should vary like at the start, line should be thin and at the end, line should be thick, there is no other points are present in between A & B ?

答案1

得分: 4

以下是您要翻译的内容:

"要理解您的兴趣点,最好是看一下您已经尝试过的内容。"

"不过,以下是如何使用ggplot2在两个点之间绘制一条线,即point_apoint_b,同时线条宽度增加的方法:"

# 定义您的点。
point_a <- 1
point_b <- 10

# 一些虚拟数据。
x <- seq(point_a, point_b, length.out = 100)

# 创建数据框。
data <- data.frame(x = x, y = x)

# 绘制线条并设置不同的宽度。
ggplot(data, aes(x = x, y = y)) +
    geom_line(linewidth = x)

以上代码将生成以下图表:

在R中变化线条粗细

如果这不是您想要的,请随时评论。

英文:

It is a bit difficult to understand what exactly you are interested in without seeing what you have already tried.

However, here is how you can plot a line with ggplot2 between two points, i.e., point_a and point_b, while the line increases in width.

# Define your points.
point_a &lt;- 1
point_b &lt;- 10

# Some dummy data.
x &lt;- seq(point_a, point_b, length.out = 100)

# Create data frame.
data &lt;- data.frame(x = x, y = x)

# Plot a line with varying width.
ggplot(data, aes(x = x, y = y)) +
    geom_line(linewidth = x)

The code above will produce the following plot:

在R中变化线条粗细

Feel free to comment if this is not what you had in mind.

答案2

得分: 2

如果您不想手动插值线宽,可以使用ggforce包中的geom_link2

library(ggforce)
#> 加载需要的包:ggplot2

df <- data.frame(x = c(1, 2, 4), y = c(3, 1, 7), width = c(1, 10, 5))

ggplot(df, aes(x, y,  linewidth = width)) +
  geom_link2(lineend = "round", color = "green3") +
  theme_minimal(base_size = 16)

在R中变化线条粗细

创建于2023-05-06,使用 reprex v2.0.2 </sup>

英文:

If you don't want to manually interpolate linewidths, you can use geom_link2 from the ggforce package:

library(ggforce)
#&gt; Loading required package: ggplot2

df &lt;- data.frame(x = c(1, 2, 4), y = c(3, 1, 7), width = c(1, 10, 5))

ggplot(df, aes(x, y,  linewidth = width)) +
  geom_link2(lineend = &quot;round&quot;, color = &quot;green3&quot;) +
  theme_minimal(base_size = 16)

在R中变化线条粗细

<sup>Created on 2023-05-06 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月7日 05:10:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定