coding for quarterly time series data in ggplot2 examples not found - r

I have a time series variable from 2010Q1 to 2019Q4 converted using ts() function.
I want to do a ggplot2 graph for this one variable (value in the y-axis) to show year and relevant quarters in the x-axis (x-axis labes to look like 2010Q1, 2010Q2,…, etc).
I could not find an appropriate example or code to do this for quarterly data. My try was:
ts_time <- tssl10qpm1[ ,1] # to pick up the time series labels from my ts data set.
g_1 <- ggplot(tssl10qpm1, aes(x=ts_time, y=tssl10qpm1.GDP)) +
geom_line() +
scale_x_date(format = "%b-%Y")
I know this is wrong ( b does not stand for quarters) : my error message:
Error in scale_x_date(format = "%b-%Y") : unused argument (format = "%b-%Y")
When I try other variants, I get other error messages, like something to do with length of aesthetics, either 1 or same as data (which I do not understand at all).
My problem is very simple. I need few lines of code to get a simple graph showing quarterly labels on the x-axis which are associated with my y-values for the given quarters and years.

To get quarterly breaks with labels formatted %b-%Y you would do this...
scale_x_date(date_breaks = "3 months", date_labels = "%b-%Y")

Related

Setting column width in charts in R

I'm very new to R. I want to plot graphs by months with ggplot2, but the last dates of the year variable are intertwined on the x-axis. I have attached the image below. Any ideas on how I can adjust the width on the x-axis? Can I also print each year in the date variable? My dates are between 2010-2020.
enter image description here
Updated version. Op seems to be asking for this. The time variable shows the full date ("year-month-day"). Modifying the x-axis using scale_x_date for showing only calendar years:
# example dataset
dt <- data.table(date=as.Date(seq(1,1000,100),origin = "2010-01-01"),var=rnorm(10))
head(dt)
# display only the YEAR
ggplot(dt,aes(y=var,x=date))+geom_point()+
scale_x_date(date_breaks = "1 year", date_labels = "%Y")
# display 6 months intervals
ggplot(dt,aes(y=var,x=date))+geom_point()+
scale_x_date(date_breaks = "6 months", date_labels = "%b %Y")
Older version: the time variable shows only years.
For showing each single year of the data here are two options.
For increasing the width I guess you mean while saving the plot permanently.
Clarification: if you use R Studio you as it seems from the screenshot, you can change the temporary visualization of the plot in many ways using the GUI.
Clarification #2: check ?facet_wrap to see how you can display the facets in multiple rows and columns, that could also help the specific visualization of your plot.
library(ggplot2)
library(data.table)
# create example dataset (no values for 2015)
dt <- data.table(var=rnorm(40),year=sample(c(seq(2010,2014,1),seq(2016,2020,1)),40,replace = T))
# clearly plot each specific year by considering it as factor (2015 not shown)
ggplot(dt,aes(y=var,x=as.factor(year)))+geom_point()+
xlab("Year") # nicer x-axis naming
# clearly plot each specific year by modifying breaks (shows also empty years if present)
ggplot(dt,aes(y=var,x=year))+geom_point()+
scale_x_continuous(breaks = seq(min(dt[,year]),max(dt[,year]),1))
# save the file with exaggerated width (just an example)
ggsave("myfilepath/myfilename.jpg",width=20,height=4,units = "cm")

How do I modify my xaxis in ggplot to show days label

My data frame consists of one column which as dates in format y/m/d. the other has deaths. The format of the date is posixct. When I plot in R using ggplot the x axis shows days as 1st feb, 15th Feb.. and so on. I want the xaxis with every day as in the data. What do i do?
Thank you
baseplot=ggplot(Data_set,aes(x=Date,y=Cumulative_confirmed_cases,colour="red"))+geom_line(size=1)
baseplot+ scale_x_datetime(date_labels = "%b/%d",limits = c(min,max))+geom_point()
You need to add the date_breaks argument in your scale_x_datetime in order to get all days displayed:
library(ggplot2)
ggplot(Data_set,aes(x=Date,y=Cumulative_confirmed_cases,colour="red"))+
geom_line(size=1) +
scale_x_datetime(date_labels = "%b/%d",limits = c(min,max), date_breaks = "day")+
geom_point()
If this is not working, please provide a reproducible example of your dataset (see: How to make a great R reproducible example)

How to fix ggplot2 time series barchart bars that are too wide?

I am plotting a time series bar chart with a measure for different categories. When I plot the time series bar chart, the width of the bars fills over many dates so that the neighbouring bars touch, even if they are a month apart, but this means that it is unclear which date that bar corresponds to. How do I change the code so that the bars only appear over the date in the underlying dataframe?
I have successfully plotted another time series bar chart with exactly the same ggplot code but different underlying data and so it is unclear to me why this is happening with this particular dataframe.
In this following example, I use a dataframe with only one category for simplicity in highlighting the issue:
data <- data.frame(a = c(as.Date("2019-05-30"), as.Date("2019-06-19")), b = c("FX FORWARD", "FX FORWARD"), c = c(29.2, 74.7))
colnames(data ) <- c("Expiration Date", "Security Type", "Exposure $M")
plot <- ggplot(data , aes(x=`Expiration Date`, y=`Exposure $M`, fill=`Security Type`)) +
geom_bar(stat="identity") + scale_x_date(labels = scales::date_format("%d-%b"), date_breaks = "3 day")
I expected the bars to appear only above the day in which they are stored in the dataframe and not as it is shown in the chart, i.e. $29.2 above 31st May 2019 only and not spreading from 23rd May to 8th June; same for the second data point. Can anyone advise how I may correct this in my code?
Thanks in advance for any help, I've tried looking all over for a solution.

R ggplot: eliminating empty date range from time series plot?

On the bottom image, I have a graph produced by this code:
library(lubridate)
shangPM$date <- with(shangPM, ymd_h(paste(year, month, day, hour, sep= ' ')))
ggplot(data = shangPM, aes(x = date, y = PM_US.Post)) +
geom_line()
However, there is four years shown on my x-axis with no data, making the graph look weird. I tried using xlim and coord_cartesian, but this does not seem to be working well with my date variable (maybe I'm wrong?)
A bit of a noob here - can someone help me zoom in on only the dates I have data for for my plot?
Here is my error:
Error in as.POSIXct.numeric(value) : 'origin' must be supplied

ggplot - error: no layers in plot

I am having a real hard time with ggplot function!
I try to briefly explain my problem.
I have a dataset of several tweets associated to a time stamp; I would like to plot the data obtaining a graph with time on the x bar and the frequency or the "tweet-rate" per hour on the y axis.
What did I do?
library(ggplot2)
c4l.tweets <- read.csv("/Users/vincenzo/Desktop/Collect %23c4l13 Tweets - Archive.csv")
c4l.tweets$time <- as.POSIXct(strptime(c4l.tweets$time, "%d/%m/%Y %H:%M:%S", tz="CST") - 6*60*60)
library(chron)
c4l.tweets$by.hour <- trunc(c4l.tweets$time, units="hours")
ggplot(count(c4l.tweets, "by.hour"), aes(x=by.hour, y=freq))
+ geom_bar(stat="identity") + xlab("Number") + ylab("Date") + labs(title="tweets by hour")
So basically I truncated the data by the timestamp and used the count function to plot them.
I get the
Error: No layers in plot
and
Error in +geom_bar(stat = "identity") : argument not valid for the operator
But why? what am I doing wrong?
I usually have this problem each time i try to plot something via ggplot, what do I do wrong?
Thank you!
Vincenzo

Resources