如何根据输入子集在ggplot中设置动态限制和中断。我的代码就像这样,
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))
但是在这里,max(Listed$value)
并未考虑我应用的过滤器filter(Listed$Country == 'USA' & Listed$period > max(Listed$period) - months(60))
- 这只是美国和过去5年。我是否需要在max函数中再次应用相同的过滤器?或者使用现有管道数据的任何其他方式?
如果需要任何其他更多信息,请与我们联系。我不知道为什么人们在这里投票而不是要求澄清。
修改即可。 - 我试图只使用列名
limits=c(0,max(value)),
breaks = seq(0,max(value), by = 2000)
但是收到此错误 - Error in seq.default(0, max(value), by = 2000) : object 'value' not found
示例数据..
头(认证)
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
.
.
.
答案 0 :(得分:4)
要么将事情分开:
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))
或使用适当的{
:
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))
}
(两者均未经测试,因为没有可重复或最小的例子。)