My dataset

我有一个数据集Speedest.csv,如下所示:


Date,Time,Download,Upload
2023/10/01,00:00:00,34957192.9969772,20046840.2637393
2023/10/01,00:20:00,35826556.3982541,36143231.5378943
2023/10/01,00:40:00,27695436.936076,4957720.87281617
...
...
2023/10/02,01:00:00,22575345.5295727,10335897.5917135
2023/10/02,01:20:00,15805169.0654657,6179704.32589804
2023/10/02,01:40:00,31638270.9069979,15951432.6154521
...
...
2023/10/03,05:00:00,31366450.4288069,4476811.81028971
2023/10/03,05:20:00,10709016.6772629,8848402.0645949
2023/10/03,05:40:00,32722858.0348491,2045099.79491319
...
...

My current code

下面给出的是我的当前代码,它能够使用ggploy中的面处理和输出数据.在本练习中,我try 使用管道在单个代码块中生成ggploy.

library(tidyverse)

df<-read.csv("speedtest.csv", header = TRUE)

df%>%
  select(Date,Time, Upload, Download)%>%
  pivot_longer(cols = c("Upload", "Download"),
               names_to = "Parameter", values_to = "Value")%>%
  mutate(Date = as.POSIXct(Date, format = "%Y/%m/%d"),
         Time = as.POSIXct(Time, format = "%H:%M:%S"))%>%
  ggplot(aes(x=Time, y=(Value/8000000), colour=Parameter))+
  geom_line()+
  geom_point()+
  facet_wrap(~Date, ncol = 3, nrow = 2)+
  scale_colour_manual(values = c("blue","orange"))+
  scale_x_datetime(date_breaks = "4 hours",
                   date_labels = "%H:%M")+
  xlab("Time")+
  ylab("Speed (Mbyte/s)")+
  ylim(c(0,5))+
  theme_bw()+
  theme(strip.background = element_blank(),
        legend.position = "bottom",
        panel.grid = element_line(linetype = "dashed"))

ggsave("Speedtest_Figure.png")


Current output

这会产生如下所示的输出:

Current_output

Requested change

是否可以在每个方面添加文本,如下所示:

Mean Download: NNN
Mean Upload: MMM

其中,nnn和MMM是每天的平均下载和上传速度,每个方面都会发生变化.如果可能的话,我想在单个代码块内,在管道内完成这项工作.

My attempt

我try 了一种非最佳的方法,即创建不同的对象,然后使用geom_text将信息输入到情节中.

我创建了一个单独的对象Mean.dat

Mean.dat<-df%>%
  select(Date,Time, Upload, Download)%>%
  mutate(Date = as.POSIXct(Date, format = "%Y/%m/%d"),
         Time = as.POSIXct(Time, format = "%H:%M:%S"))%>%
  group_by(Date)%>%
  summarise(mean(Download)/8000000)

然后将此位添加到主代码中:

  geom_text(data=Mean.dat,
            aes(x=14, y=5, label=`mean(Download)/8e+06`), 
            colour="black", inherit.aes=FALSE, hjust = -1)

但我收到了错误消息:

Error: Invalid input: time_trans works with objects of class POSIXct only

其他资源

我曾提及以下消息来源:

Add text to a faceted plot in ggplot2 with dates on X axis

Creating a facet_wrap plot with ggplot2 with different annotations in each plot

Annotate ggplot2 facets with number of observations per facet

How to add annotation on each facet

add unique text to each facet ggplot

我在以前的try 中try 修改代码,但日期-时间问题似乎是持久性的.

Addendum

为了使代码更具可重复性,我被要求使用dput()提供我的数据片段.

以下是dput(df[c(1:5, 10:15, 20:25, 30:35, 40:41),])的输出

