ggplot中的虚轴

时间:2018-12-24 09:54:12

标签: r ggplot2

我想在两个XY轴下绘图

  • 第一个将具有XTimecodeY作为每行的累积量,因为Timecode上的第= 1行
  • 最大轴是YSample_HourXType

enter image description here

df=structure(list(Sample_ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), Sample_Hour = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("17:03:13", "17:05:44", "17:08:44", "17:11:39", 
"17:14:39", "17:18:06", "17:21:13", "17:24:02", "17:27:05", "17:32:43", 
"17:35:59", "17:38:40", "17:41:26", "17:44:43", "17:48:14", "17:51:14", 
"17:55:00", "17:57:16", "18:00:12", "18:03:16", "18:06:03", "18:09:59", 
"18:12:49", "18:15:37", "18:18:01", "18:21:31", "18:26:09", "18:27:58", 
"18:29:52", "18:33:21", "18:36:40", "18:38:50", "18:41:25", "18:44:35", 
"18:48:45", "18:52:08", "18:54:19", "18:59:52", "19:02:28", "19:06:04", 
"19:11:18", "19:15:03", "19:18:06", "19:21:37"), class = "factor"), 
    Direction = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Timecode = structure(c(1545667346, 
    1545667367, 1545667368, 1545667369, 1545667370, 1545667379, 
    1545667382, 1545667383, 1545667387, 1545667390, 1545667391, 
    1545667482, 1545667482, 1545667483, 1545667483, 1545667483, 
    1545667483, 1545667483, 1545667483, 1545667484, 1545667484, 
    1545667484, 1545667484, 1545667484, 1545667484, 1545667484, 
    1545667484, 1545667484, 1545667484, 1545667484, 1545667484, 
    1545667484, 1545667484, 1545667485, 1545667485, 1545667485, 
    1545667485, 1545667485, 1545667485, 1545667485, 1545667485, 
    1545667485, 1545667485, 1545667485, 1545667485, 1545667485, 
    1545667485, 1545667486, 1545667486, 1545667486, 1545667486, 
    1545667486, 1545667486, 1545667486, 1545667486, 1545667486, 
    1545667486, 1545667486, 1545667487, 1545667487, 1545667487, 
    1545667487, 1545667487, 1545667487, 1545667487, 1545667487, 
    1545667487, 1545667487, 1545667487, 1545667487, 1545667487, 
    1545667487, 1545667488, 1545667488, 1545667488, 1545667488, 
    1545667488, 1545667488, 1545667488, 1545667488, 1545667488, 
    1545667489, 1545667489, 1545667489, 1545667489, 1545667489, 
    1545667489, 1545667489, 1545667490, 1545667490, 1545667490, 
    1545667490, 1545667491, 1545667491, 1545667491, 1545667491, 
    1545667491, 1545667491, 1545667491, 1545667492, 1545667492, 
    1545667493, 1545667493, 1545667493, 1545667494, 1545667494, 
    1545667494, 1545667494, 1545667495, 1545667495, 1545667496, 
    1545667497, 1545667497, 1545667498, 1545667498, 1545667498, 
    1545667498, 1545667499, 1545667499, 1545667500, 1545667500, 
    1545667501, 1545667501, 1545667501, 1545667502, 1545667503, 
    1545667503, 1545667503, 1545667504, 1545667504, 1545667504, 
    1545667504, 1545667505, 1545667505, 1545667505, 1545667505, 
    1545667505, 1545667506, 1545667506, 1545667506, 1545667507, 
    1545667507, 1545667507, 1545667508, 1545667508, 1545667509, 
    1545667509, 1545667509, 1545667509, 1545667510, 1545667510, 
    1545667510, 1545667510, 1545667510, 1545667511, 1545667511, 
    1545667511, 1545667512, 1545667512, 1545667512, 1545667513, 
    1545667513, 1545667514, 1545667514, 1545667514, 1545667515, 
    1545667515, 1545667516, 1545667516, 1545667516, 1545667516, 
    1545667517, 1545667517, 1545667517, 1545667518, 1545667518, 
    1545667518, 1545667519, 1545667520, 1545667520, 1545667521, 
    1545667521, 1545667522, 1545667522, 1545667522, 1545667522, 
    1545667522, 1545667523, 1545667523, 1545667523, 1545667523, 
    1545667523, 1545667524, 1545667524, 1545667524, 1545667524, 
    1545667524, 1545667525, 1545667525, 1545667526), class = c("POSIXct", 
    "POSIXt"), tzone = ""), Type = c("14", "1", "6", "14", "4", 
    "6", "14", "6", "4", "1", "6", "5", "6", "1", "8", "5", "4", 
    "7", "2", "2", "6", "5", "16", "8", "3", "5", "1", "1", "8", 
    "7", "9", "6", "11", "7", "5", "4", "9", "6", "5", "2", "1", 
    "12", "4", "4", "14", "14", "12", "7", "1", "6", "3", "4", 
    "5", "2", "12", "4", "5", "2", "9", "1", "14", "7", "1", 
    "4", "12", "5", "7", "14", "3", "5", "2", "9", "14", "4", 
    "6", "1", "2", "5", "4", "3", "9", "3", "1", "9", "4", "4", 
    "3", "1", "4", "4", "7", "1", "3", "4", "7", "1", "6", "4", 
    "4", "14", "14", "4", "4", "4", "14", "4", "4", "4", "4", 
    "6", "4", "1", "4", "7", "6", "7", "14", "7", "6", "7", "4", 
    "6", "4", "7", "6", "4", "7", "4", "7", "7", "1", "7", "6", 
    "14", "7", "4", "4", "6", "6", "7", "4", "4", "6", "7", "4", 
    "7", "4", "7", "4", "6", "7", "4", "4", "1", "6", "4", "4", 
    "6", "4", "4", "6", "4", "14", "6", "6", "6", "7", "4", "6", 
    "7", "4", "7", "4", "6", "14", "6", "1", "6", "7", "6", "6", 
    "4", "6", "6", "7", "7", "6", "7", "7", "6", "4", "1", "7", 
    "7", "14", "6", "7", "7", "6", "7"), Duration = c(0, 0, 0, 
    23, 0, 11, 13, 4, 17, 23, 8, 0, 0, 0, 0, 1, 0, 0, 0, 1, 2, 
    1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 2, 1, 1, 0, 1, 
    1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 1, 0, 0, 0, 2, 
    1, 2, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 2, 1, 1, 1, 
    0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 3, 1, 2, 1, 1, 1, 3, 
    0, 0, 4, 0, 2, 0, 0, 2, 1, 0, 0, 1, 4, 1, 6, 1, 7, 3, 0, 
    4, 1, 1, 1, 3, 2, 1, 1, 1, 2, 2, 0, 1, 0, 7, 0, 3, 7, 1, 
    2, 0, 1, 0, 1, 2, 0, 1, 2, 1, 1, 1, 0, 0, 3, 1, 1, 0, 6, 
    1, 1, 0, 1, 1, 0, 1, 1, 9, 1, 0, 1, 5, 3, 1, 1, 0, 1, 1, 
    1, 4, 1, 8, 1, 3, 1, 1, 4, 1, 0, 2, 0, 0, 1, 0, 1, 2, 5, 
    1, 0, 6, 1, 0, 1, 1, 1)), .Names = c("Sample_ID", "Sample_Hour", 
"Direction", "Timecode", "Type", "Duration"), row.names = c(NA, 
-200L), class = c("tbl_df", "tbl", "data.frame"))

1 个答案:

答案 0 :(得分:1)

dplyr方法

首先,创建一个表格,每组增加1(=观察值/人的总和)

result <- df %>% 
  group_by( Sample_ID, Type ) %>%
  mutate( nb = 1:n() )

然后创建一个多面的lins / point图。 free_x中的facet_grid使结果可读。您必须手动设置轴标签/中断

ggplot( data = result, aes( x = Timecode, y = nb ) ) +
  geom_line( aes( group = 1 ), size = 1 ) + 
  geom_point() + 
  facet_grid( Type ~ Sample_ID, scales = "free_x" ) + 
  scale_x_datetime( date_breaks = "10 secs", labels = date_format( "%H:%M:%S", tz = "" ) ) +
  theme(axis.text.x = element_text( angle = 90, vjust = 0.5, hjust = 1 ) )

enter image description here