在R中绘制分组数据(同一列)

发布于 2025-02-08 06:18:20 字数 536 浏览 1 评论 0 原文

我有3列中的Formula-1驱动程序的数据,并希望为每个驾驶员制作累积点的时间序列图。

问题是:我所有的驱动程序都在第一列中,第二个点在第二列中,第三列中的累积总和。

testdf <- c("Driver A", "Driver A", "Driver A", "Driver B", "Driver B", "Driver B")

values <- c(1,5,7,3,5,8)

driversmatrix <- cbind(testdf, values); driversmatrix

示例数据图

.jpg“ rel =“ nofollow noreferrer”>链接到数据框架视图的图片

我如何从每个驱动程序累积点相互绘制的时间序列中进行时间序列?

I have data for formula-1 drivers in 3 columns and want to make a time series plot of the cumulative points for every driver.

Problem is: All my drivers are in the first column, the points in the second and the cumulative sum in the third column.

testdf <- c("Driver A", "Driver A", "Driver A", "Driver B", "Driver B", "Driver B")

values <- c(1,5,7,3,5,8)

driversmatrix <- cbind(testdf, values); driversmatrix

example data picture here

Link to picture of View of dataframe

How could I make a time series out of this where every drivers cumulative points are plotted against each other?

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

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

发布评论

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

评论(2

掩耳倾听 2025-02-15 06:18:20
library(data.table)

# set as data table if yours isn't one already
setDT(df)

# dummy data
df <- data.table(driver = c("Driver A", "Driver A", "Driver A", "Driver B", "Driver B", "Driver B")
                 , points = c(1,5,7,3,5,8)
                 ); df

# calculate cumulative sum and date (assumes data sorted in ascending date already)
df[, `:=` (cum_sum = cumsum(points)
           , date = 1:.N
           )
   , driver
   ]

# plot
ggplot(data=df, aes(x=date, y=cum_sum, group=driver)) +
  geom_line(aes(linetype=driver)) +
  geom_point()

请注意,如果我们有很多驱动程序(杂乱的地块),则按照我们目前的操作绘制一条线可能不是最佳的

library(data.table)

# set as data table if yours isn't one already
setDT(df)

# dummy data
df <- data.table(driver = c("Driver A", "Driver A", "Driver A", "Driver B", "Driver B", "Driver B")
                 , points = c(1,5,7,3,5,8)
                 ); df

# calculate cumulative sum and date (assumes data sorted in ascending date already)
df[, `:=` (cum_sum = cumsum(points)
           , date = 1:.N
           )
   , driver
   ]

# plot
ggplot(data=df, aes(x=date, y=cum_sum, group=driver)) +
  geom_line(aes(linetype=driver)) +
  geom_point()

Notice, plotting one line per driver as we are currently doing may not be optimum if we have many drivers (cluttered plot)

不再见 2025-02-15 06:18:20

首先,您需要有一个指示赛数或日期的列,假设您的数据具有每个驱动程序的种族数量相同:

library(tidyverse)
testdf <- data.frame(Driver= c("Driver A", "Driver A", "Driver A", "Driver B", "Driver B", "Driver B") , Points=c(1,5,7,3,5,8))

testdf <- testdf %>% group_by(Driver) %>% mutate(Cum_Points=cumsum(Points), Race_No=row_number())

然后用驱动程序绘制与赛车数的累积点

ggplot(testdf, aes(Race_No, Cum_Points, colour=Driver))+geom_line()

First you would need to have a column that that indicates a race number or date, assuming that your data has the same number of races per driver:

library(tidyverse)
testdf <- data.frame(Driver= c("Driver A", "Driver A", "Driver A", "Driver B", "Driver B", "Driver B") , Points=c(1,5,7,3,5,8))

testdf <- testdf %>% group_by(Driver) %>% mutate(Cum_Points=cumsum(Points), Race_No=row_number())

Then plot cumulative points against the race number with driver as the colour variable

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