Errors while using scale_x_date

Hi forks,

I am trying to label my x axis, but I keep encountering errors.

Goal to achieve: transform 200303 to Mar 2003 with 6 months/1 year interval on my x axis.

This is the error I got in my R.
V3 %>%

  • ggplot(aes(shijian, P_INDEX, colour = EAs)) +
  • geom_line(size=0.8)+
  • scale_x_date(shijian, date_breaks = '1 year'
  •                date_labels = '%b-%Y')+
    

Error: unexpected symbol in:
" scale_x_date(shijian, date_breaks = '1 year'
** date_labels"**

scale_y_continuous(limits = c(100,150), breaks = scales::breaks_width(5))+

  • theme (plot.title = element_text(hjust = 0.5, face= 'bold'),
  •      plot.background = element_rect(fill = 'white'),
    
  •      panel.grid.major = element_blank(),
    
  •      panel.grid.minor = element_blank(),
    
  •      panel.border = element_blank(),
    
  •      axis.line.x = element_line(color = 'black'),
    
  •      axis.line.y = element_line(color = 'black'))+
    
  • ggtitle(label = 'P-index Graph')
    Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?
    Run rlang::last_error() to see where the error occurred.

head(V3)

A tibble: 6 x 3

EAs P_INDEX shijian

1 Cinema admission 77.3 200303
2 Cinema admission 79.1 200306
3 Cinema admission 78.9 200309
4 Cinema admission 80.1 200312
5 Cinema admission 80.7 200403
6 Cinema admission 81.6 200406

First time posting a question, if my format is hard to read, sorry in advance.

Thank you very much, guys.

Hi, your date column wasn't in a date format. You also had shijian in scale_x_date(), which didn't need to be there.

See the edited dataset here.

library(tidyverse)
library(scales)

V3 <- tibble::tribble(
               ~EAs, ~P_INDEX, ~shijian,
        "Cinema admission",     77.3,  200303L,
        "Cinema admission",     79.1,  200306L,
        "Cinema admission",     78.9,  200309L,
        "Cinema admission",     80.1,  200312L,
        "Cinema admission",     80.7,  200403L,
        "Cinema admission",     81.6,  200406L
        ) %>% 
  mutate(shijian = as.Date(paste0(as.character(shijian), "01"), format = "%Y%m%d"))
  

ggplot(V3, aes(shijian, P_INDEX, colour = EAs)) +
  geom_line(size=0.8) +
  scale_x_date(date_breaks = '3 months',
               date_labels = '%b-%Y') +
  scale_y_continuous(limits = c(50,100), breaks = scales::breaks_width(5)) +
  theme (plot.title = element_text(hjust = 0.5, face= 'bold'),
         plot.background = element_rect(fill = 'white'),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         panel.border = element_blank(),
         axis.line.x = element_line(color = 'black'),
         axis.line.y = element_line(color = 'black')) +
  labs(title = 'P-index Graph')

Next time please provide a reproducible dataset

Hi William,

Thank you very much for your reply. It totally makes sense.

I gave it a try. Somehow, I still get the error. I tried to create a new column, or simply as.Date the column itself, both gave me the same error.

A tibble: 395 x 4

EAs P_INDEX shijian date

1 Cinema admission 77.3 2003-03-01 2003-03-01
2 Cinema admission 79.1 2003-06-01 2003-06-01
3 Cinema admission 78.9 2003-09-01 2003-09-01
4 Cinema admission 80.1 2003-12-01 2003-12-01
5 Cinema admission 80.7 2004-03-01 2004-03-01
6 Cinema admission 81.6 2004-06-01 2004-06-01
7 Cinema admission 82.6 2004-09-01 2004-09-01
8 Cinema admission 82.9 2004-12-01 2004-12-01
9 Cinema admission 81.3 2005-03-01 2005-03-01
10 Cinema admission 76.5 2005-06-01 2005-06-01

... with 385 more rows

V3 %>%

  • ggplot(aes(date, P_INDEX, colour = EAs)) +
  • geom_line(size=0.8)+
  • scale_x_date(date_breaks = '3 months'
  •            date_labels = '%b-%Y')+
    

Error: unexpected symbol in:
" scale_x_date(date_breaks = '3 months'
** date_labels"**

scale_y_continuous(limits = c(100,150), breaks = scales::breaks_width(5))+

  • theme (plot.title = element_text(hjust = 0.5, face= 'bold'),
  •      plot.background = element_rect(fill = 'white'),
    
  •      panel.grid.major = element_blank(),
    
  •      panel.grid.minor = element_blank(),
    
  •      panel.border = element_blank(),
    
  •      axis.line.x = element_line(color = 'black'),
    
  •      axis.line.y = element_line(color = 'black'))+
    
  • ggtitle(label = 'P-index Graph')
    Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?
    Run rlang::last_error() to see where the error occurred.
    rlang::last_error()
    <error/rlang_error>
    Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?
    Backtrace:
  1. ggplot2:::+.gg(...)
    Run rlang::last_trace() to see the full context.

Really appreciate your response!!

Can you have a look at the reproducible example article? It makes it easier for people to help you if you format your code and provide a proper reproducible example.

Will do that.

Thanks a lot!

Hi Williaml,

Thanks for your help, I figured out the problem I encountered was, which was due to not creating a new data frame.

Thanks again.

1 Like

This topic was automatically closed 42 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.