I want to plot the time evolution for the variable lending per capita. The data in column "year" is numeric, so I’ve added a new column to my data frame named "yearint" where I convert year from numeric to integer. Unfortunately, the x-axis still shows years with a decimal place (see image). I just want to have the year without any decimal place on the x-axis. How can I do this?
#adding a new column to my data frame for "year" as an integer
myDataframe$yearint <- as.integer(myDataframe$year)
#creating a plot
myPlot <- group_by(myDataframe, yearint) %>% summarize(myVariablePlot = mean(myVariable))
timeEvolution <- ggplot(myPlot, aes(x = yearint, y = myVariablePlot)) + xlim(2009,2019) + geom_point(size=3) + geom_line(color = "steelblue") + labs(x = "Year", y = "Average log lending per capita") + ggtitle("Time evolution for lending")
print(timeEvolution)
>Solution :
Since we don’t have your original data, I’ll use the gapminder dataset from the gapminder package to demonstrate.
First take a look at df, we can confirm the year column is of type integer (<int>).
library(gapminder)
library(tidyverse)
df <- filter(gapminder::gapminder, country == "Afghanistan")
df
# A tibble: 12 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
Then plot the data. Here we need scale_x_continuous. To achieve what you want, you need to set both limits and breaks. To mimic your graph, I’ve set breaks to every two years with limits of ten years.
ggplot(df, aes(year, lifeExp)) +
geom_point(size=3) +
geom_line(color = "steelblue") +
scale_x_continuous(limits = c(1952, 1962), breaks = seq(1952, 1962, 2))

