3

I want my X axis text to look like:

J
a
n

not be rotated with the letters turned.

I want to keep it as a date axis. I know I could make it discrete with values of "J\na\na\n" for instance. Perhaps I can map a vector of values like that over the axis.text.x values? It seems like there should be an easier way though.

Below will demonstrate the issue. I've rotated it 90 degrees but as shown above this is not what I want.

library(tidyverse)
library(scales)

y<- c(52014,51598,61920,58135,71242,76254,63882,64768,53526,55290,45490,35602)
months<-seq(as.Date("2018-01-01"),as.Date("2018-12-01"),"month")
dat<-as.tibble(cbind(y,months)) %>% 
  mutate(month=as.Date(months,origin="1970-01-01"))

ggplot(dat) +
  geom_line(aes(x=month,y=y)) +
  scale_x_date(breaks=date_breaks("month"),labels=date_format("%b")) +
  theme(axis.text.x=element_text(angle=90))
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
Chris Umphlett
  • 380
  • 3
  • 15
  • Several suggestions here: [Insert line breaks in long string — word wrap](https://stackoverflow.com/questions/2351744/insert-line-breaks-in-long-string-word-wrap) – Henrik Oct 22 '18 at 14:38
  • I will try, but I was not assuming that I could use string functions on a date type column. the "Jan" "Feb" etc. come from using `scale_x_date(breaks="month")`. – Chris Umphlett Oct 22 '18 at 15:37
  • Perhaps use the `labels` argument? Also, it's much easier to help if you provide a minimal reproducible example. – Henrik Oct 22 '18 at 15:41
  • Tried the `gsub` function method, got an error that appeared to be due to the column type. Added an example to my original question. – Chris Umphlett Oct 22 '18 at 16:02

1 Answers1

2

Example data :

date <- seq(from = as.Date("2000-01-01"), to = as.Date("2000-12-01"), by = "month")
df <- data.frame(Month = date, Value = rnorm(12))

First, produce a custom set of dates you want. Here I use strsplit() and lapply to achieve your request.(month.name and month.abb are native character vectors in R )

mon.split <- strsplit(month.name, "")
mon <- unlist(lapply(mon.split, paste0, "\n", collapse = ""))
mon
 [1] "J\na\nn\nu\na\nr\ny\n"       "F\ne\nb\nr\nu\na\nr\ny\n"   
 [3] "M\na\nr\nc\nh\n"             "A\np\nr\ni\nl\n"            
 [5] "M\na\ny\n"                   "J\nu\nn\ne\n"               
 [7] "J\nu\nl\ny\n"                "A\nu\ng\nu\ns\nt\n"         
 [9] "S\ne\np\nt\ne\nm\nb\ne\nr\n" "O\nc\nt\no\nb\ne\nr\n"      
[11] "N\no\nv\ne\nm\nb\ne\nr\n"    "D\ne\nc\ne\nm\nb\ne\nr\n"

I supposed your date variable is 'Date' class so I use scale_x_date. If it's numeric or character, use scale_x_continuous and scale_x_discrete.

ggplot(df, aes(x = Month, y = Value)) +
  geom_line() +
  scale_x_date(breaks = date, labels = mon)

enter image description here

Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
  • Thanks. This is what I assumed was possible. I didn't know about `month.name` (is that some kind of constant built in to R?). I will use `month.abb` which I discovered also exists. – Chris Umphlett Oct 22 '18 at 16:11
  • 1
    Glad to help you. `month.name` and `month.abb` are native character vectors in R. – Darren Tsai Oct 22 '18 at 16:14
  • 1
    In case anyone else reads this, `breaks=date_breaks("month")` did not work. I had to switch it to `breaks=MY.DATE`. – Chris Umphlett Oct 22 '18 at 16:18