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
这会产生如下所示的输出:
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")