Setting column width in charts in R - 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")

Related

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

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.

ggplot2 scale_x_date in Shiny reactive table [duplicate]

This question already has an answer here:
How to make scale_x_date week start with Sunday
(1 answer)
Closed 4 years ago.
I have a Shiny app which has both a reactive table, and a ggplot2 line chart (time series) that uses the reactive table as the dataset. I am using scale_x_date(date_breaks = "1 week") in the line chart, and the min and max values of the dates seen change appropriately with the reactive table.
My problem is that I would like the week intervals to be a Saturday (corresponding with the end of our fiscal week, and all the timestamps in the reactive table) but ggplot2 defaults to showing Mondays. How may I change the x-axis to show Saturdays but also scale appropriately in response to the reactive table?
How about like this, borrowing from here but using lubridate to set the lowest break at the preceding Saturday?
library(lubridate)
scale_x_date(breaks = seq(floor_date(min(data.set$week.start), unit = 'week',
week_start = getOption('lubridate.week.start', 6)), max(data.set$week.start),by="week"),
labels = date_format(format = "%Y-%m-%d"))
I just needed
output$fcstlinechart_weekly <- renderPlotly({
p1 <- ggplot2::ggplot(data= my_reactive_table(),
aes(x=Week_End_Date,y=Forecast)+
geom_line()+ scale_x_date(
breaks= unique(data_frame_source_for_reactive_table$Week_End_Date))
p2 <- ggplotly(p1)
p2$elementId <- NULL
p2
})
And the plot rendering was "smart enough" to only show date values that exist in the filtered reactive table.

Resources