Changing Axis Ticks

I'm plotting 2 lines on the same graph and I'm having difficulty chagning the X axis labels. I think this is becuase of the way my data is formatted but I'm not sure. My data is an excel file converted to csv format. I just want to have the years 2010 through to 2017 along the X axis rather than 0, 25, 50, 75, 100. I've commented out where I was using scale_x_discrete as it seems to just remove the x axis ticks.

My code:


mydf <- read.csv("EC4044 Data.csv") #Loads data into R studio

mydf$fuelpriceX25 <- mydf$fuelprice * 25

head(mydf)

ggplot(mydf, aes(x=1:96)) + 
  geom_line(aes(y = shareprice), color = "darkred") +
  geom_line(aes(y = fuelpriceX25), color="steelblue") +
  ggtitle("Share Price vs. Jet Fuel Price (2010-2017)") +
  #scale_x_discrete(breaks = c("0", "12", "24", "36", "48", "60", "72", "84"),
                   #labels = c("2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017")) +
  labs(x="", y="Price in $") +
  theme(axis.title.y = element_text(size=12, family="Trebuchet MS", color="#666666")) +
  theme(plot.title = element_text(size=16, family="Trebuchet MS", face="bold", hjust=0, color="#666666")) +
  theme(axis.text = element_text(size=8, family="Trebuchet MS"))

My Output

  ï..na count       date shareprice fuelprice fuelpriceX25
1     0     1 01/01/2010      25.23     2.129       53.225
2     0     2 01/02/2010      26.68     2.018       50.450
3     0     3 01/03/2010      26.39     2.144       53.600
4     0     4 01/04/2010      27.35     2.272       56.800
5     0     5 01/05/2010      22.86     2.199       54.975
6     0     6 01/06/2010      26.31     2.105       52.625

0000ba

When I change "ggplot(mydf, aes(x=1:96)) +" to "ggplot(mydf, aes(date)) +" I get an empty graph with lots of overlapping text where the x axis ticks should be

I think your dates are being imported as characters or as a factor. You can use the mdy() function from lubridate to convert them to a date and then use scale_x_date in ggplot to set up the axis.
To convert the date, try this

mydf <- read.csv("EC4044 Data.csv", stringsAsFactors = FALSE)
library(lubridate)
mydf$date <- mdy(mydf$date)

Here is an example of manipulating the x axis.

DF <- data.frame(Date = seq.Date(as.Date("2010-01-01"), as.Date("2017-12-31"), 1),
                 SharePrice = runif(2922, min = -1, max = 1) + seq(20, 45, length.out = 2922),
                 FuelPrice = rnorm(2922, 30, 2))
library(ggplot2)
ggplot(DF, aes(x = Date)) + 
  geom_line(aes(y = SharePrice)) + 
  geom_line(aes(y = FuelPrice)) +
  scale_x_date(breaks = seq.Date(from = as.Date("2010-01-01"),
                                 to = as.Date("2017-01-01"), by = "year"),
               labels = as.character(seq(2010, 2017, 1)))

Created on 2020-04-16 by the reprex package (v0.3.0)

Doh! I bet your dates are in the format d/m/y. If so, convert them with the dmy() function instead of using mdy().

1 Like

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

Do you have data only for the first few days of each year? That would account for the appearance of the graph. Can you post the result of the command

head(mydf, 25)

I've used lubridate to convert the date but I'm having trouble manipulating the x axis.

I'm assuming the code below just creates a sample data frame for you to work on and that I don't actaully need to include this in my code...or do I? (Sorry I'm quite new to R)

DF <- data.frame(Date = seq.Date(as.Date("2010-01-01"), as.Date("2017-12-31"), 1),
                 SharePrice = runif(2922, min = -1, max = 1) + seq(20, 45, length.out = 2922),
                 FuelPrice = rnorm(2922, 30, 2))

I added scale_x_date to my code and I now have the years along the X axis but the lines are messed up.

Input:

mydf <- read.csv("EC4044 Data.csv", stringsAsFactors = FALSE)

library(tidyverse)
library(lubridate)

mydf$date <- mdy(mydf$date)

mydf$fuelpriceX25 <- mydf$fuelprice * 25

ggplot(mydf, aes(x=date)) + 
  geom_line(aes(y = shareprice), color = "darkred") +
  geom_line(aes(y = fuelpriceX25), color="steelblue") +
  ggtitle("Share Price vs. Jet Fuel Price (2010-2017)") +
  scale_x_date(breaks = seq.Date(from = as.Date("2010-01-01"),
                                 to = as.Date("2017-01-01"), by = "year"),
               labels = as.character(seq(2010, 2017, 1))) +
  labs(x="", y="Price in $") +
  theme(axis.title.y = element_text(size=12, family="Trebuchet MS", color="#666666")) +
  theme(plot.title = element_text(size=16, family="Trebuchet MS", face="bold", hjust=0, color="#666666")) +
  theme(axis.text = element_text(size=8, family="Trebuchet MS"))

Output:
image

Thanks so much that's fixed it :star_struck: It was stupid of me not to realize in the first place that mdy() stood for month/day/year :laughing: