如何根据我的输入子集在 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))
Run Code Online (Sandbox Code Playgroud)
但在这里,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)
Run Code Online (Sandbox Code Playgroud)
但是得到这个错误 - 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
.
.
.
Run Code Online (Sandbox Code Playgroud)
要么把事情分开:
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))
Run Code Online (Sandbox Code Playgroud)
或使用适当的{:
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))
}
Run Code Online (Sandbox Code Playgroud)
(都未经测试,因为没有可重复或最小的例子。)