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

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)

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 to increase the x axis resolution using ggplot in R (from yearly to monthly)

#This is my code, I need help in improving the x axis resolution that is monthly (an 2017, Feb 2017.....Dec 2020)
ggplot()+
geom_line(data=IDA_DATA,
aes(y=final1,x= Date,colour="darkblue"),size=1 )+
geom_line(data=IDA_DATA,
aes(y=fpmc2,x= Date,colour="red"),
size=1) +
scale_color_discrete(name = "Y series", labels = c("Adjusted Trend", "Long Term Comp"))+
theme(legend.position = c(.85, .85))+
labs(y="PM10 Conc (ug/m3)")
I am assuming your Date variable is an actual date type (ie. numeric).
If your data is continuous (measurements on multiple days per month), you can use the breaks parameter in scale_x_continuous() to specify each break you want. Your breaks will also have to be in date format, not text strings, so you'll need dmy() to convert a vector of all the first-day-of-the-month dates. That can be tedious if your data spans many years, but I can't think of how else you can force all the month labels to show up on a continuous axis.
If your data can be summarised as a single x value for each entire month, you can convert the Date into a month-year string or factor variable and use that as a discrete x axis. Discrete axis automatically show all the x values, so you won't have to create all the breaks. This should be relatively easy to achieve.

coding for quarterly time series data in ggplot2 examples not found

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")

Trying to manually set the x-axis range for a date variable

I want to have the x-axis run from March until November although the dates only run between April and August.
Generate random date data and convert to data frame
date.data <- sample(seq(as.Date("2016-04-01"),as.Date("2016-08- 31"),by = "day"),50)
date.df <- as.data.frame(date.data)
generate ggplot histogram with default values for x-axis
ggplot(date.df,aes(date.data)) + geom_histogram()
Attempt to override the default values by writing a datebreaks vector and using a scale command. The resulting histogram still only spans April through August.
datebreaks <- seq(as.Date("2016-03-01"),as.Date("2016-11-15"),by = "month")
ggplot(date.df,aes(date.data)) + geom_histogram() +
scale_x_date(breaks = datebreaks,labels=date_format("%b"))
How can I have the x-axis run from March until November?
aosmith made the useful suggestion to use xlim(). That works except the histogram has only the alternate months labeled on the axis. If I use scale_x_date to have each month labeled, that scale overrides the xlim(command)
mar <- as.Date("2016-03-01")
nov <- as.Date("2016-11-01")
ggplot(date.df,aes(date.data)) + geom_histogram() +
xlim(c(mar,nov))
Just what I am looking for except only alternate months appear as labels.
The addition of the scale command gets all months labeled but I lose the March to November span of the x-axis.
mar <- as.Date("2016-03-01")
nov <- as.Date("2016-11-01")
ggplot(date.df,aes(date.data)) + geom_histogram() +
xlim(c(mar,nov)) + scale_x_date(date_breaks = "1 month", date_labels = "%b")
Anyone know a way to use xlim() and set the label formats (each month appearing as a label) at the same time?

plotting multiple plot in R for different calendar date

I have about 20 years of daily data in a time series. It has columns Date, rainfall and other data.
I am trying plot rainfall vs Time. I want to get 20 line plots with different colours and legend is generated that show the years in one graph. I tried the following codes but it is not giving me the desired results. Any suggestion to fix my issue would be most welcome
library(ggplot2)
library(seas)
data(mscdata)
p<-ggplot(data=mscdata,aes(x=date,y=precip,group=year,color=year))
p+geom_line()+scale_x_date(labels=date_format("%m"),breaks=date_breaks("1 months"))
It doesnt look great but here's a method. We first coerce the data into dates in the same year:
mscdata$dayofyear <- as.Date(format(mscdata$date, "%j"), format = "%j")
Then we plot:
library(ggplot2)
library(scales)
p <- ggplot(data = mscdata, aes(x = dayofyear, y = precip, group = year, color = year))
p + geom_line() +
scale_x_date(labels = date_format("%m"), breaks = date_breaks("1 months"))
While I agree with #Jaap that this may not be the best way to depict these data, try to following:
mscdata$doy <- as.numeric(strftime(mscdata$date, format="%j"))
ggplot(data=mscdata,aes(x=doy,y=precip,group=year)) +
geom_line(aes(color=year))
Although the given answers are good answers to your questions as it stands, i don't think it will solve your problem. I think you should be looking at a different way to present the data. #Jaap already suggested using facets. Take for example this approach:
#first add a month column to your dataframe
mscdata$month <- format(mscdata$date, "%m")
#then plot it using boxplot with year on the X-axis and month as facet.
p1 <- ggplot(data = mscdata, aes(x = year, y = precip, group=year))
p1 + geom_boxplot(outlier.shape = 3) + facet_wrap(~month)
This will give you a graph per month, showing the rainfall per year next to one each other. Because i use boxplot, the peaks in rainfall show up as dots ('normal' rain events are inside box).
Another possible approach would be to use stat_summary.

Resources