Reputation: 430
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))
Upvotes: 3
Views: 468
Reputation: 35604
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)
Upvotes: 2