I generate a plot with the code below:
filtered_data |>
filter(geo != "AT" & geo != "HU" & geo != "ES") |>
ggplot(aes(x = TIME_PERIOD, y = OBS_VALUE, group = geo, color = geo))+
geom_line()+
geom_line(data = filter(filtered_data, geo %in% c("AT", "HU", "ES")), linewidth = 1.2)+
scale_color_manual(values = c("AT" = "darkgoldenrod", "HU" = "darkred", "ES" = "darkgreen"))+
geom_text(data = last_values |> filter(geo %in% c("AT", "HU", "ES")),
aes(label = ifelse(geo %in% c("AT", "HU", "ES"), paste(OBS_VALUE, "%", sep = ""), OBS_VALUE)),
hjust = 0, vjust = -0.5)+
guides(color = "none")+
scale_x_discrete(expand = expansion(add = c(0, 10)))+
scale_y_continuous(labels = label_percent(scale = 1))+
labs(x = "", y = "",
title = "Inflation in the European Union",
subtitle = "Annual change in the Harmonized Consumer Prices Index 2010-2022",
caption = "Source: Eurostat. Figure: Darius Markovskij")+
theme_minimal()+
theme(panel.grid.minor = element_blank())
But as you can notice the dates labels in x-axis are too dense, so what should I do if I wany to display date labels by 5 years (only 2010, 2015 and 2020).
The values from TIME_PERIOD
are with format %Y-%m-%d
.
How could I do that? Thanks.
I tried to convert TIME_PERIOD
from character to date using this
filtered_data$TIME_PERIOD <- as.Date(filtered_data$TIME_PERIOD, format = "%Y-%m-%d")
However, when I enter this data to my ggplot()
, then the follwoing error message occurs - Error: Invalid input: date_trans works with objects of class Date only
.
I assume that your data looks like this after you converted
filtered_data$TIME_PERIOD
from class"character"
to class"Date"
:Please check for your data if
filtered_data$TIME_PERIOD
really has class `"Date" by running:Replacing
scale_x_discrete()
byscale_x_date()
withdate_breaks = "5 years"
should then result in the desired breaks of the x-axis: