Reputation: 2179
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
.
.
.
Upvotes: 1
Views: 5626
Reputation: 35187
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.)
Upvotes: 6