向 ggplot 添加一行

发布于 2024-08-12 06:19:43 字数 972 浏览 2 评论 0原文

我试图在点图中添加一条线,但我无法弄清楚。我的 y 值是从 0 到 Inf 的数字,而我的 x 值来自有序因子。

这是绘图代码,仅显示点:

g = ggplot() +
  layer(data = ratesdf, mapping = aes(x = age, y = rates), geom = "point", stat="identity") +
  layer(data = ratesdf, mapping = aes(x = age, y = rates), geom = "smooth", stat = "smooth", method = loess)
print(g)

这是数据框:

          rates      age
[0,5)    0.00000000    [0,5)
[5,10)   0.00000000   [5,10)
[10,15)  0.00000000  [10,15)
[15,20)  0.02017059  [15,20)
[20,25)  0.32707402  [20,25)
[25,30)  0.54013169  [25,30)
[30,35)  0.71698958  [30,35)
[35,40)  0.81120944  [35,40)
[40,45)  0.87283637  [40,45)
[45,50)  0.91411649  [45,50)
[50,55)  0.91273334  [50,55)
[55,60)  0.95627322  [55,60)
[60,65)  0.92879819  [60,65)
[65,70)  0.98088779  [65,70)
[70,75)  0.90406674  [70,75)
[75,80)  1.00000000  [75,80)
[80,85)  1.00000000  [80,85)
[85,Inf] 1.00000000 [85,Inf]

提前感谢大家!

(哈德利,我保证一旦收到年度生日礼品卡就买你的书:))

I am trying to add a line to a plot of points, and I can't figure it out. My y-values are numbers from 0 to Inf, while my x-values are from an ordered factor.

Here is the plotting code, which only displays points:

g = ggplot() +
  layer(data = ratesdf, mapping = aes(x = age, y = rates), geom = "point", stat="identity") +
  layer(data = ratesdf, mapping = aes(x = age, y = rates), geom = "smooth", stat = "smooth", method = loess)
print(g)

Here is the dataframe:

          rates      age
[0,5)    0.00000000    [0,5)
[5,10)   0.00000000   [5,10)
[10,15)  0.00000000  [10,15)
[15,20)  0.02017059  [15,20)
[20,25)  0.32707402  [20,25)
[25,30)  0.54013169  [25,30)
[30,35)  0.71698958  [30,35)
[35,40)  0.81120944  [35,40)
[40,45)  0.87283637  [40,45)
[45,50)  0.91411649  [45,50)
[50,55)  0.91273334  [50,55)
[55,60)  0.95627322  [55,60)
[60,65)  0.92879819  [60,65)
[65,70)  0.98088779  [65,70)
[70,75)  0.90406674  [70,75)
[75,80)  1.00000000  [75,80)
[80,85)  1.00000000  [80,85)
[85,Inf] 1.00000000 [85,Inf]

Thanks to everyone in advance!

(Hadley, I promise to buy your book as soon as I get my annual birthday giftcards :) )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡紫姑娘! 2024-08-19 06:19:43

您需要手动指定 group = 1,因为默认情况下 ggplot2 按绘图上所有分类变量的组合进行分组。

You'll need to manually specify group = 1 because by default ggplot2 groups by the combination of all categorical variables on the plot.

沒落の蓅哖 2024-08-19 06:19:43

我不确定我是否错过了您想要做的事情,但基本上想要一个阶跃函数。例如:

rates = c(0.00000000 ,0.00000000 ,0.00000000 ,0.02017059 ,0.32707402, 0.54013169 ,0.71698958 ,0.81120944 ,0.87283637 ,0.91411649 ,0.91273334 ,0.95627322 ,0.92879819 ,0.98088779 ,0.90406674 ,1.00000000 ,1.00000000, 1.00000000 )
age = seq(0, 85, 5)

#ReJig the variables
r2 = sort(rep(rates,2));r2 = r2[1:(length(r2)-1)]
a = sort(rep(age,2));a = a[2:(length(a))]

library(ggplot2)
ggplot() + geom_line(aes(x=a, y=r2))

HTH

I'm not sure if I've missed what you are trying to do, but are basically wanting a step function. For example:

rates = c(0.00000000 ,0.00000000 ,0.00000000 ,0.02017059 ,0.32707402, 0.54013169 ,0.71698958 ,0.81120944 ,0.87283637 ,0.91411649 ,0.91273334 ,0.95627322 ,0.92879819 ,0.98088779 ,0.90406674 ,1.00000000 ,1.00000000, 1.00000000 )
age = seq(0, 85, 5)

#ReJig the variables
r2 = sort(rep(rates,2));r2 = r2[1:(length(r2)-1)]
a = sort(rep(age,2));a = a[2:(length(a))]

library(ggplot2)
ggplot() + geom_line(aes(x=a, y=r2))

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文