X

时间:2016-05-06 20:17:21

标签: r ggplot2

我有每小时的价值观,并且我试图让它变得更好,因为x标签是不可读的,我不能让它变小,因为它已经足够了。

有没有简单的方法来改善它?也许显示x1,x3,x5,x7。如果我能以这种方式显示,它适合我,但如何做到这一点?

这是数据:

    k1=structure(list(Heure = structure(c(428L, 7L, 11L, 20L, 28L, 41L, 
57L, 75L, 92L, 112L, 128L, 138L, 146L, 154L, 163L, 425L, 8L, 
12L, 16L, 21L, 29L, 42L, 129L, 140L, 147L, 155L, 164L, 52L, 61L, 
70L, 79L, 87L, 97L, 107L, 117L, 2L, 10L, 18L, 27L, 32L, 39L, 
46L, 54L, 63L, 72L, 81L, 89L, 99L, 109L, 119L, 136L, 145L, 153L, 
162L, 15L, 24L, 34L, 48L, 66L, 84L, 102L, 122L, 133L, 142L, 149L, 
158L, 166L, 25L, 35L, 49L, 58L, 67L, 76L, 85L, 93L, 103L, 113L, 
123L, 134L, 143L, 150L, 159L, 55L, 64L, 73L, 82L, 90L, 100L, 
110L, 120L, 30L, 37L, 44L, 51L, 60L, 69L, 78L, 86L, 95L, 105L, 
115L, 126L, 26L, 430L, 1L, 5L, 14L, 23L, 131L, 141L, 148L, 156L, 
165L, 240L, 247L, 254L, 264L, 275L, 285L, 295L, 305L, 315L, 173L, 
181L, 189L, 197L, 205L, 213L, 221L, 230L, 363L, 373L, 381L, 389L, 
396L, 403L, 416L, 237L, 244L, 252L, 261L, 271L, 171L, 179L, 187L, 
195L, 203L, 211L, 219L, 228L, 236L, 251L, 269L, 290L, 310L, 329L, 
345L, 359L, 371L, 379L, 387L, 394L, 401L, 407L, 414L, 421L, 172L, 
180L, 188L, 196L, 204L, 212L, 220L, 229L, 239L, 253L, 380L, 388L, 
398L, 412L, 170L, 178L, 186L, 194L, 202L, 210L, 218L, 226L, 235L, 
249L, 266L, 287L, 307L, 326L, 343L, 357L, 370L, 378L, 386L, 393L, 
400L, 410L, 424L, 174L, 182L, 190L, 198L, 206L, 214L, 222L, 231L, 
243L, 259L, 280L, 300L, 320L, 338L, 352L, 365L, 374L, 382L, 390L, 
167L, 175L, 183L, 191L, 199L, 207L, 215L, 223L, 232L, 245L, 262L, 
273L, 283L, 293L, 303L, 313L, 323L, 332L, 340L, 347L, 354L, 361L, 
367L, 372L, 375L, 383L, 391L, 405L, 418L, 268L, 279L, 289L, 242L, 
256L, 277L, 297L, 317L, 335L, 349L), .Label = c("04:41:00", "04:56:00", 
"05:05:00", "05:10:00", "05:11:00", "05:15:00", "05:16:00", "05:20:00", 
"05:21:00", "05:25:00", "05:31:00", "05:35:00", "05:40:00", "05:41:00", 
"05:45:00", "05:50:00", "05:51:00", "05:56:00", "06:00:00", "06:01:00", 
"06:05:00", "06:10:00", "06:11:00", "06:15:00", "06:21:00", "06:23:00", 
"06:25:00", "06:30:00", "06:35:00", "06:38:00", "06:40:00", "06:41:00", 
"06:45:00", "06:46:00", "06:50:00", "06:51:00", "06:53:00", "06:55:00", 
"06:56:00", "07:00:00", "07:01:00", "07:04:00", "07:05:00", "07:08:00", 
"07:10:00", "07:11:00", "07:15:00", "07:16:00", "07:18:00", "07:19:00", 
"07:21:00", "07:24:00", "07:25:00", "07:27:00", "07:29:00", "07:30:00", 
"07:31:00", "07:33:00", "07:35:00", "07:36:00", "07:39:00", "07:40:00", 
"07:42:00", "07:44:00", "07:45:00", "07:46:00", "07:48:00", "07:49:00", 
"07:51:00", "07:54:00", "07:55:00", "07:57:00", "07:59:00", "08:00:00", 
"08:01:00", "08:03:00", "08:05:00", "08:06:00", "08:09:00", "08:10:00", 
"08:12:00", "08:14:00", "08:15:00", "08:16:00", "08:19:00", "08:21:00", 
"08:24:00", "08:25:00", "08:27:00", "08:29:00", "08:30:00", "08:31:00", 
"08:33:00", "08:35:00", "08:36:00", "08:37:00", "08:39:00", "08:40:00", 
"08:42:00", "08:44:00", "08:45:00", "08:46:00", "08:48:00", "08:50:00", 
"08:51:00", "08:52:00", "08:54:00", "08:55:00", "08:57:00", "08:59:00", 
"09:00:00", "09:01:00", "09:03:00", "09:05:00", "09:06:00", "09:07:00", 
"09:09:00", "09:10:00", "09:12:00", "09:14:00", "09:15:00", "09:16:00", 
"09:18:00", "09:20:00", "09:22:00", "09:25:00", "09:27:00", "09:30:00", 
"09:35:00", "09:37:00", "09:40:00", "09:42:00", "09:45:00", "09:50:00", 
"09:55:00", "09:56:00", "09:57:00", "10:00:00", "10:01:00", "10:05:00", 
"10:10:00", "10:15:00", "10:20:00", "10:25:00", "10:26:00", "10:30:00", 
"10:35:00", "10:40:00", "10:45:00", "10:50:00", "10:51:00", "10:55:00", 
"10:56:00", "11:00:00", "11:05:00", "11:09:00", "11:10:00", "11:15:00", 
"11:20:00", "11:21:00", "11:25:00", "11:26:00", "11:30:00", "11:35:00", 
"11:40:00", "11:45:00", "11:50:00", "11:51:00", "11:55:00", "11:56:00", 
"12:00:00", "12:05:00", "12:10:00", "12:15:00", "12:20:00", "12:21:00", 
"12:25:00", "12:26:00", "12:30:00", "12:35:00", "12:40:00", "12:45:00", 
"12:50:00", "12:51:00", "12:55:00", "12:56:00", "13:00:00", "13:05:00", 
"13:10:00", "13:15:00", "13:20:00", "13:21:00", "13:25:00", "13:26:00", 
"13:30:00", "13:35:00", "13:40:00", "13:45:00", "13:50:00", "13:51:00", 
"13:55:00", "13:56:00", "14:00:00", "14:05:00", "14:10:00", "14:15:00", 
"14:20:00", "14:21:00", "14:25:00", "14:26:00", "14:30:00", "14:35:00", 
"14:40:00", "14:45:00", "14:50:00", "14:51:00", "14:55:00", "14:56:00", 
"15:00:00", "15:05:00", "15:10:00", "15:15:00", "15:20:00", "15:21:00", 
"15:25:00", "15:26:00", "15:30:00", "15:31:00", "15:35:00", "15:40:00", 
"15:45:00", "15:50:00", "15:51:00", "15:55:00", "15:56:00", "16:00:00", 
"16:03:00", "16:05:00", "16:06:00", "16:08:00", "16:10:00", "16:11:00", 
"16:15:00", "16:18:00", "16:20:00", "16:21:00", "16:23:00", "16:25:00", 
"16:26:00", "16:30:00", "16:31:00", "16:33:00", "16:35:00", "16:38:00", 
"16:40:00", "16:41:00", "16:43:00", "16:44:00", "16:46:00", "16:47:00", 
"16:48:00", "16:50:00", "16:52:00", "16:53:00", "16:54:00", "16:56:00", 
"16:58:00", "16:59:00", "17:01:00", "17:02:00", "17:03:00", "17:04:00", 
"17:05:00", "17:07:00", "17:08:00", "17:09:00", "17:11:00", "17:13:00", 
"17:14:00", "17:16:00", "17:17:00", "17:19:00", "17:20:00", "17:22:00", 
"17:23:00", "17:24:00", "17:26:00", "17:28:00", "17:29:00", "17:31:00", 
"17:32:00", "17:34:00", "17:35:00", "17:37:00", "17:38:00", "17:39:00", 
"17:41:00", "17:43:00", "17:44:00", "17:45:00", "17:47:00", "17:49:00", 
"17:50:00", "17:52:00", "17:53:00", "17:54:00", "17:56:00", "17:58:00", 
"17:59:00", "18:01:00", "18:02:00", "18:04:00", "18:05:00", "18:07:00", 
"18:08:00", "18:09:00", "18:11:00", "18:13:00", "18:14:00", "18:15:00", 
"18:17:00", "18:19:00", "18:20:00", "18:22:00", "18:24:00", "18:26:00", 
"18:28:00", "18:29:00", "18:30:00", "18:32:00", "18:34:00", "18:35:00", 
"18:37:00", "18:39:00", "18:41:00", "18:43:00", "18:44:00", "18:45:00", 
"18:49:00", "18:50:00", "18:52:00", "18:55:00", "18:56:00", "18:59:00", 
"19:00:00", "19:04:00", "19:05:00", "19:07:00", "19:10:00", "19:13:00", 
"19:14:00", "19:15:00", "19:18:00", "19:20:00", "19:22:00", "19:25:00", 
"19:26:00", "19:29:00", "19:30:00", "19:33:00", "19:35:00", "19:37:00", 
"19:40:00", "19:43:00", "19:45:00", "19:49:00", "19:50:00", "19:52:00", 
"19:55:00", "19:56:00", "20:00:00", "20:05:00", "20:10:00", "20:15:00", 
"20:20:00", "20:21:00", "20:25:00", "20:26:00", "20:30:00", "20:35:00", 
"20:40:00", "20:45:00", "20:50:00", "20:51:00", "20:55:00", "20:56:00", 
"21:00:00", "21:05:00", "21:10:00", "21:15:00", "21:20:00", "21:21:00", 
"21:26:00", "21:30:00", "21:35:00", "21:40:00", "21:45:00", "21:49:00", 
"21:51:00", "21:56:00", "22:00:00", "22:05:00", "22:10:00", "22:15:00", 
"22:19:00", "22:21:00", "22:30:00", "22:35:00", "22:40:00", "22:41:00", 
"22:45:00", "22:49:00", "22:51:00", "23:00:00", "23:05:00", "23:10:00", 
"23:15:00", "23:19:00", "23:21:00", "23:30:00", "23:31:00", "23:35:00", 
"23:40:00", "23:41:00", "23:49:00", "23:51:00", "24:00:00", "24:01:00", 
"24:05:00", "24:10:00", "24:21:00", "24:30:00", "24:40:00", "24:50:00"
), class = "factor"), X = c(9L, 1L, 14L, 7L, 15L, 16L, 19L, 67L, 
84L, 50L, 32L, 28L, 24L, 33L, 26L, 9L, 57L, 7L, 22L, 13L, 22L, 
17L, 30L, 39L, 26L, 33L, 21L, 35L, 16L, 38L, 42L, 45L, 18L, 9L, 
24L, 4L, 9L, 8L, 28L, 5L, 21L, 28L, 21L, 41L, 47L, 101L, 107L, 
62L, 69L, 10L, 34L, 8L, 15L, 17L, 6L, 15L, 17L, 21L, 36L, 59L, 
84L, 54L, 36L, 54L, 33L, 33L, 33L, 21L, 26L, 26L, 32L, 49L, 42L, 
46L, 130L, 101L, 50L, 48L, 54L, 70L, 20L, 56L, 8L, 12L, 28L, 
22L, 21L, 22L, 22L, 31L, 2L, 17L, 24L, 11L, 8L, 31L, 24L, 31L, 
45L, 74L, 18L, 44L, 51L, 9L, 9L, 9L, 0L, 4L, 22L, 48L, 0L, 6L, 
13L, 35L, 55L, 24L, 42L, 52L, 65L, 62L, 40L, 43L, 23L, 23L, 13L, 
22L, 5L, 15L, 20L, 18L, 34L, 49L, 11L, 19L, 13L, 18L, 2L, 18L, 
25L, 30L, 25L, 63L, 34L, 17L, 44L, 25L, 38L, 22L, 16L, 46L, 34L, 
77L, 32L, 73L, 49L, 71L, 43L, 33L, 30L, 35L, 31L, 29L, 8L, 20L, 
9L, 29L, 36L, 58L, 24L, 32L, 21L, 31L, 47L, 27L, 29L, 15L, 26L, 
0L, 21L, 15L, 23L, 31L, 7L, 19L, 14L, 29L, 11L, 37L, 36L, 6L, 
13L, 57L, 37L, 90L, 57L, 45L, 41L, 19L, 8L, 17L, 13L, 28L, 6L, 
44L, 76L, 29L, 54L, 43L, 24L, 17L, 22L, 42L, 58L, 30L, 44L, 62L, 
86L, 72L, 65L, 28L, 17L, 31L, 45L, 32L, 39L, 25L, 48L, 35L, 33L, 
29L, 31L, 41L, 31L, 30L, 82L, 89L, 75L, 81L, 80L, 95L, 79L, 66L, 
67L, 64L, 44L, 52L, 38L, 26L, 27L, 6L, 4L, 53L, 69L, 45L, 23L, 
14L, 49L, 51L, 58L, 55L, 31L), Y = c(26L, 120L, 65L, 79L, 173L, 
239L, 282L, 313L, 438L, 330L, 238L, 149L, 119L, 79L, 57L, 18L, 
27L, 35L, 34L, 65L, 163L, 216L, 167L, 101L, 89L, 64L, 74L, 328L, 
309L, 366L, 278L, 426L, 450L, 442L, 341L, 8L, 53L, 85L, 150L, 
139L, 189L, 256L, 344L, 389L, 507L, 587L, 553L, 497L, 525L, 247L, 
376L, 149L, 205L, 263L, 134L, 92L, 175L, 311L, 308L, 395L, 445L, 
367L, 189L, 141L, 114L, 101L, 86L, 94L, 172L, 194L, 215L, 311L, 
330L, 452L, 420L, 296L, 249L, 220L, 171L, 121L, 196L, 72L, 239L, 
347L, 467L, 442L, 500L, 414L, 319L, 333L, 204L, 318L, 384L, 243L, 
197L, 311L, 353L, 378L, 365L, 538L, 298L, 334L, 0L, 19L, 14L, 
14L, 111L, 154L, 494L, 198L, 0L, 163L, 141L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 123L, 164L, 158L, 127L, 110L, 98L, 132L, 
108L, 77L, 68L, 61L, 32L, 38L, 27L, 23L, 0L, 0L, 0L, 0L, 0L, 
96L, 115L, 65L, 97L, 69L, 113L, 42L, 150L, 114L, 189L, 208L, 
278L, 221L, 233L, 155L, 128L, 49L, 86L, 53L, 38L, 32L, 98L, 45L, 
25L, 56L, 56L, 62L, 66L, 68L, 70L, 62L, 48L, 76L, 76L, 34L, 0L, 
48L, 35L, 137L, 91L, 110L, 135L, 110L, 101L, 93L, 69L, 93L, 68L, 
107L, 139L, 194L, 156L, 120L, 123L, 96L, 49L, 43L, 65L, 32L, 
43L, 22L, 123L, 72L, 112L, 76L, 100L, 122L, 106L, 99L, 174L, 
169L, 253L, 338L, 286L, 267L, 222L, 151L, 70L, 65L, 49L, 114L, 
58L, 91L, 72L, 92L, 73L, 64L, 81L, 75L, 93L, 93L, 97L, 145L, 
130L, 157L, 119L, 114L, 90L, 118L, 85L, 62L, 100L, 54L, 77L, 
42L, 38L, 45L, 12L, 28L, 0L, 0L, 0L, 109L, 110L, 183L, 197L, 
121L, 134L, 92L), Z = c(40L, 37L, 120L, 247L, 236L, 318L, 421L, 
468L, 721L, 535L, 352L, 207L, 257L, 167L, 176L, 22L, 130L, 98L, 
133L, 128L, 177L, 222L, 355L, 228L, 178L, 115L, 187L, 458L, 369L, 
469L, 413L, 593L, 464L, 466L, 360L, 42L, 160L, 219L, 390L, 378L, 
450L, 501L, 527L, 646L, 755L, 1002L, 1045L, 803L, 645L, 709L, 
462L, 260L, 305L, 178L, 94L, 157L, 202L, 380L, 402L, 668L, 658L, 
413L, 215L, 229L, 129L, 162L, 146L, 193L, 235L, 269L, 332L, 386L, 
427L, 624L, 718L, 809L, 504L, 403L, 399L, 295L, 350L, 189L, 334L, 
405L, 644L, 563L, 692L, 504L, 483L, 416L, 311L, 429L, 382L, 338L, 
312L, 416L, 469L, 547L, 599L, 733L, 289L, 612L, 51L, 45L, 23L, 
60L, 113L, 236L, 606L, 425L, 0L, 177L, 172L, 35L, 55L, 24L, 42L, 
52L, 65L, 62L, 40L, 43L, 215L, 248L, 158L, 268L, 127L, 193L, 
194L, 193L, 143L, 85L, 92L, 72L, 51L, 47L, 59L, 18L, 25L, 30L, 
25L, 63L, 142L, 177L, 181L, 185L, 166L, 130L, 140L, 521L, 224L, 
253L, 310L, 363L, 262L, 317L, 187L, 143L, 113L, 117L, 93L, 61L, 
74L, 76L, 55L, 62L, 189L, 152L, 95L, 160L, 112L, 152L, 157L, 
129L, 112L, 136L, 79L, 0L, 71L, 60L, 195L, 224L, 212L, 274L, 
215L, 273L, 128L, 243L, 195L, 210L, 223L, 299L, 324L, 215L, 239L, 
197L, 200L, 83L, 65L, 50L, 49L, 77L, 57L, 153L, 221L, 186L, 180L, 
208L, 198L, 175L, 244L, 365L, 399L, 423L, 455L, 448L, 410L, 265L, 
210L, 149L, 135L, 100L, 208L, 217L, 255L, 203L, 212L, 127L, 169L, 
128L, 203L, 174L, 181L, 210L, 304L, 301L, 268L, 320L, 277L, 304L, 
188L, 127L, 131L, 156L, 135L, 162L, 107L, 70L, 133L, 54L, 24L, 
53L, 69L, 45L, 185L, 192L, 379L, 294L, 296L, 338L, 204L)), .Names = c("Heure", 
"X", "Y", "Z"), row.names = c(NA, -261L), class = "data.frame")

