Trouble making plot correctly

Hi! I am having some issues making a plot. My goal is to have a plot that shows the inflation of turkey compared to OECD total. I am able to get a plot however the lines are not at all how they should be.

rep:
library(tidyverse)
library(janitor)
library(OECD)
dsets<-get_datasets()
search_dataset("cpi",dsets)

CPI <- get_dataset("PRICES_CPI",
filter = "TUR+OECD",
start_time = 2003,
end_time = 2023,
pre_formatted = TRUE)

CPI <- CPI %>%
filter(TIME_FORMAT=="P1M",
REFERENCEPERIOD == "2015_100",
MEASURE == "IXOB")

CPI <- CPI %>%
select(Time, ObsValue, LOCATION)

CPI <- CPI %>%
mutate(date = ym(Time))

p <- ggplot(CPI, aes(x=date, y = ObsValue, group = LOCATION, color = LOCATION))+
geom_line(aes(group=LOCATION), size = 1) +
labs(title = "Inflation in Turkey compared to OECD Total the past 20 years (2015=100)")

p

My output looks like this:

However I want it to look something like this:

I appreciate any help!

It seems your ObsValue column is a factor and there may be other problems. Please post the output of

dput(head(CPI, 25))

using the version of CPI that you use in the ggplot code. Place rows with three back ticks just before and after the pasted output, like this
```
output of dput goes here
```

Your main problem is that your CPI variable is a string not a number. You can convert it to a numeric value with

CPI$ObsValue <- as.numeric(CPI$ObsValue)

This will give you a more meaningful graph. However you should note that you are not plotting inflation, but the price level, which is not very meaningful. To get the inflation you should divide the CPI by the lagged CPI (12 months before) and subtract 1.

I also do not believe that your data is correct. The CPI values fall too much and too often between one period and the next. I strongly suspect that the variable which you think is CPI is not in fact the CPI, but without checking the original source I cannot be sure. I would suggest looking at the original OECD data for a few months to check whether it corresponds to your data.

I hope this helps a little

Thanks, this pointed out many things I had forgotten about! I was uncertain about the data as well, it should be correct although I am new to using the OECD package and I may have sorted it wrong. I'll look more into this and try to get the correct dataset, thanks! Might be easier to just download OECD csv file instead.

The plot is still off, but this issue may be caused by the numbers not being correct?

put(head(CPI, 25)) gives me this result:

> dput(head(CPI, 25))
structure(list(Time = c("2005-01", "2005-02", "2005-03", "2005-04", 
"2005-05", "2005-06", "2005-07", "2005-08", "2005-09", "2005-10", 
"2005-11", "2005-12", "2006-01", "2006-02", "2006-03", "2006-04", 
"2006-05", "2006-06", "2006-07", "2006-08", "2006-09", "2006-10", 
"2006-11", "2006-12", "2007-01"), ObsValue = c("63.20835", "58.84876", 
"56.8858", "61.77608", "67.23127", "67.77908", "62.72902", "59.55063", 
"60.23538", "64.60068", "66.88319", "66.48946", "62.33529", "57.21106", 
"55.53912", "60.64053", "67.23698", "67.8019", "63.09993", "58.73463", 
"60.15549", "65.97018", "69.13716", "67.75625", "62.04427"), 
    LOCATION = c("TUR", "TUR", "TUR", "TUR", "TUR", "TUR", "TUR", 
    "TUR", "TUR", "TUR", "TUR", "TUR", "TUR", "TUR", "TUR", "TUR", 
    "TUR", "TUR", "TUR", "TUR", "TUR", "TUR", "TUR", "TUR", "TUR"
    ), date = structure(c(12784, 12815, 12843, 12874, 12904, 
    12935, 12965, 12996, 13027, 13057, 13088, 13118, 13149, 13180, 
    13208, 13239, 13269, 13300, 13330, 13361, 13392, 13422, 13453, 
    13483, 13514), class = "Date")), row.names = c(NA, -25L), class = c("tbl_df", 
"tbl", "data.frame"))

The quotation marks around the ObsValue values show that they are characters. As @Rolando suggested, run

CPI$ObsValue <- as.numeric(CPI$ObsValue)

to fix that

1 Like

Yes exactly. Your CPI series is moving up and down rather than rising smoothly as price level data usually does, particularly when you have a significant positive inflation rate. That is how I was almost sure your data is incorrect.

You are correct. The data I retrieved from the OECD package was not the same as the data on the OECD website. Thank you for shining a light upon this and for all help, I now have a graph that correctly shows inflation as a % increase each year!

You are welcome. I am pleased to have been able to help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.