I'm plotting a dataset a little over three months long with a data each half hour.
So in my dataframe I have a datetime vector with the format %Y-%m-%d %H:%M:%OS
- year, month, day, hour, minute, second.
I want just to set the ticks on the x-axis each fiveteen days.
I tried with scale_x_date
and scale_x_datetime
setting the breaks
, but without result and an error message.
I didn't understand well the difference between scale_x_date
, scale_x_datetime
, but I think that in my case, with a datetime vector I should use scale_x_datetime
.
It's very diffcult share my dataset, because is very large and using a smaller portion I thing wouldn't give enough material to figure out my problem.
So I share the code that I used and the graphical output that I have.
library(ggplot2)
ggplot(fluxmxrdesp,aes(x=Datetime))+
geom_line(aes(y=mz31_mxr,colour="Formaldehyde"), size = 1)+
labs(y = "VMR (ppb)" , x = "Date",colour="Legend")+
scale_colour_manual(name='',values = ("Formaldehyde"="darkolivegreen4"))+
theme_classic()+theme(legend.position = "top") +
theme(axis.title.y = element_text(margin = margin(r=25),color="black", size=18),
axis.text.y = element_text(color="black",size=14),
axis.title.x = element_text(margin = margin(t=10),color="black", size=18),
axis.text.x = element_text(color="black", size=14),
legend.text = element_text(color = "black", size = 18))
Short dataset
structure(list(Datetime = structure(c(1527813000, 1527814800,
1527816600, 1527818400, 1527820200, 1527822000), class = c("POSIXct",
"POSIXt"), tzone = "%Y-%m-%d %H:%M:%OS"), H = c(-6.088316625,
-9.882886968, -6.533935906, -5.305478189, -3.844176459, -12.90132526
), LE = c(0.946172041403071, -1.10377384331459, -1.53126820765533,
-2.28713506105331, 1.59264111550727, -1.08866725213062), Turbulence = c(0.0838,
0.128583643, 0.0806, 0.0875, 0.0733, 0.168911167), Wind_dir = c(255.3307133,
277.4016533, 281.2972942, 274.410293, 241.1132665, 256.6879056
), mz31_flux = c(0.00754, 0.0393, -0.00867, -0.0075, -0.00622,
0.0347), mz33_flux = c(-0.0898, 0.162, -0.0944, 0.0466, 0.0614,
-0.271), mz39_flux = c(0.131, -0.245, -0.157, 0.153, 0.147, 0.322
), mz42_flux = c(-0.00644, 0.00684, 0.0103, 0.0135, -0.0039,
0.017), mz45_flux = c(0.0245, 0.107, 0.031, 0.025, -0.0294, -0.0719
), mz47_flux = c(-0.031, -0.061, -0.0345, 0.0482, -0.0244, 0.0803
), mz59_flux = c(0.0309, 0.0639, 0.0345, 0.0424, 0.0231, -0.0926
), mz61_flux = c(-0.013, -0.0187, -0.0155, 0.0382, 0.00854, 0.053
), mz69_flux = c(0.0208, 0.0213, -0.00855, 0.011, -0.0112, -0.0213
), mz71_flux = c(0.00807, -0.00698, 0.00235, 0.00435, -0.00648,
0.0109), mz75_flux = c(-1.83e-17, -0.00405, -0.000647, 0.000867,
-0.000523, 0.0108), mz79_flux = c(-0.00999, 0.03, 0.00945, 0.00893,
0.0136, -0.028), mz85_flux = c(0.00681, -0.0219, 0.00518, -0.0091,
-0.00905, -0.0183), mz87_flux = c(0.00908, 0.0112, 0.00519, 0.00973,
0.00647, -0.0135), mz93_flux = c(-0.0299, 0.0532, 0.0417, 0.0203,
-0.0189, 0.0637), mz99_flux = c(-0.00727, 0.00771, 0.00569, -0.0047,
-0.00118, -0.00661), mz101_flux = c(0.00489, 0.0114, 0.00326,
-0.00676, 0.0051, 0.0165), mz107_flux = c(-0.0279, -0.0268, 0.0211,
0.0167, -0.0084, 0.0372), mz111_flux = c(-0.00163, 0.00664, -0.00186,
0.00456, 0.00155, 0.00682), mz113_flux = c(-0.0013, 0.0056, 0.000907,
0.00154, -0.00125, 0.00679), mz135_flux = c(-0.00619, -0.00983,
0.00429, 0.00534, 0.00503, 0.0231), mz137_flux = c(0.0233, 0.0346,
0.0171, -0.000554, 0.0282, -0.0985), mz149_flux = c(-0.00176,
-0.00336, 0.000902, -0.000605, -0.000848, 0.00901), mz155_flux = c(0.00116,
-0.00137, 0.000528, 0.000541, -5.79e-05, -7.08e-05), mz31_mxr = c(7.27e-06,
0.000172, 7.29e-06, 7.69e-06, 6.69e-06, 0.000154), mz33_mxr = c(0.0042,
0.00439, 0.00349, 0.00321, 0.00334, 0.00323), mz39_mxr = c(0.0038,
0.0038, 0.00364, 0.00352, 0.0033, 0.00323), mz42_mxr = c(0.000102,
0.000135, 7.58e-05, 0.000112, 9.04e-05, 0.000143), mz45_mxr = c(0.00128,
0.0012, 0.00107, 0.001, 0.00101, 0.00099), mz47_mxr = c(0.000273,
0.000476, 0.000236, 0.000398, 0.000264, 0.000476), mz59_mxr = c(0.00205,
0.00194, 0.00193, 0.0019, 0.00182, 0.00172), mz61_mxr = c(3.78e-05,
0.000182, 0.000124, 0.000261, 0.000156, 0.000241), mz69_mxr = c(0.000299,
0.000313, 0.000291, 0.000255, 0.00026, 0.000252), mz71_mxr = c(7.59e-05,
6.53e-05, 6.75e-05, 6.59e-05, 6.21e-05, 6.33e-05), mz75_mxr = c(2.21e-21,
1.42e-05, 1.89e-07, 3.56e-07, 3.07e-07, 2.16e-05), mz79_mxr = c(0.000346,
0.000406, 0.000382, 0.00031, 0.000383, 0.000293), mz85_mxr = c(0.000151,
0.00014, 0.000143, 0.000133, 0.000123, 0.000111), mz87_mxr = c(9.9e-05,
9.82e-05, 9.68e-05, 8.91e-05, 8.39e-05, 8.74e-05), mz93_mxr = c(0.00228,
0.00268, 0.00252, 0.00208, 0.0024, 0.00189), mz99_mxr = c(3.69e-05,
6.19e-05, 3.81e-05, 5.54e-05, 1.45e-05, 4.66e-05), mz101_mxr = c(0.000155,
0.000155, 0.000157, 0.000147, 0.000137, 0.000139), mz107_mxr = c(0.00129,
0.00152, 0.00142, 0.00115, 0.00133, 0.00104), mz111_mxr = c(1.09e-05,
5.27e-05, 9.35e-06, 5.86e-06, 6.03e-06, 3.74e-05), mz113_mxr = c(2.15e-05,
3.78e-05, 2.07e-05, 2e-05, 1.89e-05, 3.51e-05), mz135_mxr = c(0.000153,
0.000141, 0.000115, 9.73e-05, 9.94e-05, 8.94e-05), mz137_mxr = c(0.000142,
0.000122, 4.16e-05, 1.78e-06, 7.94e-05, 0.000197), mz149_mxr = c(1.58e-06,
2.22e-05, 1.13e-06, 6.74e-07, 9.28e-07, 1.84e-05), mz155_mxr = c(1.57e-06,
4.71e-06, 3.58e-07, 4.42e-07, 4.64e-08, 4.9e-08)), row.names = c(NA,
6L), class = "data.frame")
As you can see in the figure (here) the tick, as text, for each month.
I'd like to have something like: 01/06/2018 00:00, 15/06/2018 00:00, 1/07/2018 00:00
and so on.
Also with the possibility of reducing the window between one tick and another
Since you want to specify the dates to just the 1st and 15th, you have to manually create the vector of breaks and then use this vector inside the scale_x_datetime function.
In this case I manually created the date range from June 1 to October 15.
#create breaks for the 1st and 15th
breaks1st<-seq.Date(as.Date("2018-06-01"), as.Date("2018-10-01"), by="month")
breaks15th<-seq.Date(as.Date("2018-06-15"), as.Date("2018-10-15"), by="month")
mybreaks<-sort(c(breaks1st, breaks15th))
mybreaks<-as.POSIXct(mybreaks, tz="GMT")
ggplot(fluxmxrdesp, aes(x=Datetime))+
geom_line(aes(y=mz31_mxr,colour="Formaldehyde"), size = 1)+
labs(y = "VMR (ppb)" , x = "Date",colour="Legend")+
scale_colour_manual(name='',values = ("Formaldehyde"="darkolivegreen4"))+
theme_classic()+theme(legend.position = "top") +
theme(axis.title.y = element_text(margin = margin(r=25),color="black", size=18),
axis.text.y = element_text(color="black",size=14),
axis.title.x = element_text(margin = margin(t=10),color="black", size=18),
axis.text.x = element_text(color="black", size=14, angle=30, hjust = 1)),
legend.text = element_text(color = "black", size = 18)) +
scale_x_datetime(breaks=mybreaks, date_labels="%m/%d/%Y %H:%M")
In order to have the labels fit, I needed to angle the label text.
I would recommend not adding the "%H:%M" to the labels as the time component does not provide any additional significance and adds a considerable amount of clutter to the axis.