R X-Axis Date Labels Using Plot()

R X-axis Date Labels using plot()

With plots it's very hard to reproduce results with out sample data. Here's a sample I'll use

dd<-data.frame(
saldt=seq(as.Date("1999-01-01"), as.Date("2014-01-10"), by="6 mon"),
salpr = cumsum(rnorm(31))
)

A simple plot with

with(dd, plot(saldt, salpr))

produces a few year marks

Sample Image

If i wanted more control, I could use axis.Date as you alluded to

with(dd, plot(saldt, salpr, xaxt="n"))
axis.Date(1, at=seq(min(dd$saldt), max(dd$saldt), by="30 mon"), format="%m-%Y")

which gives

Sample Image

note that xlim will only zoom in parts of the plot. It is not directly connected to the axis labels but the axis labels will adjust to provide a "pretty" range to cover the data that is plotted. Doing just

xlim=c(as.Date("1999-01-01"),as.Date("2014-01-01"))

is the correct way to zoom the plot. No need for conversion to numeric or POSIXct.

Customize x-axis labels on ggplot: Only display dates that are on Mondays

You may try

ggplot(df, aes(x=Date, y=Count)) + 
geom_bar(stat="identity", fill="steelblue") +
scale_x_date(date_breaks = "week", date_labels = "%b %d")

Sample Image

Customize x-axis labels with dates in R

Doing this with ggplot is fairly easy, because you can use scale_x_date() (you need to convert x to date with as.Date()).

x <-
as.Date(
c(
"2016-05-09 09:00:00",
"2016-05-09 10:00:00",
"2016-05-09 11:00:00",
"2016-05-10 12:00:00"
)
)
y <- c(2, NA, 3, 5)
df <- data.frame(x, y)

library(ggplot2)
ggplot(df, aes(x = x, y = y)) + geom_line() + scale_x_date()

Sample Image

How to show date x-axis labels every 3 or 6 months in ggplot2

First convert date by: df$Month_Yr <- as.Date(as.yearmon(df$Month_Yr))

Then use this can solve the issue:

ggplot(reshaped_median, aes(x= Month_Yr, y = value))+ 
geom_line(aes(color = Sentiments)) +
geom_point(aes(color = Sentiments)) +
#Here you set date_breaks ="6 month" or what you wish
scale_x_date(date_labels="%b-%d",date_breaks ="3 month")+
labs(title = 'Change in Sentiments (in median)', x = 'Month_Yr', y = 'Proportion of Sentiments %') +
theme(axis.text.x = element_text(angle = 60, hjust = 1))

Time series plot in R skips dates on x-axis

With ggplot this should work -

library(ggplot2)
ggplot(by_date, aes(Date, sales)) + geom_line()

You can use scale_x_date to format your x-axis as you want.

Set x-axis labels to dates when plotting time series

When you call axis(1, at=time) you're telling R to plot the x axis with labels at points given by time. However, time is a vector of characters, not numbers.

In general, you call axis(1, at=..., labels=...) indicating the actual labels and where to place them along the axis. In your case, your call to plot.ts implicitly sets the x-axis limits to 2010 and 2013.75, so your at parameter should reflect those limits.

So you want to call axis saying that the labels are time and the positions are 2010, 2010.25, 2010.50 ..., that is, seq(from=2010, to=2013.25, by=0.25). A general solution is this one:

plot.ts(y,axes=F) # don't plot the axes yet
axis(2) # plot the y axis
axis(1, labels=time, at=seq(from=2010, by=0.25, length.out=length(time)) )
box() # and the box around the plot


Related Topics



Leave a reply



Submit