这是我的代码:

meltk1 = melt(k1, id = "Heure")
ggplot(meltk1, aes(x=Heure , y = value, group = variable, colour = variable)) +
   geom_line(size=1) + scale_x_discrete(name="Heure") + theme_bw() +
   theme(plot.title = element_text(size=16,lineheight=2, face="bold"),
   legend.text=element_text(size=14),
   legend.title=element_text(size=14, face="bold"),
   axis.text.y = element_text(size=12),
   axis.text.x = element_text(size=8, angle=45),
   strip.background = element_rect(fill = "White") ) 
   ggsave(filename=paste("Dynamique",".tiff", sep = ""), width = 80, height = 30, units = "cm")

enter image description here

编辑: 我使用以下代码转换Heure

Heure=strptime(mydata1$Heure,format='%H:%M:%S')

它只给我7个X值,但这还不够。我每两个至少需要一个值。

enter image description here

EDIT2:大约2点和3点在红场看 enter image description here

编辑3:这是我运行代码时获得的。同样的问题,我多次重新运行R。奇怪的是,我没有与你的情节相同的情节 enter image description here

EDIT4:TZ的问题,以下代码解决了问题,只有"最后"问题我希望,它不解释24:00:00。我无法在00:00:00转换它,因为它表示它是同一天,而24:00:00表示以下几天。这似乎很复杂。

