Home > Mobile >  How to remove unwanted space in plot due to discontinuous time intervals in R?
How to remove unwanted space in plot due to discontinuous time intervals in R?

Time:08-14

I am trying to plot a dataset with 3 durations from July-December for 3 years (2019, 2020 & 2021). Below is my sample dataset:

df_temp <- structure(list(created_at = structure(c(18077, 18077, 18077, 
18077, 18077, 18077, 18077, 18077, 18084, 18084, 18084, 18084, 
18084, 18084, 18084, 18084, 18091, 18091, 18091, 18091, 18091, 
18091, 18091, 18091, 18098, 18098, 18098, 18098, 18098, 18098, 
18098, 18098, 18105, 18105, 18105, 18105, 18105, 18105, 18105, 
18105, 18112, 18112, 18112, 18112, 18112, 18112, 18112, 18112, 
18119, 18119, 18119, 18119, 18119, 18119, 18119, 18119, 18126, 
18126, 18126, 18126, 18126, 18126, 18126, 18126, 18133, 18133, 
18133, 18133, 18133, 18133, 18133, 18133, 18140, 18140, 18140, 
18140, 18140, 18140, 18140, 18140, 18147, 18147, 18147, 18147, 
18147, 18147, 18147, 18147, 18154, 18154, 18154, 18154, 18154, 
18154, 18154, 18154, 18161, 18161, 18161, 18161, 18161, 18161, 
18161, 18161, 18168, 18168, 18168, 18168, 18168, 18168, 18168, 
18168, 18175, 18175, 18175, 18175, 18175, 18175, 18175, 18175, 
18182, 18182, 18182, 18182, 18182, 18182, 18182, 18182, 18189, 
18189, 18189, 18189, 18189, 18189, 18189, 18189, 18196, 18196, 
18196, 18196, 18196, 18196, 18196, 18196, 18203, 18203, 18203, 
18203, 18203, 18203, 18203, 18203, 18210, 18210, 18210, 18210, 
18210, 18210, 18210, 18210, 18217, 18217, 18217, 18217, 18217, 
18217, 18217, 18217, 18224, 18224, 18224, 18224, 18224, 18224, 
18224, 18224, 18231, 18231, 18231, 18231, 18231, 18231, 18231, 
18231, 18238, 18238, 18238, 18238, 18238, 18238, 18238, 18238, 
18245, 18245, 18245, 18245, 18245, 18245, 18245, 18245, 18252, 
18252, 18252, 18252, 18252, 18252, 18252, 18252, 18259, 18259, 
18259, 18259, 18259, 18259, 18259, 18259, 18441, 18441, 18441, 
18441, 18441, 18441, 18441, 18441, 18448, 18448, 18448, 18448, 
18448, 18448, 18448, 18448, 18455, 18455, 18455, 18455, 18455, 
18455, 18455, 18455, 18462, 18462, 18462, 18462, 18462, 18462,
18462, 18462, 18469, 18469, 18469, 18469, 18469, 18469, 18469, 
18469, 18476, 18476, 18476, 18476, 18476, 18476, 18476, 18476, 
18483, 18483, 18483, 18483, 18483, 18483, 18483, 18483, 18490, 
18490, 18490, 18490, 18490, 18490, 18490, 18490, 18497, 18497, 
18497, 18497, 18497, 18497, 18497, 18497, 18504, 18504, 18504, 
18504, 18504, 18504, 18504, 18504, 18511, 18511, 18511, 18511, 
18511, 18511, 18511, 18511, 18518, 18518, 18518, 18518, 18518, 
18518, 18518, 18518, 18525, 18525, 18525, 18525, 18525, 18525, 
18525, 18525, 18532, 18532, 18532, 18532, 18532, 18532, 18532, 
18532, 18539, 18539, 18539, 18539, 18539, 18539, 18539, 18539, 
18546, 18546, 18546, 18546, 18546, 18546, 18546, 18546, 18553, 
18553, 18553, 18553, 18553, 18553, 18553, 18553, 18560, 18560, 
18560, 18560, 18560, 18560, 18560, 18560, 18567, 18567, 18567, 
18567, 18567, 18567, 18567, 18567, 18574, 18574, 18574, 18574, 
18574, 18574, 18574, 18574, 18581, 18581, 18581, 18581, 18581, 
18581, 18581, 18581, 18588, 18588, 18588, 18588, 18588, 18588, 
18588, 18588, 18595, 18595, 18595, 18595, 18595, 18595, 18595, 
18595, 18602, 18602, 18602, 18602, 18602, 18602, 18602, 18602, 
18609, 18609, 18609, 18609, 18609, 18609, 18609, 18609, 18616, 
18616, 18616, 18616, 18616, 18616, 18616, 18616, 18623, 18623, 
18623, 18623, 18623, 18623, 18623, 18623, 18805, 18805, 18805, 
18805, 18805, 18805, 18805, 18805, 18812, 18812, 18812, 18812, 
18812, 18812, 18812, 18812, 18819, 18819, 18819, 18819, 18819, 
18819, 18819, 18819, 18826, 18826, 18826, 18826, 18826, 18826, 
18826, 18826, 18833, 18833, 18833, 18833, 18833, 18833, 18833, 
18833, 18840, 18840, 18840, 18840, 18840, 18840, 18840, 18840, 
18847, 18847, 18847, 18847, 18847, 18847, 18847, 18847, 18854, 
18854, 18854, 18854, 18854, 18854, 18854, 18854, 18861, 18861,
18861, 18861, 18861, 18861, 18861, 18861, 18868, 18868, 18868, 
18868, 18868, 18868, 18868, 18868, 18875, 18875, 18875, 18875, 
18875, 18875, 18875, 18875, 18882, 18882, 18882, 18882, 18882, 
18882, 18882, 18882, 18889, 18889, 18889, 18889, 18889, 18889, 
18889, 18889, 18896, 18896, 18896, 18896, 18896, 18896, 18896, 
18896, 18903, 18903, 18903, 18903, 18903, 18903, 18903, 18903, 
18910, 18910, 18910, 18910, 18910, 18910, 18910, 18910, 18917, 
18917, 18917, 18917, 18917, 18917, 18917, 18917, 18924, 18924, 
18924, 18924, 18924, 18924, 18924, 18924, 18931, 18931, 18931, 
18931, 18931, 18931, 18931, 18931, 18938, 18938, 18938, 18938, 
18938, 18938, 18938, 18938, 18945, 18945, 18945, 18945, 18945, 
18945, 18945, 18945, 18952, 18952, 18952, 18952, 18952, 18952, 
18952, 18952, 18959, 18959, 18959, 18959, 18959, 18959, 18959, 
18959, 18966, 18966, 18966, 18966, 18966, 18966, 18966, 18966, 
18973, 18973, 18973, 18973, 18973, 18973, 18973, 18973, 18980, 
18980, 18980, 18980, 18980, 18980, 18980, 18980, 18987, 18987, 
18987, 18987, 18987, 18987, 18987, 18987), class = "Date"), value_count = c(1223L, 
2670L, 234L, 633L, 2356L, 614L, 1396L, 2726L, 1512L, 3311L, 316L, 
717L, 2920L, 686L, 1865L, 3251L, 1545L, 3164L, 373L, 885L, 2796L, 
731L, 1816L, 3096L, 1851L, 3481L, 304L, 747L, 3017L, 602L, 2134L, 
3345L, 1335L, 2849L, 287L, 667L, 2420L, 658L, 1523L, 2699L, 1484L, 
2916L, 258L, 740L, 2466L, 649L, 1627L, 2721L, 1448L, 2966L, 220L, 
715L, 2593L, 661L, 1601L, 2866L, 1568L, 3127L, 297L, 702L, 2697L, 
742L, 1657L, 3113L, 1470L, 2987L, 303L, 702L, 2631L, 692L, 1642L, 
2874L, 1560L, 3124L, 281L, 751L, 2720L, 721L, 1785L, 3033L, 1566L, 
3046L, 300L, 754L, 2606L, 777L, 1767L, 3034L, 1555L, 3102L, 306L, 
694L, 2637L, 731L, 1694L, 2985L, 1758L, 3441L, 286L, 782L, 3017L, 
731L, 1918L, 3258L, 1520L, 3066L, 248L, 623L, 2702L, 642L, 1719L,
2950L, 1447L, 2947L, 297L, 579L, 2458L, 700L, 1580L, 2839L, 1223L, 
2986L, 253L, 575L, 2594L, 997L, 1371L, 2847L, 1211L, 2814L, 201L, 
568L, 2304L, 685L, 1369L, 2594L, 1250L, 2527L, 189L, 534L, 2118L, 
475L, 1371L, 2370L, 1135L, 2489L, 229L, 637L, 2096L, 645L, 1253L, 
2393L, 1276L, 2793L, 206L, 614L, 2393L, 710L, 1332L, 2626L, 1392L, 
3098L, 197L, 853L, 2557L, 1041L, 1373L, 2804L, 1013L, 2197L, 
159L, 548L, 1893L, 561L, 1049L, 2264L, 1107L, 2437L, 171L, 637L, 
1921L, 527L, 1131L, 2556L, 1104L, 2261L, 196L, 690L, 1888L, 583L, 
998L, 2380L, 1057L, 2127L, 151L, 586L, 1787L, 444L, 1069L, 2183L, 
841L, 1878L, 148L, 423L, 1557L, 345L, 913L, 1771L, 431L, 936L, 
61L, 191L, 764L, 174L, 438L, 926L, 1504L, 4509L, 531L, 1392L, 
3410L, 1455L, 1951L, 4620L, 2691L, 7555L, 783L, 2652L, 5728L, 
2475L, 3062L, 7803L, 2605L, 7468L, 1117L, 2775L, 6166L, 2414L, 
3103L, 8453L, 9079L, 14271L, 1097L, 8780L, 12412L, 8430L, 4066L, 
14868L, 2794L, 7797L, 908L, 2757L, 5996L, 2596L, 3322L, 8229L, 
2995L, 8362L, 881L, 2773L, 6468L, 2708L, 3513L, 8465L, 2705L, 
7339L, 827L, 2555L, 5672L, 2326L, 3179L, 8030L, 2949L, 7893L, 
788L, 2740L, 6007L, 2533L, 3116L, 8552L, 3660L, 8221L, 1033L, 
3254L, 6905L, 2781L, 3607L, 9263L, 2508L, 8395L, 939L, 2417L, 
6133L, 3010L, 3891L, 8188L, 2488L, 7144L, 888L, 2408L, 5604L, 
2456L, 3060L, 7532L, 2241L, 6646L, 785L, 2221L, 5133L, 2244L, 
3046L, 7051L, 2502L, 7347L, 904L, 2906L, 5778L, 2514L, 3215L, 
8034L, 2382L, 6440L, 735L, 2292L, 4984L, 2288L, 2837L, 7206L, 
2279L, 6113L, 601L, 2269L, 4758L, 2086L, 2726L, 6543L, 2137L, 
6519L, 655L, 2314L, 5125L, 2267L, 2961L, 7106L, 2152L, 6474L, 
710L, 2296L, 5158L, 2108L, 2802L, 7085L, 2027L, 5667L, 872L, 
1983L, 4572L, 2134L, 2535L, 6270L, 2078L, 5207L, 611L, 1999L,
4161L, 1810L, 2234L, 6075L, 1888L, 5688L, 639L, 1892L, 4520L, 
1984L, 2630L, 6169L, 2054L, 5880L, 711L, 2154L, 4408L, 2043L, 
2641L, 6054L, 2128L, 5477L, 666L, 2185L, 4328L, 2193L, 2516L, 
5755L, 1934L, 5126L, 686L, 2065L, 4086L, 2119L, 2280L, 5798L, 
1948L, 5488L, 580L, 2091L, 3966L, 1787L, 2291L, 5634L, 1825L, 
5352L, 530L, 1926L, 3756L, 1613L, 2100L, 5872L, 1787L, 5226L, 
509L, 1677L, 3359L, 1575L, 1937L, 5402L, 1229L, 3912L, 324L, 
1086L, 2511L, 1028L, 1505L, 4132L, 673L, 2000L, 148L, 702L, 1538L, 
670L, 856L, 2211L, 2070L, 5369L, 424L, 1839L, 4083L, 1685L, 2312L, 
5559L, 1955L, 5179L, 383L, 1925L, 3963L, 1587L, 2035L, 5388L, 
1730L, 4559L, 434L, 1688L, 4001L, 1607L, 1954L, 5232L, 1419L, 
4405L, 411L, 1630L, 3475L, 1473L, 1797L, 4776L, 1584L, 4642L, 
391L, 1472L, 3522L, 1494L, 1869L, 4920L, 1529L, 4852L, 473L, 
1555L, 3920L, 1583L, 2013L, 5357L, 1527L, 4410L, 454L, 1395L, 
3501L, 1296L, 1897L, 4706L, 1558L, 4509L, 474L, 1355L, 3588L, 
1214L, 1932L, 4670L, 1341L, 4433L, 432L, 1266L, 3660L, 1351L, 
2034L, 4868L, 1457L, 4302L, 345L, 1164L, 3361L, 1118L, 1943L, 
4462L, 1562L, 4553L, 357L, 1394L, 3520L, 1200L, 1898L, 4845L, 
1649L, 4671L, 377L, 1440L, 3718L, 1236L, 2016L, 4909L, 1878L, 
5150L, 544L, 1508L, 3783L, 1362L, 2234L, 5001L, 1861L, 4942L, 
424L, 1331L, 3726L, 1120L, 2400L, 4820L, 1540L, 4352L, 429L, 
1268L, 3230L, 1016L, 1872L, 4242L, 1926L, 5164L, 391L, 1753L, 
4156L, 1199L, 2259L, 4622L, 2000L, 4655L, 402L, 1826L, 3809L, 
1076L, 2356L, 4533L, 1910L, 4004L, 341L, 1816L, 3364L, 908L, 
2147L, 4332L, 1914L, 4483L, 345L, 1794L, 3451L, 997L, 2220L, 
4605L, 1376L, 3966L, 336L, 1361L, 2881L, 1029L, 1712L, 4098L, 
1592L, 3425L, 372L, 1093L, 2721L, 1252L, 1536L, 3564L, 1359L, 
3583L, 407L, 1099L, 2906L, 1121L, 1551L, 3706L, 1454L, 4175L,
482L, 1345L, 3272L, 1381L, 1684L, 4273L, 1517L, 4008L, 535L, 
1571L, 3330L, 1221L, 1640L, 4289L, 1299L, 3473L, 513L, 1290L, 
2587L, 1152L, 1441L, 3513L, 817L, 2278L, 289L, 745L, 1719L, 671L, 
964L, 2242L)), row.names = c(NA, -648L), groups = structure(list(
    created_at = structure(c(18077, 18084, 18091, 18098, 18105, 
    18112, 18119, 18126, 18133, 18140, 18147, 18154, 18161, 18168, 
    18175, 18182, 18189, 18196, 18203, 18210, 18217, 18224, 18231, 
    18238, 18245, 18252, 18259, 18441, 18448, 18455, 18462, 18469, 
    18476, 18483, 18490, 18497, 18504, 18511, 18518, 18525, 18532, 
    18539, 18546, 18553, 18560, 18567, 18574, 18581, 18588, 18595, 
    18602, 18609, 18616, 18623, 18805, 18812, 18819, 18826, 18833, 
    18840, 18847, 18854, 18861, 18868, 18875, 18882, 18889, 18896, 
    18903, 18910, 18917, 18924, 18931, 18938, 18945, 18952, 18959, 
    18966, 18973, 18980, 18987), class = "Date"), .rows = structure(list(
        1:8, 9:16, 17:24, 25:32, 33:40, 41:48, 49:56, 57:64, 
        65:72, 73:80, 81:88, 89:96, 97:104, 105:112, 113:120, 
        121:128, 129:136, 137:144, 145:152, 153:160, 161:168, 
        169:176, 177:184, 185:192, 193:200, 201:208, 209:216, 
        217:224, 225:232, 233:240, 241:248, 249:256, 257:264, 
        265:272, 273:280, 281:288, 289:296, 297:304, 305:312, 
        313:320, 321:328, 329:336, 337:344, 345:352, 353:360, 
        361:368, 369:376, 377:384, 385:392, 393:400, 401:408, 
        409:416, 417:424, 425:432, 433:440, 441:448, 449:456, 
        457:464, 465:472, 473:480, 481:488, 489:496, 497:504, 
        505:512, 513:520, 521:528, 529:536, 537:544, 545:552, 
        553:560, 561:568, 569:576, 577:584, 585:592, 593:600, 
        601:608, 609:616, 617:624, 625:632, 633:640, 641:648), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -81L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

When I try to plot it using geom_point, there's unwanted space as shown in the plot below: enter image description here

I am using the following code to create my plot:

ggplot(data = df_temp, aes(x = created_at))   
  geom_point(aes(y = value_count))   
  labs(title = 'The Plot',
       x = '',
       y = 'Count',
       color = " ")  
  theme_bw()  
  scale_x_date(date_breaks = '2 month', date_labels = "%b/%y", expand = expansion(0,0)) 
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) 
  theme(text=element_text(size=13),panel.spacing.x=unit(0.6, "lines"),
        panel.spacing.y=unit(1, "lines")) 
  scale_linetype_manual(name = NULL, values = 2)

Can someone please guide how to remove that unwanted space?

CodePudding user response:

If p is the result of the ggplot2 command shown then:

p   facet_wrap(vars(format(created_at, "%Y")), scales = "free_x", nrow = 1)

CodePudding user response:

You could use the function enter image description here

Created on 2022-08-14 by the reprex package (v2.0.1)

  • Related