Reputation: 821
I am using the following code to plot a DateTime column as a histogram for each day.
Even after setting the X-axis breaks to 1 day, I am getting 1 Month probably the default option. How can I plot the X-axis with "Year-Month-Day" for each day?
Also if possible I want to display the count on top of each bar.
code
data <- read.table("input.csv", sep=",", head=T)
data$datetime <- as.Date(data$datetime)
ggplot(data, aes(x=datetime)) +
geom_histogram(binwidth=0.5, colour="black", fill="white") +
scale_x_date(date_minor_breaks = "1 day")
data
index,datetime,values,type
55,2019-11-02 12:00:00,2.2571265131358302,x
56,2019-11-02 13:00:00,2.4175854658358227,x
57,2019-11-02 14:00:00,2.7247976096720685,x
58,2019-11-02 15:00:00,3.0477882795726146,x
59,2019-11-02 16:00:00,3.2006008421516996,x
60,2019-11-02 17:00:00,3.2116870146833625,x
61,2019-11-02 18:00:00,3.4693910132468457,x
62,2019-11-02 19:00:00,3.911869748466746,x
63,2019-11-02 20:00:00,4.00069618775975,x
64,2019-11-02 22:00:00,4.0834105399449285,x
65,2019-11-02 23:00:00,4.160321416499418,x
66,2019-11-03 00:00:00,4.098125637510356,x
67,2019-11-03 01:00:00,4.021221769171646,x
68,2019-11-03 02:00:00,4.017759023510684,x
69,2019-11-03 03:00:00,4.013082714411543,x
70,2019-11-03 04:00:00,4.057993925343051,x
71,2019-11-03 05:00:00,4.194095193140784,x
72,2019-11-03 07:00:00,4.294259567020734,x
73,2019-11-03 08:00:00,4.337746909001244,x
74,2019-11-03 09:00:00,4.384365666444803,x
75,2019-11-03 10:00:00,4.448100962130655,x
76,2019-11-03 11:00:00,4.501990074404225,x
77,2019-11-03 13:00:00,4.52830137381731,x
78,2019-11-03 14:00:00,4.647279146289785,x
79,2019-11-03 15:00:00,4.776722797787091,x
80,2019-11-03 19:00:00,4.696071186160853,x
81,2019-11-03 21:00:00,4.57669998620599,x
82,2019-11-03 22:00:00,4.462372935827071,x
83,2019-11-03 23:00:00,4.29887239487861,x
84,2019-11-04 00:00:00,4.138775537444896,x
85,2019-11-04 01:00:00,4.010458288624996,x
86,2019-11-04 02:00:00,3.8001917855220837,x
87,2019-11-04 03:00:00,3.5833796414675563,x
88,2019-11-04 04:00:00,3.4035716671606404,x
89,2019-11-04 05:00:00,3.272242285475774,x
90,2019-11-04 06:00:00,3.103512688095525,x
91,2019-11-04 08:00:00,3.1125352688100882,x
92,2019-11-04 09:00:00,3.220763083848201,x
93,2019-11-04 10:00:00,3.290134157323074,x
94,2019-11-04 11:00:00,3.415488671370309,x
95,2019-11-04 12:00:00,3.5455879768850576,x
96,2019-11-04 13:00:00,3.5474491297923727,x
97,2019-11-04 14:00:00,3.783816611089137,x
98,2019-11-04 15:00:00,4.184648621004391,x
99,2019-11-04 16:00:00,4.2413488810807864,x
100,2019-11-04 18:00:00,4.179690857096466,x
101,2019-11-05 08:00:00,4.186025943693791,x
102,2019-11-05 09:00:00,4.028004946673923,x
103,2019-11-05 12:00:00,3.996324873094708,x
104,2019-11-05 14:00:00,4.170572049548053,x
105,2019-11-05 15:00:00,4.324133816110184,x
106,2019-11-05 16:00:00,4.38233647529367,x
107,2019-11-05 17:00:00,4.375290157364679,x
108,2019-11-05 18:00:00,4.360382781128352,x
109,2019-11-05 19:00:00,4.264913977361403,x
110,2019-11-05 20:00:00,4.1399117448002025,x
111,2019-11-05 21:00:00,4.181169914887134,x
112,2019-11-05 22:00:00,4.251114951814682,x
113,2019-11-06 00:00:00,4.235355868286902,x
114,2019-11-06 01:00:00,4.246615325280132,x
115,2019-11-06 02:00:00,4.2108788354127595,x
116,2019-11-06 03:00:00,4.150994459861172,x
117,2019-11-06 04:00:00,4.053771508278776,x
118,2019-11-06 05:00:00,3.9656677100404196,x
119,2019-11-06 06:00:00,3.891497364440651,x
120,2019-11-06 07:00:00,3.9250961484429268,x
121,2019-11-06 09:00:00,3.9564409947149786,x
122,2019-11-06 10:00:00,3.934135576221536,x
123,2019-11-06 11:00:00,3.811665254552286,x
124,2019-11-06 13:00:00,3.698395441791204,x
125,2019-11-06 14:00:00,3.6541064673462826,x
126,2019-11-06 15:00:00,3.594042699299084,x
127,2019-11-06 16:00:00,3.450994105235735,x
128,2019-11-06 17:00:00,3.3646913567819343,x
129,2019-11-06 18:00:00,3.2999220715176176,x
130,2019-11-06 19:00:00,3.258550773145985,x
131,2019-11-06 21:00:00,3.2225326106631136,x
132,2019-11-06 22:00:00,3.185935446208913,x
133,2019-11-06 23:00:00,3.1472600656989873,x
134,2019-11-07 00:00:00,3.096357252528618,x
135,2019-11-07 01:00:00,3.0201448403174727,x
136,2019-11-07 02:00:00,3.019167468392874,x
137,2019-11-07 03:00:00,3.065834385577178,x
138,2019-11-07 05:00:00,3.111118977817557,x
139,2019-11-07 06:00:00,2.997443808096788,x
140,2019-11-07 07:00:00,2.9863646062096496,x
141,2019-11-07 08:00:00,2.9854114565645036,x
142,2019-11-07 09:00:00,2.976881175704244,x
143,2019-11-07 10:00:00,2.9485472925204563,x
144,2019-11-07 11:00:00,2.9052588427934265,x
145,2019-11-07 12:00:00,2.8723489707552354,x
146,2019-11-07 13:00:00,2.787288523493735,x
147,2019-11-07 14:00:00,2.699838153360574,x
148,2019-11-07 15:00:00,2.7790335378861903,x
149,2019-11-07 16:00:00,2.7701168032009678,x
150,2019-11-07 17:00:00,2.7599015900121775,x
151,2019-11-07 18:00:00,2.734276786826625,x
152,2019-11-07 19:00:00,2.6166425495004892,x
153,2019-11-07 20:00:00,2.480661023265874,x
154,2019-11-08 01:00:00,2.3245284179813024,x
155,2019-11-08 02:00:00,2.1160992644464174,x
156,2019-11-08 03:00:00,2.0041605300101835,x
157,2019-11-08 04:00:00,1.8515859873500127,x
598,2019-12-29 22:00:00,1.6110582697766904,x
599,2019-12-29 23:00:00,1.6596777339084172,x
600,2019-12-30 00:00:00,1.6872956896946691,x
601,2019-12-30 01:00:00,1.681427364032406,x
602,2019-12-30 02:00:00,1.6646005456192854,x
603,2019-12-30 03:00:00,1.61024577041883,x
787,2020-01-12 20:00:00,1.432215548619208,x
788,2020-01-12 21:00:00,1.482798113602255,x
789,2020-01-12 22:00:00,1.5180138321075316,x
790,2020-01-12 23:00:00,1.5862635616643168,x
791,2020-01-13 00:00:00,1.624179322341154,x
792,2020-01-13 01:00:00,1.5897191079933364,x
793,2020-01-13 02:00:00,1.5686478770443524,x
794,2020-01-13 03:00:00,1.5158600949449366,x
795,2020-01-13 04:00:00,1.4760764670728665,x
796,2020-01-13 05:00:00,1.4869610695066484,x
797,2020-01-13 07:00:00,1.5493409766366932,x
798,2020-01-13 08:00:00,1.6101944516836666,x
799,2020-01-13 09:00:00,1.6237659716487467,x
800,2020-01-13 10:00:00,1.6041134677825444,x
801,2020-01-13 11:00:00,1.5903269214934665,x
802,2020-01-13 13:00:00,1.536524696499736,x
803,2020-01-13 14:00:00,1.494193483950321,x
804,2020-01-13 15:00:00,1.4816960437129962,x
805,2020-01-13 16:00:00,1.5132917235386587,x
806,2020-01-13 18:00:00,1.6435825530166581,x
807,2020-01-13 19:00:00,1.8375058361044376,x
808,2020-01-13 20:00:00,1.9585768838462818,x
809,2020-01-13 21:00:00,1.9788951029564366,x
810,2020-01-13 22:00:00,1.9860829813291714,x
811,2020-01-13 23:00:00,1.9216810837736404,x
812,2020-01-14 00:00:00,1.86184574914949,x
Upvotes: 0
Views: 1535
Reputation: 13833
Your original code was working as intended, which is to say that scale_x_date(date_minor_breaks = '1 day')
was setting the minor breaks in your x axis to be separated by day. Minor breaks are not labeled, whereas Major breaks are labeled. So you had an axis that was separated by month (major breaks) and then you had all the minor axis tick marks for each day.
To label by each day, you need to set date_breaks="1 day"
within scale_x_date()
, and then specify the format of the label to be YYYY-MM-DD via specifying date_labels=
.
If we do this with you code you get the following icky mess:
ggplot(data, aes(x=datetime)) +
geom_histogram(binwidth=0.5, colour="black", fill="white") +
scale_x_date(date_breaks = "1 day", date_labels = '%Y-%m-%d')
If you want to label every day, you need to make those labels fit. In which case, you can do that by formatting the theme for the axis text to be rotated. vjust=0.5
is necessary if you do that, since the default does not align your text in the middle vertically.
For adding counts above each histogram bar, this has been answered already here. Your case is a bit different though, because you have a lot of values that would read "0". It would be much better to show only those labels where the count was > 0. For this, I'm just building into the aes(label=
call an ifelse()
function to represent the label as blank when it is equal to 0. This doesn't mean the label is not there... it just means it contains "NA".
You will also notice the y
aesthetic for stat_bin
is set to be ..count..+1
. This ensures that each value is drawn just above each bar, rather than right on the edge of the bar. You could also mess with vjust
to get this to work right.
ggplot(data, aes(x=datetime)) +
geom_histogram(binwidth=0.5, colour="black", fill="white") +
stat_bin(aes(y=..count..+1,
label=ifelse(..count..!=0, ..count.., NA)), geom='text', binwidth = 0.5) +
scale_x_date(date_breaks = "1 day", date_labels = '%Y-%m-%d') +
theme(axis.text.x = element_text(angle=90, size=7, vjust=0.5))
I personally think the plot could look a bit better, and I think it's still hard to read with the label every day. So here's my suggestion to snazz it up a bit. In the code below, I switch to the theme_bw()
(preference), then format your major axis ticks to be every week, with minor ticks every 1 day. I also prefer to have bar graphs (like histograms) to have the bottom of the bar meet the x axis, so use scale_y_continuous(expand=
) to do that. Finally, I formatted the axis label to include a line break and removed the rotation. Not sure if it fits your preference, but it also shows you a bit more of what you can do when combining date_breaks=
and date_minor_breaks=
in the same plot.
ggplot(data, aes(x=datetime)) + theme_bw() +
geom_histogram(binwidth=0.5, colour="black", fill="white") +
stat_bin(aes(y=..count..+1,
label=ifelse(..count..!=0, ..count.., NA)), geom='text', binwidth = 0.5) +
scale_x_date(date_breaks = "1 week", date_minor_breaks = '1 day', date_labels = '%Y\n%m-%d') +
scale_y_continuous(expand=expansion(mult=c(0,0.05)))
Upvotes: 5