structure(list(Date = c("2023/11/04", "2023/11/04", "2023/11/04", 
"2023/11/04", "2023/11/04", "2023/11/04", "2023/11/04", "2023/11/04", 
"2023/11/04", "2023/11/04", "2023/11/04", "2023/11/04", "2023/11/04", 
"2023/11/04", "2023/11/04", "2023/11/05", "2023/11/05", "2023/11/05", 
"2023/11/05", "2023/11/05", "2023/11/05", "2023/11/05", "2023/11/05", 
"2023/11/05", "2023/11/05"), Time = c("13:05:32", "13:20:03", 
"13:40:05", "14:20:03", "14:40:03", "17:20:03", "17:40:02", "18:20:03", 
"18:40:03", "19:20:04", "19:40:03", "22:20:03", "22:40:03", "23:20:03", 
"23:40:03", "00:20:03", "00:40:03", "03:20:03", "03:40:03", "04:20:03", 
"04:40:03", "05:20:03", "05:40:03", "08:20:03", "08:40:03"), 
    Download = c(34957192.9969772, 35826556.3982541, 27695436.936076, 
    32785866.6580349, 34373754.7935802, 29644745.5493678, 31936459.8397868, 
    32782764.8827361, 31366450.4288069, 10709016.6772629, 32722858.0348491, 
    34821984.5787153, 28379214.5120736, 26820887.0474698, 31839780.4165726, 
    32066886.2373525, 33440458.6440393, 28113353.9035434, 26284377.6573347, 
    29154520.6902359, 34918254.2123446, 21598680.1274404, 20700752.1868799, 
    34638409.416459, 34572097.5993048), Upload = c(20046840.2637393, 
    36143231.5378943, 4957720.87281616, 15688120.7580889, 35845959.9473685, 
    18072485.390123, 9069468.67273845, 6973860.4270036, 4476811.81028971, 
    8848402.0645949, 2045099.79491319, 23198345.7376053, 31702122.0677866, 
    11711052.7340582, 12556196.1275965, 28941390.4693129, 21543697.8944099, 
    12966120.4632239, 28660937.1396553, 28476185.8084195, 16678584.8862002, 
    29032008.7959507, 17276854.3732636, 36479144.7960276, 37478780.5131303
    )), row.names = c(1L, 2L, 3L, 4L, 5L, 10L, 11L, 12L, 13L, 
14L, 15L, 20L, 21L, 22L, 23L, 24L, 25L, 30L, 31L, 32L, 33L, 34L, 
35L, 40L, 41L), class = "data.frame")

推荐答案

正如我在我的 comments 中提到的,第二种方法的问题是您必须将标签的x轴值也指定为日期时间,即DO x = as.POSIXct("14:00", format = "%H:%M").

然而,正如您提到的,您的首选或最佳方法是在一个管道中完成所有计算,而不是使用第二个数据集.下面是一种使用stat_summary添加标签的方法.

library(tidyverse)

df %>%
  select(Date, Time, Upload, Download) %>%
  pivot_longer(
    cols = c("Upload", "Download"),
    names_to = "Parameter", values_to = "Value"
  ) %>%
  mutate(
    Date = as.POSIXct(Date, format = "%Y/%m/%d"),
    Time = as.POSIXct(Time, format = "%H:%M:%S")
  ) %>%
  ggplot(aes(x = Time, y = (Value / 8000000), colour = Parameter)) +
  geom_line() +
  geom_point() +
  stat_summary(
    data = ~filter(.x, Parameter == "Download"),
    geom = "text",
    aes(
      x = as.POSIXct("14:00", format = "%H:%M"),
      y = stage(Value / 8000000, after_stat = 5),
      label = after_stat(round(y, 2))
    ),
    fun = mean, show.legend = FALSE
  ) +
  facet_wrap(~Date, ncol = 3, nrow = 2) +
  scale_colour_manual(values = c("blue", "orange")) +
  scale_x_datetime(
    date_breaks = "4 hours",
    date_labels = "%H:%M"
  ) +
  xlab("Time") +
  ylab("Speed (Mbyte/s)") +
  ylim(c(0, 5)) +
  theme_bw() +
  theme(
    strip.background = element_blank(),
    legend.position = "bottom",
    panel.grid = element_line(linetype = "dashed")
  )

R相关问答推荐

过滤Expand.Grid的结果

在R中使用GG Plot时如何 suppress 等值线图中的彩色条

在ubuntu 22.04上更新到R4.4后包安装出现编译错误

卸载安装了BRM的模型发出的警告

混淆矩阵,其中每列和等于1

如何使用rmarkdown和kableExtra删除包含折叠行的表的第一列的名称

筛选出以特定顺序患病的个体

自动变更列表

在GGPLATE中将突出的点放在前面

根据日期从参考帧中创建不同的帧

R函数,用于生成伪随机二进制序列,其中同一数字在一行中不出现超过两次

在使用bslb和bootstrap5时,有没有办法更改特定dt行的 colored颜色 ?

使用范围和单个数字将数字与字符串进行比较

如何在ggplot2中绘制具有特定 colored颜色 的连续色轮

您是否可以将组添加到堆叠的柱状图

变长向量的矢量化和

有没有办法将不等长的列表转换为R中的数据帧

如果满足条件,则替换列的前一个值和后续值

在shiny 表格中输入的文本在第一次后未更新

在不带max()的data.table中按组查找最后一个元素