我想创建一个线图,以显示特定日期内的不同测量时间。请参阅下面的线图 - 但我如何更改为每天有 7 种不同类型的线而不是尖峰?每行定义一天。所以六个盒子中的七种颜色相同,但时间/形状不同。为什么这条线这么尖?
更新 1 @Elia 提供了一个解决方案,我设法创建了情节
更新 2:如何将时间格式化为从 04:00 开始?
基本上,我的目标是每个盒子下面的图
示例代码:
(plot<-ggplot(df, aes(x = time,y = value, group=variable)) +
geom_line(aes(color=Day)) +
labs(x="Time", y="%", colour="Day of the week" ) +
facet_wrap(~variable)+
theme_bw()+
theme(legend.position="right", axis.text.x = element_text(angle = 0, hjust = 0)))
样本数据前 200 个观察值:
`df<-` structure(list(time = structure(c(14400, 15000, 15600, 16200,
16800, 17400, 18000, 18600, 19200, 19800, 20400, 21000, 21600,
22200, 22800, 23400, 24000, 24600, 25200, 25800, 26400, 27000,
27600, 28200, 28800, 29400, 30000, 30600, 31200, 31800, 32400,
33000, 33600, 34200, 34800, 35400, 36000, 36600, 37200, 37800,
38400, 39000, 39600, 40200, 40800, 41400, 42000, 42600, 43200,
43800, 44400, 45000, 45600, 46200, 46800, 47400, 48000, 48600,
49200, 49800, 50400, 51000, 51600, 52200, 52800, 53400, 54000,
54600, 55200, 55800, 56400, 57000, 57600, 58200, 58800, 59400,
60000, 60600, 61200, 61800, 62400, 63000, 63600, 64200, 64800,
65400, 66000, 66600, 67200, 67800, 68400, 69000, 69600, 70200,
70800, 71400, 72000, 72600, 73200, 73800, 74400, 75000, 75600,
76200, 76800, 77400, 78000, 78600, 79200, 79800, 80400, 81000,
81600, 82200, 82800, 83400, 84000, 84600, 85200, 85800, 0, 600,
1200, 1800, 2400, 3000, 3600, 4200, 4800, 5400, 6000, 6600, 7200,
7800, 8400, 9000, 9600, 10200, 10800, 11400, 12000, 12600, 13200,
13800, 14400, 15000, 15600, 16200, 16800, 17400, 18000, 18600,
19200, 19800, 20400, 21000, 21600, 22200, 22800, 23400, 24000,
24600, 25200, 25800, 26400, 27000, 27600, 28200, 28800, 29400,
30000, 30600, 31200, 31800, 32400, 33000, 33600, 34200, 34800,
35400, 36000, 36600, 37200, 37800, 38400, 39000, 39600, 40200,
40800, 41400, 42000, 42600, 43200, 43800, 44400, 45000, 45600,
46200, 46800, 47400), class = c("hms", "difftime"), units = "secs"),
variable = c("A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"), value = c(0.221281741,
0.220878678, 0.220777912, 0.220777912, 0.220878678, 0.219770254,
0.218863362, 0.218158001, 0.217251108, 0.216646514, 0.215336558,
0.213422007, 0.210096735, 0.206771463, 0.20274083, 0.1984079,
0.191152761, 0.187726723, 0.177247078, 0.172007255, 0.161729141,
0.152861749, 0.14439742, 0.132708585, 0.119407497, 0.106005643,
0.098045143, 0.091596131, 0.08655784, 0.078698106, 0.071241435,
0.068419992, 0.063280935, 0.059754131, 0.056328094, 0.052902056,
0.052297461, 0.047158404, 0.043027005, 0.042220879, 0.039903265,
0.037384119, 0.03688029, 0.03496574, 0.031942765, 0.028113664,
0.026199113, 0.031741233, 0.04725917, 0.053305119, 0.059048771,
0.057436518, 0.054312777, 0.053607416, 0.040205562, 0.036779524,
0.032748892, 0.02891979, 0.026199113, 0.025292221, 0.021563886,
0.02025393, 0.02025393, 0.020354696, 0.02216848, 0.023679968,
0.024687626, 0.024284563, 0.022873841, 0.020959291, 0.020455462,
0.020052398, 0.01904474, 0.017734784, 0.017230955, 0.01783555,
0.019850867, 0.023579202, 0.030330512, 0.036477227, 0.044639258,
0.054917372, 0.060560258, 0.068117694, 0.076279726, 0.079201935,
0.078194277, 0.068520758, 0.06076179, 0.055320435, 0.044437727,
0.04171705, 0.038996372, 0.034663442, 0.033252721, 0.033151955,
0.0356711, 0.036376461, 0.037686417, 0.036678759, 0.034663442,
0.03688029, 0.038391778, 0.037283353, 0.041414752, 0.046856106,
0.050584442, 0.057033454, 0.074365175, 0.078395808, 0.08655784,
0.099858928, 0.110439339, 0.12535268, 0.148730351, 0.157496977,
0.167976622, 0.177146312, 0.183192261, 0.189137445, 0.195888755,
0.200221685, 0.203546957, 0.206469166, 0.208081419, 0.210096735,
0.21311971, 0.213623539, 0.213825071, 0.21503426, 0.215235792,
0.216041919, 0.216646514, 0.217150343, 0.217351874, 0.217754938,
0.217754938, 0.218359532, 0.21866183, 0.21866183, 0.21866183,
0.218561064, 0.218561064, 0.218762596, 0.219568722, 0.218561064,
0.218863362, 0.218359532, 0.218561064, 0.217855703, 0.21745264,
0.217150343, 0.216444982, 0.215941153, 0.213422007, 0.21191052,
0.209189843, 0.20515921, 0.20153164, 0.195687223, 0.189540508,
0.183393793, 0.172309553, 0.165255945, 0.156489319, 0.143490528,
0.133716243, 0.123538896, 0.109230149, 0.096735187, 0.08847239,
0.081922612, 0.075171302, 0.069931479, 0.061567916, 0.057335752,
0.05401048, 0.051189037, 0.047561467, 0.047158404, 0.042321644,
0.039600967, 0.036981056, 0.032648126, 0.032345828, 0.028718259,
0.03133817, 0.028315195, 0.025292221, 0.025997582, 0.025392987,
0.03133817, 0.045143087, 0.0515921, 0.054816606, 0.05834341,
0.053002821, 0.052297461, 0.040709391, 0.037988714), Day = c("Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday",
"Monday", "Monday", "Monday", "Monday", "Monday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday")), spec = structure(list(cols = list(time = structure(list(
format = ""), class = c("collector_time", "collector")),
variable = structure(list(), class = c("collector_character",
"collector")), value = structure(list(), class = c("collector_double",
"collector")), Day = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"), row.names = c(NA,
200L), class = "data.frame")
答案 0 :(得分:1)
只需将 group
ing 变量更改为 Day
:
library(ggplot2)
(plot<-ggplot(df, aes(x = time,y = value, group=Day)) +
geom_line(aes(color=Day)) +
labs(x="Time", y="%", colour="Day of the week" ) +
facet_wrap(~variable)+
theme_bw()+
theme(legend.position="right", axis.text.x = element_text(angle = 0, hjust = 0)))
通过您发布的示例数据,我获得了: