2 years ago

#72272

test-img

data_science_newbie

ggplot2 axis formatting using n.breaks and labels

While creating a graph with ggplot2, I'm having trouble to format the x-axis which are dates that show as numerical values. I want to use 'n.breaks' in 'scale_x_continuous'. From the documentation, I see that the 'labels' argument can be "A function that takes the breaks as input and returns labels as output.". How do I access those breaks and pass them in a formatting function to get my labels? Here's a sample of my code:

ggplot(data = aggregatedCovidData) +
   geom_line(aes(x = date, y = newCases/1000)) +
   labs(title = "COVID-19 daily new cases (000's) timeline",
        x = NULL,
        y = NULL) +
   scale_x_continuous(
      n.breaks = 9
      # ,labels = as_date()
   )

If you need more than a sample of code, I've uploaded my data to dropbox as well as my code.

Thanks in advance!

EDIT:

As requested by chemdork123, I've added a sample of my data here. I'll keep the sample code as it is, because the rest of it is not relevant to the problem.

structure(list(date = structure(c(18262, 18263, 18264, 18265, 
18266, 18267, 18268, 18269, 18270, 18271, 18272, 18273, 18274, 
18275, 18276, 18277, 18278, 18279, 18280, 18281, 18282, 18283, 
18284, 18285, 18286, 18287, 18288, 18289, 18290, 18291, 18292, 
18293, 18294, 18295, 18296, 18297, 18298, 18299, 18300, 18301, 
18302, 18303, 18304, 18305, 18306, 18307, 18308, 18309, 18310, 
18311, 18312, 18313, 18314, 18315, 18316, 18317, 18318, 18319, 
18320, 18321, 18322, 18323, 18324, 18325, 18326, 18327, 18328, 
18329, 18330, 18331, 18332, 18333, 18334, 18335, 18336, 18337, 
18338, 18339, 18340, 18341, 18342, 18343, 18344, 18345, 18346, 
18347, 18348, 18349, 18350, 18351, 18352, 18353, 18354, 18355, 
18356, 18357, 18358, 18359, 18360, 18361), class = "Date"), newCases = 
c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
392, 1146, 1973, 2736, 3237, 10608, 2358, 8272, 6771, 8450, 18998, 
12402, 16045, 14980, 12648, 14256, 10929, 12120, 10290, 8170, 
1592, 60608, 26026, 8439, 8592, 7942, 7352, 1842, 2214, 2533, 
7086, 1499, 2346, 3519, 4038, 5686, 5836, 7924, 10219, 8410, 
11006, 10077, 12584, 17213, 18388, 18116, 19643, 22059, 35284, 
26473, 67557, 51791, 52483, 69351, 73988, 90618, 125198, 141006, 
147124, 157929, 192913, 184684, 230876, 282471, 290149, 310396, 
253111, 287410, 337570, 361260, 359891, 367883, 241710, 300750, 
315166, 293112, 359142, 374295)), row.names = c(NA, -100L), class = 
c("tbl_df", "tbl", "data.frame"))

ggplot2

scale

graph-visualization

0 Answers

Your Answer

Accepted video resources