1

How do I have dynamic limits and breaks in my ggplot based on my input subset. My code works like this,

Listed %>%
  filter(Listed$Country == 'USA' & Listed$period > max(Listed$period) - months(60)) %>%
  mutate(Months_year = format(as.Date(period), "%b")) %>%
  mutate(fill = ifelse(Months_year %in% past3months,"A","B")) %>%
  ggplot(aes(x = variable, y = value,fill = fill)) + guides(fill=FALSE) +
  geom_bar(stat = "identity") +
  theme_classic() +
  labs(x = "",y="") +
  ggtitle("Newly Listed") + 
  theme(plot.title = element_text(hjust = 0.5,face="bold"))+
  scale_x_date(labels = date_format("%b-%Y"), date_breaks  ="2 month",     
  expand = c(0.005,0)) + 
  scale_y_continuous(limits=c(0,max(Listed$value)), 
                     breaks  = seq(0,max(Listed$value), by = 2000),
  expand = c(0,0))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=0.5))

But here, the max(Listed$value) does not consider the filter I have applied filter(Listed$Country == 'USA' & Listed$period > max(Listed$period) - months(60)) - which is only USA and past 5 years. Do I need to apply the same filters again within the max function ? Or any other way to make use of the existing pipe data ?

Let me know if any other further information is required. I don't know why people down vote here instead of asking for clarification.

EDIT. -- I tried to use just the column name,

limits=c(0,max(value)), 
                     breaks  = seq(0,max(value), by = 2000)

But getting this error - Error in seq.default(0, max(value), by = 2000) : object 'value' not found

Sample data..

head(Listed)

  Country   period    value
    USA    2007-01-01   704
    UK     2007-01-01  3621
    AU     2007-01-01   776
    USA    2007-02-01  1015
    AU     2007-02-01    71
   China   2007-03-01   485
   .
   .
   .
ds_user
  • 2,139
  • 4
  • 36
  • 71
  • replace all the `listed$VARNAME` by `VARNAME` in your code, eg. `max(Listed$value)` => `max($value)` – scoa May 15 '17 at 08:47
  • I tried it. Getting this error. Error in seq.default(0, max(value), by = 2000) : object 'value' not found – ds_user May 15 '17 at 08:49
  • You should show some of your data then: http://stackoverflow.com/a/5963610/4132844 – scoa May 15 '17 at 08:54

1 Answers1

6

Either break things apart:

Listed2 <- Listed %>%
  filter(Listed$Country == 'USA' & Listed$period > max(Listed$period) - months(60)) %>%
  mutate(Months_year = format(as.Date(period), "%b")) %>%
  mutate(fill = ifelse(Months_year %in% past3months,"A","B"))

ggplot(Listed2, aes(x = variable, y = value,fill = fill)) + guides(fill=FALSE) +
  geom_bar(stat = "identity") +
  theme_classic() +
  labs(x = "",y="") +
  ggtitle("Newly Listed") + 
  theme(plot.title = element_text(hjust = 0.5,face="bold"))+
  scale_x_date(labels = date_format("%b-%Y"), date_breaks  ="2 month",     
  expand = c(0.005,0)) + 
  scale_y_continuous(limits=c(0,max(Listed2$value)), 
                     breaks  = seq(0,max(Listed2$value), by = 2000),
  expand = c(0,0))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=0.5))

Or use appropriate {:

Listed %>%
  filter(Listed$Country == 'USA' & Listed$period > max(Listed$period) - months(60)) %>%
  mutate(Months_year = format(as.Date(period), "%b")) %>%
  mutate(fill = ifelse(Months_year %in% past3months,"A","B")) %>%
  {
    ggplot(., aes(x = variable, y = value,fill = fill)) + guides(fill=FALSE) +
    geom_bar(stat = "identity") +
    theme_classic() +
    labs(x = "",y="") +
    ggtitle("Newly Listed") + 
    theme(plot.title = element_text(hjust = 0.5,face="bold"))+
    scale_x_date(labels = date_format("%b-%Y"), date_breaks  ="2 month",     
    expand = c(0.005,0)) + 
    scale_y_continuous(limits = c(0,max(.$value)), 
                       breaks = seq(0,max(.$value), by = 2000),
    expand = c(0,0))+
    theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=0.5))
  }

(Both untested, because no reproducible or minimal example.)

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • And I do not know how to provide the reproducible example, have given my code and sample dataset. Not sure what is expected further. – ds_user May 15 '17 at 19:57
  • Thanks. Both the solution works. Ideally "value" won't be found if I don't specify {. Just noticed that. Thanks again. – ds_user May 15 '17 at 22:12