k1$Hour=strptime(as.character(k1$Heure),format='%H:%M:%S',tz="Etc/GMT")
meltk1$Hour=strptime(as.character(meltk1$Heure),format='%H:%M:%S',tz="Etc/GMT")

1 个答案:

答案 0 :(得分:2)

以下代码附有说明和图片。将Heure变量更改为实际时间变量非常重要,因为时间间隔不一致。

#load the required packages
library(reshape2)
library(ggplot2)
library(scales)


#Omitted data load step
meltk1 = melt(k1, id = "Heure")
#Now add a variable that will be the hour, but format it to POSXICT
k1$Hour=strptime(as.character(k1$Heure),format='%H:%M:%S')
meltk1$Hour=strptime(as.character(meltk1$Heure),format='%H:%M:%S')

#Note that a few values were set to missing. I don't know why but think you should investigate.

ggplot(meltk1, aes(x=Hour , y = value, group = variable, colour = variable)) +
        geom_line(size=1) + 
        scale_x_datetime(breaks=date_breaks("1 hour"), labels=date_format("%H:%M:%S")) +  
        theme_bw() +
        theme(plot.title = element_text(size=16,lineheight=2, face="bold"),
              legend.text=element_text(size=14),
              legend.title=element_text(size=14, face="bold"),
              axis.text.y = element_text(size=12),
              axis.text.x = element_text(size=8, angle=45),
              strip.background = element_rect(fill = "White") ) 

scale_x_datetime行是最重要的部分。在这里,我将间隔设置为相隔1小时,您可以将其更改为各种级别,请参阅帮助文件。格式需要以这种方式应用,否则它也会添加日期。 最终结果是一张精美的图表:

enter image description here

但是,看起来您的某些时间格式无法使用strptime进行解析,因此您必须手动编辑这些格式。