0

I have a problem with the order of the x-axis in my plot.

Download the data from the website of the Central Bank of Perú (GDP by quarter).

library(jsonlite)
library(rstudioapi)
library(ggplot2)
library(data.table)

PBI <- "PN02635BQ"
URL3 <- paste0("https://estadisticas.bcrp.gob.pe/estadisticas/series/api/",
               PBI,"/json/2018-1-1/2021-7-31")

Use of json for download the data

l_json <- jsonlite::fromJSON(URL3)
dt_PBI <- data.table(l_json$periods)
sapply(dt_PBI,class)
dt_PBI[,values := round(as.numeric(values),4)]
colnames(dt_PBI)<- c("Quarter", "Millions")

As you see the plot is not in order in the x-axis.

ggplot(dt_PBI, aes(x=Quarter, y=Millions)) +
geom_point()
Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

0

The x-axis is not in order because your character column becomes a factor and is sorted by alphanumeric ordering. So in your case "T1.19" would sort before "T2.18". To fix this, use library forcats and sort the factor based on the year. I split this out into its own column ("year") to make it clear what was being accomplished here.

library(jsonlite)
library(rstudioapi)
library(ggplot2)
library(data.table)
library(forcats)

PBI <- "PN02635BQ"
URL3 <- paste0("https://estadisticas.bcrp.gob.pe/estadisticas/series/api/",
               PBI,"/json/2018-1-1/2021-7-31")


l_json <- jsonlite::fromJSON(URL3)
dt_PBI <- data.table(l_json$periods)
sapply(dt_PBI,class)
dt_PBI[,values := round(as.numeric(values),4)]
colnames(dt_PBI) <- c("Quarter", "Millions")

dt_PBI$year <- substr(dt_PBI$Quarter, 4, 5)
dt_PBI$Quarter <- fct_reorder(dt_PBI$Quarter, dt_PBI$year, min)

ggplot(dt_PBI, aes(x=Quarter, y=Millions)) +
  geom_point()
Tiberius
  • 11
  • 4
  • Thanks for the answer, but I notice that when I want to change the geom_point to geom_line, appears a warning sign: geom_path: "Each group consists of only one observation. Do you need to adjust the group aesthetic? " – Armando Delgado Sep 13 '21 at 18:20
  • Yes, your variable is a character which means that it is a "categorical" variable. Therefore, a line would not have worked because lines work with "continuous" data. – Tiberius Sep 17 '21 at 05:10