Formatting X axis label in R for a specific condition

CrashOverride

For the following sample code:

y <- c(23, 34, 11, 9.6, 26, 31, 38, 38, 30, 36, 31)
days <- seq(as.Date("2015-2-25"), by="day", length=11)
n <- length(y)
x <- 1:n
plot(x, y, type='n', xlab="Days", ylab="Y", xaxt='n')
axis(1, at=seq(1,11) ,labels=format(days, "%d"), las=1)
lines(y)

I have the following chart:

enter image description here

What I want is when the month changes, I want to be able to add the month name below the day on the x axis. So in this example, when it becomes 01, it should show 01 Mar (March on a separate line)

LyzandeR

It can happen if you do something like this:

Your data plus the month vector:

y <- c(23, 34, 11, 9.6, 26, 31, 38, 38, 30, 36, 31)
days <- seq(as.Date("2015-2-25"), by="day", length=11)

#my addition
#contains the name of the month for where day == '01' else is blank
months <- ifelse(format(days, '%d')=='01',  months(days) , '') 

n <- length(y)
x <- 1:n

Solution:

plot(x, y, type='n', xlab="Days", ylab="Y", xaxt='n')
axis(1, at=seq(1,11) ,labels=format(days, "%d"), las=1)
lines(y)

Up to here is only your code. Now you need to add a new axis, set the axis colour to white and plot the month vector created above:

par(new=T)           #new plot
par(mar=c(4,4,4,2))  #set the margins. Original are 5,4,4,2.
#I only changed the bottom margin i.e. the first number from 5 to 4

#plot the new axis as blank (colour = 'white')
axis(1, at=seq(1,11) ,labels=months, las=1, col='white')

The result looks like what you asked for:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JQuery Flot X axis label formatting

From Dev

X axis label formatting in Shinobi charts

From Dev

JQuery Flot X axis label formatting

From Dev

jqPlot not showing x-axis label or formatting axis

From Dev

R: how to label the x-axis of a boxplot

From Dev

Label the x axis correct in a histogram in R

From Dev

Specific axis formatting in matplotlib

From Dev

JavaFX Chart axis label formatting

From Dev

In Highcharts drilldown charts, Custom Formatting of X-Axis label displays the label with underline even in the last level

From Dev

How to label specific points on x-axis in MATLAB?

From Dev

Modify a specific x-axis tick label in python

From Dev

hPlot R x-axis label and x-y correspondence

From Dev

ggplot2: Conditional formatting of x axis label in facet_grid

From Dev

R: Boxplot - how to move the x-axis label down?

From Dev

R how to label non-sequential x-axis ticks

From Dev

R: Boxplot - how to move the x-axis label down?

From Dev

Custom ggplot2 axis and label formatting

From Dev

Rotating axis label in R

From Dev

x-axis label in Flot

From Dev

Highcharts Omits X Axis Label

From Dev

Change the tick label of x axis

From Dev

r- axis label in image

From Dev

Adding label to secondary axis in R

From Dev

Change Number Formatting on X-Axis Matplotlib

From Dev

SSRS bar char x-axis formatting

From Dev

Formatting X axis with dates format Matplotlib

From Dev

Application of date formatting on x-axis text

From Dev

Formatting pyplot major and minor label's distance from axis

From Dev

Add a Specific Y-Axis Label with Highcharts