Changing x axis label in GGPlot2 - r

I have an r data set which has money spendings spread across months, and also grouped by years.
Year|Mth_Year|Mth_Spend
2004|01-2004|42507163
2004|02-2004|3377592
2006|10-2006|3507636
2006|11-2006|4479139
2006|12-2006|2439603
I need to display the monthly information (grouped year wise), so that some quick comparisons can be done.
I am using the ggplot, geom_bar options to display the monthly spends. Below is the code, I use.
ggplot(data=yearly_spending,aes(x=Year,y=Mth_Spend,fill=Mth_Year))+
geom_bar(stat="identity",color="black",position=position_dodge())+
theme(axis.text.x=element_text(angle=90,hjust=1))
When I use this code, the bar chart is getting displayed. But in X Axis, only the years (2004, 2005 & 2006) are displayed. Can I get the months also displayed above the years. The years can appear horizontally, and while months can be placed vertically.

Thank you for all the suggestions. Using the scales package and lubridate to convert strings into date, I could solve the issue.
ggplot(data=monthly_spend_catwise,aes(x=Mth_Year,y=TotSpending,fill=Type))+
scale_x_date(labels=date_format("%m-%Y"),date_breaks = "1 month")+
theme(axis.text.x=element_text(angle=45,hjust=1,vjust=0.5))+
geom_bar(stat="identity",color="black",position=position_dodge())
I had formatted the dates in Mth_Year (instead of 01-2004, made it into 01-01-2004), using the below code.
spending$Mth_Year <- as.Date(paste("01",spending$Mth_Year,sep="-"),"%d-%m-%y")

Related

plotly and week number line graph

I am trying to graph the weekly evolution of the number of downloads.
I have data similar data for all o week year and plotly cut de line.
example:
Downloads_weekly=Downloads.groupby(['Week_Number']).agg({'Daily Installs' : 'sum'})
fig1=px.line(Downloads_weekly.reset_index() , x='Week_Number' , y= 'Daily Installs')
fig1.update_xaxes(
tickformat="%Y-%W"
)
fig1
I have the exact same issue- Im still trying to figure it out. What I think is that Plotly is converting the dates to months, anyway it becomes a big mess. The way I solve the problem is by hacking around it, I create my Year-Week columns
df['Year-Week'] = df['Date'].dt.strftime('%Y.%U')
And then I sort the data frame, and plot that
df.sort_values(['Year-Week'], ascending=True, inplace=True)

How can I convert a characters into dates in RStudio?

still new to R. I wanted to create a simple (bar) chart of the fluctuations/occurrences of burglaries per month in my city. I found that the column, 'Occurence_Date' is a character, I wanted it to be "time", or something simpler, to create a visualization. I wanted the "x-axis" to be the months of January to June 2019, with the "y-axis" to be the amount of burglaries per month. Can anyone help me get started on this please? Thanks!
This is my data frame
The lubridate package is very helpful for working with dates and times in R.
# load.packages("lubridate") ## only run once
library(lubridate)
df$Occurence_Date <- ymd(df$Occurence_Date) # converts text in year month day format, igrores time
Generally it's better to put example data in your question so people can work with it and show an example.

Force ggplot scales to start on e.g. 1st of year, 1st of month etc

I'm looking for a way to force the date labels on a ggplot to start at a (seemingly) logical time. I've had the problem a number of times but my current problem is I want the breaks to be on the 01/01/yyyy
My data is a large dataset with POSIXct Date column, data to plot in Flow column and a number of site names in the Site column.
library(ggplot2)
library(scales)
ggplot(AllFlowData, aes(x=Date, y = Flow, colour = Site))+geom_line()+
scale_x_datetime(date_breaks = "1 year", expand =c(0,0),labels=date_format("%Y"))
I can force the breaks to be every year and they appear okay without the labels=date_format("%Y") (starting on 01/01 each year) but if I include labels=date_format("%Y") (as there is 10 years of data so gets a bit messy) the date labels move to ~November, and 1989 is the first label even though my data starts on the 01/01/1990.
I have had this problem numerous times in the past on different time steps, such as wanting to force it to the 1st of the month or daily times to be at midnight instead during the day. Is there a generic way to do this?
I have looked at create specific date range in ggplot2 ( scale_x_date), but I do not want to have to hard code my breaks as I have a fair few plots to do with different date ranges.
Thanks
If the dates come to you in a vector like:
dates <- seq.Date(as.Date("2001-03-04"), as.Date("2001-11-04"), by="day")
## "2001-03-04" "2001-03-05" "2001-03-06" ... "2001-11-03" "2001-11-04"
use pretty.Dates() to make a best guess about the end points.
range(pretty(dates))
## "2001-01-01" "2002-01-01"
Then pass this range to ggplot.
However, I recommend coord_cartesian() instead of scale_x_date(). Typically I want to crop the graphic bounds, instead of flat-out exclude the values entirely (which can mess up things like a loess summary).

Problems with drawing graphs in R

I am making a plot of means in R and my data collection started in October and ends in August the following year. the problem is when I draw my graph the default graph that I get has the months in alphabetical order(i.e. april,august,december etc..) instead of the order that I had enter them(i.e. ocboer, november,december etc..). how do I change that?
You can make your month into a factor variable and then plot the factor on the X axis.
factorisedMonth <- factor(oldMonth,levels=month.name)
R has a built in month.name that orders the factor properly if it's the standard english names in full

How can I define custom quarter boundaries (not calendar) in R?

I see a ton of libraries like zoo, ts, timeSeries for working with quarters but I can't seem to figure out a way to change quarter boundaries.
The data I analyze needs to be broken into fiscal quarters.
Ex:
Fiscal Q1: 7/28/2013 - 10/26/2013
Fiscal Q2: 10/27/2013 - 1/25/2014
and so on...
Try useing cut to define your own date ranges:
boundaries <- as.Date(c("7/28/2013","10/27/2013","1/26/2014"),"%m/%d/%Y")
quarterNames <- c("Fiscal Q1","Fiscal Q2")
cut(vectorOfDates ,
breaks = boundaries,
labels = quarterNames)
Note that you need one more boundary than label (since the labels are applied to the ranges between the breaks), and that the boundaries must span your date range, otherwise you'll introduce missing values.

Resources