I have been trying to convert POSIXct format so that my date and times would reflect Julian dates.
ind$DateAndTime <- as.POSIXct(ind$DateAndTime, tz = "UTC",
origin = '1970-01-01')
ind$DateAndTime<- format(as.POSIXct(ind_steps$t2),"%y%j")
I had used these two lines of code to do so, but I am now having trouble plotting them using ggplot.
plot_list[[i]] <- ggplot(ind, aes(x = DateAndTime, y = NSD)) +
geom_line() + theme_bw() +
ggtitle(random_tables[i]) +
theme(axis.text.x = element_text(angle = 90))
When I plot it I get this, where the julian dates are vertical, but they still overlap. I would like to get the graph to show the julian dates more visibly and to show every other julian date so that it isn't so cramped in the x-axis. Is there a way to do this?
Here is the completed code. Without any sample data, it is difficult to provide an exact example.
From your previous question, your issue maybe related to attempting to pass a datetime object to a function expecting a date object. In this case I used as.Date() and scale_x_date(), in your case you may want to use as.POSIXct() and scale_x_datetime()
#create dummy data
DateAndTime = 18000:18300
NSD = DateAndTime/10000
ind <-data.frame(DateAndTime, NSD)
#convert the DateAndTime column into a date object
ind$DateAndTime <- as.Date(ind$DateAndTime, tz = "UTC",
origin = '1970-01-01')
#Plot table and format x-axis
ggplot(ind, aes(x = DateAndTime, y = NSD)) +
geom_line() + theme_bw() +
ggtitle("Demo Title") +
scale_x_date(date_breaks = "1 month", date_labels = "%y-%j")
theme(axis.text.x = element_text(angle = 90))
Related
I am trying to plot my weekly time series ranging from 2010-02-05 to 2012-08-03 after applying ts function to my data. The problem is that my x-axis only shows the years 2010, 2011 and 2012, however I would like to have the corresponding date for each observation of my time series displayed on the x-axis.
This is the code I used:
Store_1_Sales_ts <- ts(Store_1_Sales$Weekly_Sales,start = c(2010,5),end = c(2012,31),frequency = 52, deltat = 1/52)
autoplot(Store_1_Sales_ts) + labs(y= "Sales", x="Date") + ggtitle('Store 1 Weekly Sales additive decomposition') +
geom_line(color="royalblue", size = 1) + theme(plot.title=element_text(hjust=0.5)) + theme_minimal() + geom_point(color="royalblue") +
scale_y_continuous(limits = c(1250000, 2500000),labels = function(x) format(x, scientific = FALSE))
And it returns the following graph:
I tried using another code for specifying the x-axis dates, but it returns the following error:
"Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.
Error: Invalid input: date_trans works with objects of class Date only"
The code I used is the following:
autoplot(Store_1_Sales_ts) + labs(y= "Sales", x="Date") + ggtitle('Store 1 Weekly Sales additive decomposition') +
geom_line(color="royalblue", size = 1) + theme(plot.title=element_text(hjust=0.5)) + theme_minimal() + geom_point(color="royalblue") +
scale_x_date(breaks = scales::breaks_width("1 week", offset = 4),limits = as.Date(c('2010-02-05', '2012-08-03'), format = '%Y-%m-%d'), date_labels = "%d %b %y") +
scale_y_continuous(limits = c(1250000, 2500000),labels = function(x) format(x, scientific = FALSE))
I want to have a graph similar to the one below, which I got by using the last code before transforming my data into time series:
I can't use the last graph since I need to perform a forecast later and it's necessary to transform the data into time series.
PS:
The data I used concerns the weekly sales of Walmart during the period I specified above, and can be found in the following link: https://www.kaggle.com/datasets/rutuspatel/walmart-dataset-retail
Trying to plot the following data frame (call it bob):
1
Since the original date is in d/m/y, I use Finaldate and Value to graph.
Here is the code used to graph:
ggplot(Bob, aes(Finaldate, Value)) +geom_line() + geom_point(size = 3) +
labs(title = "TITLE",subtitle = "SUBTITLE", y = "Y", x = "X") +
theme_fivethirtyeight()+scale_y_continuous(name="name", labels = scales::comma)+theme(legend.title = element_blank())+scale_x_discrete(guide = guide_axis(check.overlap = TRUE))
While I do get an output, it is not as a time series but rather the dates are not in order and the plot makes no sense. Attached a copy of the plot as well.
enter image description here
Not sure how to fix this problem, and have tried a couple of different things
Have you tried using
+ scale_x_date(date_labels = "%d %m %Y") (ggplot2)
https://r-graph-gallery.com/279-plotting-time-series-with-ggplot2.html
You need to convert Finaldate to a date -- it is being treated as a character so all the dates are in "alphabetical" order. Try:
Bob$finalDate <- as.Date(Bob$finalDate, format = "%m/%d/%Y")
I have a data frame with a column of dates. I am trying to get a frequency count of each date. I was thinking that a histogram would visualize the data nicely, but maybe there is a better way? I was able to created a histogram of the data but it is not exactly what I was looking for. I was hoping to get each individual date on the x-axis and the frequency count on the y-axis.
I have done some programming in R but I have not done much visualizations in R. Any help would be greatly appreciated.
RawDates<- c("11/8/2017","12/6/2017","10/6/2017","12/6/2017","1/24/2018","9/5/2017","1/24/2018","2/21/2018","10/12/2017","1/22/2018","5/2/2018","1/24/2018","10/12/2017","1/22/2018","2/21/2018","5/2/2018","3/12/2018","5/3/2018","11/7/2017","12/5/2017","9/8/2017","10/6/2017","10/5/2017","11/3/2017","12/6/2017","2/21/2018","11/2/2017","12/5/2017","5/2/2018","1/24/2018","9/6/2017","11/2/2017","2/21/2018","5/2/2018","1/24/2018","11/8/2017","3/12/2018","5/3/2018","1/24/2018")
FormattedDates <- as.Date(RawDates, format = "%m/%d/%Y")
df <- data.frame(FormattedDates)
##This is whatI have already tried
hist(df$FormattedDates, "days", format = "%m/%d/%Y")
Here a simple ggplot2 solution:
library(ggplot2)
library(scales)
ggplot(df) +
geom_histogram(aes(x = FormattedDates)) +
scale_x_date(labels = date_format("%m %d %Y"), date_breaks = "30 days") +
theme(legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1))
I want to set bounds for the x-axis for a plot of time-series data which features only time (no dates). My limits are:
lims <- strptime(c("03:00","16:00"), format = "%H:%M")
And my ggplot prints fine, but when I add this to scale_x_datetime
scale_x_datetime(limits = lims)
I get Error: Invalid input: time_trans works with objects of class POSIXct only
Fully reproducible example courtesy of How to create a time scatterplot with R?
dates <- as.POSIXct(as.Date("2011/01/01") + sample(0:365, 100, replace=TRUE))
times <- as.POSIXct(runif(100, 0, 24*60*60), origin="2011/01/01")
df <- data.frame(
dates = dates,
times = times
)
lims <- strptime(c("04:00","16:00"), format = "%H:%M")
library(scales)
library(ggplot2)
ggplot(df, aes(x=dates, y=times)) +
geom_point() +
scale_y_datetime(limits = lims, breaks=date_breaks("4 hour"), labels=date_format("%H:%M")) +
theme(axis.text.x=element_text(angle=90))
the error message says that you should use as.POSIXct on lims.
You also need to add the date (year, month and day) in lims, because by default it will be `2015, which is off limits.
lims <- as.POSIXct(strptime(c("2011-01-01 03:00","2011-01-01 16:00"), format = "%Y-%m-%d %H:%M"))
ggplot(df, aes(x=dates, y=times)) +
geom_point() +
scale_y_datetime(limits =lims, breaks=date_breaks("4 hour"), labels=date_format("%H:%M"))+
theme(axis.text.x=element_text(angle=90))
I've been trying to add appropriate dates on the x-axis of my graph, but can't figure out how to do it in a sane way. What I want is pretty simple: a date at every January 1st in between the minimum and maximum of my data set.
I don't want to include the month - just '2008' or '2009' or whatever is fine. A great example would be this graph:
example graph
Except I want the date on every year, rather than every other year.
I can't seem to figure this out. My dates are defined as days since 1/1/1970, and I've included a method dateEPOCH_formatter which converts the epoch format to a format using the chron package. I've figured out how to make a tick mark and date at the origin of the graph and every 365 days thereafter, but that's not quite the same thing.
Another minor problem is that, mysteriously, the line chron(floor(y), out.format="mon year",origin.=epoch) outputs a graph with axis markers like 'Mar 2008', but changing the line to chron(floor(y), out.format="year",origin.=epoch) doesn't give me a result like '2008' - it just results in the error:
Error in parse.format(format[1]) : unrecognized format year
Calls: print ... as.character.times -> format -> format.dates -> parse.format
Execution halted
Here's my code - thanks for the help.
library(ggplot2)
library(chron)
argv <- commandArgs(trailingOnly = TRUE)
mydata = read.csv(argv[1])
png(argv[2], height=300, width=470)
timeHMS_formatter <- function(x) { # Takes time in seconds from midnight, converts to HH:MM:SS
h <- floor(x/3600)
m <- floor(x %% 60)
s <- round(60*(x %% 1)) # Round to nearest second
lab <- sprintf('%02d:%02d', h, m, s) # Format the strings as HH:MM:SS
lab <- gsub('^00:', '', lab) # Remove leading 00: if present
lab <- gsub('^0', '', lab) # Remove leading 0 if present
}
dateEPOCH_formatter <- function (y){
epoch <- c(month=1,day=1,year=1970)
chron(floor(y), out.format="mon year",origin.=epoch)
}
p= ggplot() +
coord_cartesian(xlim=c(min(mydata$day),max(mydata$day)), ylim=c(0,86400)) + # displays data from first email through present
scale_color_hue() +
xlab("Date") +
ylab("Time of Day") +
scale_y_continuous(label=timeHMS_formatter, breaks=seq(0, 86400, 14400)) + # adds tick marks every 4 hours
scale_x_continuous(label=dateEPOCH_formatter, breaks=seq(min(mydata$day), max(mydata$day), 365) ) +
ggtitle("Email Sending Times") + # adds graph title
theme( legend.position = "none", axis.title.x = element_text(vjust=-0.3)) +
theme_bw() +
layer(
data=mydata,
mapping=aes(x=mydata$day, y=mydata$seconds),
stat="identity",
stat_params=list(),
geom="point",
geom_params=list(alpha=5/8, size=2, color="#A9203E"),
position=position_identity(),
)
print(p)
dev.off()
I think it will be much easier to use the built in function scale_x_date with date_format and date_breaks from the scales package. These should work with most date classes in R, such as Date, chron etc
for example
library(ggplot2)
library(chron)
library(scales)
# some example data
days <- seq(as.Date('01-01-2000', format = '%d-%m-%Y'),
as.Date('01-01-2010', format = '%d-%m-%Y'), by = 1)
days_chron <- as.chron(days)
mydata <- data.frame(day = days_chron, y = rnorm(length(days)))
# the plot
ggplot(mydata, aes(x=days, y= y)) + geom_point() +
scale_x_date(breaks = date_breaks('year'), labels = date_format('%Y'))
To show how intuitive and easy these function are, if you wanted Montth-year labels every 6 months - note that this requires a very wide plot or very small axis labels
ggplot(mydata, aes(x=days, y= y)) + geom_point() +
scale_x_date(breaks = date_breaks('6 months'), labels = date_format('%b-%Y'))