爲什麼不想像他們一樣實際分佈?
p <- ggplot(d, aes(visit, count))
p <- p + geom_bar(stat="identity", width=0.75)
p <- p + scale_x_discrete(expand=c(0,0))
p <- p + scale_y_continuous(expand=c(0,0))
p <- p + facet_wrap(~year)
p <- p + labs(x=NULL, y="Visits")
p <- p + ggthemes::theme_tufte(base_family="Helvetica")
p <- p + theme(legend.position="none")
p <- p + theme(panel.grid=element_line(color="#2b2b2b", size=0.15))
p <- p + theme(panel.grid.minor=element_blank())
p <- p + theme(panel.grid.major.x=element_blank())
p <- p + theme(axis.ticks=element_blank())
p <- p + theme(strip.text=element_text(hjust=0))
p <- p + theme(panel.margin.x=unit(1, "cm"))
p
要查看一年訪問計數的增量,你可以交換方面:
d$year <- factor(d$year)
d$visit <- sprintf("Visit: %d", d$visit)
d$visit <- factor(d$visit, levels=unique(d$visit))
p <- ggplot(d, aes(year, count))
p <- p + geom_segment(aes(xend=year, yend=0), size=0.3)
p <- p + geom_point()
p <- p + scale_x_discrete(expand=c(0, 0.25))
p <- p + scale_y_continuous(label=scales::comma)
p <- p + facet_wrap(~visit, scales="free_y")
p <- p + labs(x="NOTE: Free y-axis scale", y="Count")
p <- p + ggthemes::theme_tufte(base_family="Helvetica")
p <- p + theme(legend.position="none")
p <- p + theme(panel.grid=element_line(color="#2b2b2b", size=0.15))
p <- p + theme(panel.grid.minor=element_blank())
p <- p + theme(panel.grid.major.x=element_blank())
p <- p + theme(axis.ticks=element_blank())
p <- p + theme(strip.text=element_text(hjust=0))
p <- p + theme(panel.margin=unit(1.5, "cm"))
p
或者,你可以看看同比增幅由訪問(%):
library(dplyr)
group_by(d, visit) %>%
arrange(year) %>%
mutate(lag=lag(count),
chg_pct=(count-lag)/lag,
chg_pct=ifelse(is.na(chg_pct), 0, chg_pct),
pos=as.character(sign(chg_pct))) -> d
p <- ggplot(d, aes(year, chg_pct))
p <- p + geom_hline(yintercept=0, color="#2b2b2b", size=0.5)
p <- p + geom_segment(aes(xend=year, yend=0, color=pos), size=0.3)
p <- p + geom_point(aes(color=pos))
p <- p + scale_x_discrete(expand=c(0, 0.25))
p <- p + scale_y_continuous(label=scales::percent)
p <- p + scale_color_manual(values=c("#b2182b", "#878787", "#7fbc41"))
p <- p + facet_wrap(~visit, scales="free_y")
p <- p + labs(x="NOTE: free y-axis", y="YoY % Difference per visit count")
p <- p + ggthemes::theme_tufte(base_family="Helvetica")
p <- p + theme(legend.position="none")
p <- p + theme(panel.grid=element_line(color="#2b2b2b", size=0.15))
p <- p + theme(panel.grid.minor=element_blank())
p <- p + theme(panel.grid.major.x=element_blank())
p <- p + theme(axis.ticks=element_blank())
p <- p + theme(strip.text=element_text(hjust=0))
p <- p + theme(panel.margin=unit(1.5, "cm"))
p <- p + theme(legend.position="none")
p
爲什麼不減少你的數據做更有意義的類別? (1,2,3,4和更多?) – Heroka
我同意。 5次以上的數字不會增加太多。 –