For all the things you want to do, it would be easier if you work with dates instead of integers, try to do something like this.
library(dplyr)
library(lubridate)
library(ggplot2)
sample_data <- data.frame(date = c(20110101L, 20111231L, 20120105L),
value = runif(3, 0, 20))
# Converting integers to dates
sample_data <- sample_data %>%
mutate(date = ymd(date))
# Using dates with ggplot
sample_data %>%
ggplot(aes(x = date, y = value)) +
geom_col() +
scale_x_date(date_breaks = '1 month', date_labels = '%Y-%m') +
theme_bw() +
theme(axis.text.x = element_text(angle=30, hjust=1, vjust = 1))
Created on 2019-02-08 by the reprex package (v0.2.1)
We could give you better help if you provide a REPR oducible EX ample (reprex) If you've never heard of a reprex before, you might want to start by reading this FAQ:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the error
The minimal runnable code necessary to reproduce the error, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of this with an example:
Minimal Dataset (Sample Data)
You need to provide a dataframe that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as example, that you are working with the iris dataframe
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 se…