gpt4 book ai didi

r - ggplot2::geom_segment。如何设置多个尺寸,比如 c(10,12)?

转载 作者:行者123 更新时间:2023-12-04 12:35:45 26 4
gpt4 key购买 nike

我正在用 ggplot 绘制不同 Action (A,B)的数据框,这些 Action 不互斥并且有多个 Id。事件经常在一个 Id 中重叠,这可能会导致一些事件不可见。

数据

df <- data.frame(id = c("id1", "id1", "id2", "id2"), x1 = c(10, 9, 12, 12 ), x2 = c(16, 17, 15, 19), type = c("A", "B", "A", "B"))

代码

library("ggplot2")

这行得通:

ggplot2::ggplot() +
geom_segment(data = df, aes(colour = type, x = x1, xend = x2, y = id, yend = id), size = 10)

结果:B和A重叠。如何指定不同的geom_segment size

不起作用:

ggplot2::ggplot() +
geom_segment(data = df, aes(colour = type, x = x1, xend = x2, y = id, yend = id), size = c(10,12,10,12))

Error: Aesthetics must be either length 1 or the same as the data (2): size

最佳答案

看起来您的尺寸映射到事件类型。在这种情况下,您可以输入 size=type像图形的任何其他部分一样在美学映射中,然后使用 scale_size_discrete 指定不同尺寸的工作方式:

# Put all data and aesthetics that apply to the whole plot in the original
# ggplot() call
ggplot2::ggplot(data = df, aes(colour = type, x = x1, xend = x2, y = id, yend = id, size=type)) +
geom_segment() +
# A is drawn first so make it bigger
scale_size_discrete(range=c(12, 8))

结果:

Plot with sizes mapped to activity types

关于r - ggplot2::geom_segment。如何设置多个尺寸,比如 c(10,12)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43081620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com