0

I am trying to plot %change in Oil price over the years 1988 to 2022 as per below code:

change_in_oil_price <- read.csv("Desktop/R models/change in oil price.csv")
head(change_in_oil_price)

plot1 <- ggplot(change_in_oil_price, aes(Year, Annual...Change, group = 1)) +
geom_point() +
geom_line() +
labs(x = "Year", y = "%change in Price",
title = "Annual Change in Brent Crude Price")
plot1

However, I am not getting the Y-axis as desired. I want 0 in the middle so I can compare/analyze the prices. Please see the attached Image.

enter image description here

I am trying to fix the y-axis.

stefan
  • 90,330
  • 6
  • 25
  • 51
Ali
  • 1
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Mar 22 '22 at 17:49
  • .. this said: The issue is that your `Annual...Change` column isn't a numeric. To fix your issue convert it to a numeric using e.g. `readr::parse_number(Annual...Change)`. – stefan Mar 22 '22 at 17:51
  • @stefan thanks for your comment, here's what the data looks like:Year Average.Closing.Price Year.Open Year.High Year.Low Year.Close Annual...Change 1 2022 $97.06 $78.25 $133.18 $78.25 $110.39 42.92% 2 2021 $70.86 $50.37 $85.76 $50.37 $77.24 50.80% 3 2020 $41.96 $67.05 $70.25 $9.12 $51.22 -24.42% 4 2019 $64.28 $54.06 $74.94 $53.23 $67.77 34.01% – Ali Mar 22 '22 at 19:02
  • Annual...Change is a % number, I did as you mentioned but still the problem persists: change_in_oil_price <- read.csv("Desktop/R models/change in oil price.csv") head(change_in_oil_price) readr::parse_number(change_in_oil_price$Annual...Change) plot1 <- ggplot(chasx``nge_in_oil_price, aes(Year, Annual...Change, group = 1)) + geom_point() + geom_line() + labs(x = "Year", y = "%change in Price", title = "Annual Change in Brent Crude Price") plot1 – Ali Mar 22 '22 at 19:04

1 Answers1

0

As I mentioned in my comment, the issue is that your Annual...Change column isn't a numeric but a character. For humans it's a percentage number but for R it's a character string made up of digits, a dot and a percent symbol.

To fix your issue convert your Annual...Change column to a numeric which could e.g. easily be done with readr::parse_number which will take care of the percent symbol.

Using some fake example data to mimic your real data:

# Create example data
change_in_oil_price <- data.frame(
  Year = 1990:2020,
  Annual...Change = paste0(seq(-70, 150, length.out = 31), "%")
)

Without converting to a numeric I run in the same issue as you, i.e. the numbers on the y axis get ordered alphabetically:

library(ggplot2)
library(readr)

ggplot(change_in_oil_price, aes(Year, Annual...Change, group = 1)) +
  geom_point() +
  geom_line() +
  labs(x = "Year", y = "%change in Price",
       title = "Annual Change in Brent Crude Price")

Converting to a numeric gives a nice plot instead with the numbers in the desired order. Additionally I make use of scale_y_continuous(labels = scales::label_percent(scale = 1)) to format the axis labels as percentages:

change_in_oil_price$Annual...Change <- readr::parse_number(change_in_oil_price$Annual...Change )

ggplot(change_in_oil_price, aes(Year, Annual...Change, group = 1)) +
  geom_point() +
  geom_line() +
  scale_y_continuous(labels = scales::label_percent(scale = 1)) +
  labs(x = "Year", y = "%change in Price",
       title = "Annual Change in Brent Crude Price")

stefan
  • 90,330
  • 6
  • 25
  • 51