gpt4 book ai didi

r - ggplot2 数字点大小 : Do not display point when value = 0

转载 作者:行者123 更新时间:2023-12-04 11:13:22 25 4
gpt4 key购买 nike

我有两个时间序列的数据,我想将它们绘制在一起。 x 轴将是日期,y 轴将是系列 1 的折线图,而点的大小将根据系列 2 的数值进行缩放。但是,当系列 2 = 0 时,我希望 ggplot根本不显示一个点。我已经尝试将点大小的范围从最小值 0 开始设置,但它仍然显示值为 0 的点。

这是重现问题的代码:

Dates = c("2015-05-01", "2015-05-02", "2015-05-03", "2015-05-04", "2015-05-05", "2015-05-06")
Dates = as.Date(Dates)
Series1 = c(0,2,8,5,3,1)
Series2 = c(0,0,5,0,10,5)

df = data.frame(Dates, Series1, Series2)
ggplot(data = df)+
geom_line(aes(x=Dates, y = Series1))+
geom_point(aes(x=Dates, y = Series1, size = Series2))+
scale_size_continuous(range = c(0, 5))

这会产生下图: PointSize=0 Displayed

如何让 ggplot2 在 Series2 = 0 时不创建点,但仍显示线?我还尝试用 NA 替换 Series2 的 0,但这导致绘图失败。

最佳答案

您可以将最小值更改为负值:

ggplot(data = df) +
geom_line(aes(x = Dates, y = Series1))+
geom_point(aes(x = Dates, y = Series1, size = Series2))+
scale_size_continuous(range = c(-1, 5))

如果您不希望图例包含 0,您可以添加 breaks:

scale_size_continuous(range = c(-1, 5), breaks = seq(2.5, 10, 2.5)) 

enter image description here

关于r - ggplot2 数字点大小 : Do not display point when value = 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55262551/

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