英文:
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_a
和point_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)
以上代码将生成以下图表:
如果这不是您想要的,请随时评论。
英文:
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 <- 1
point_b <- 10
# Some dummy data.
x <- seq(point_a, point_b, length.out = 100)
# Create data frame.
data <- 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:
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)
创建于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)
#> Loading required package: 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)
<sup>Created on 2023-05-06 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论