使用stat ='count'时,用ggplot(geom_line)分隔行 [英] Separating Lines with ggplot (geom_line) when using stat='count'
问题描述
我目前有一些基本上是一个因素和一个日期的数据.这是一个简化的想法.
I currently have some data that is basically a factor and a date. Here is a simplified idea of it.
date <- c(1901,1901,1901,1902,1902,1902,1901,1903,1902,1904,1902,1903,1903,1904,1905, 1901,1903,1902,1904,1902,1902,1903,1904,1902,1902,1901,1903,1903,1904,1905, 1905,1906,1907,1908,1901,1908,1907,1905,1906,1902,1903,1903,1903,1904,1905,1901,1901,1901,1902,1902,1902,1901,1903,1902,1904,1902,1903,1903,1904,1905,
1901,1903,1902,1904,1902,1902,1903,1904,1902,1902,1901,1903,1903,1904,1905,
1905,1906,1907,1908,1901,1908,1907,1905,1906,1902,1903,1903,1903,1904,1905,
1905,1906,1907,1908,1901,1908,1907,1920,1920,1920,1921,1921,1921,1921,1921)
genre <- sample(c("fiction","nonfiction"),105,replace=TRUE)
data <- as.data.frame(cbind(date,genre))
# I know this is not an ideal way to coerce to a numeric
data$date <- as.numeric(as.character(data$date))
到目前为止,太好了.但是,您会注意到,如果将其绘制出来,则该线所遮盖的数据之间会有很大的差距.该图将说明.
So far, so good. As you'll note if you plot it it, though, there is a big gap in the data which the line obscures. This plot will illustrate.
library(ggplot2)
ggplot(data,aes(x=date,color=genre)) + geom_line(stat='count')
我看过这篇文章,其中建议添加一个小组,我能做到的.
I have seen this post which suggests adding a group, which I can do.
data$group <- ifelse(data$date < 1910,1,2)
ggplot(data,aes(x=date,color=genre,group=group)) + geom_line(stat='count')
因此,似乎没有办法保留我要用于输出的色彩美学 ,并使用stat='count'
指定group
, while .例如,此图很好地显示了数据中的间隙,但由于genre
因子而失去了颜色/分度:
So there appears to be no way to preserve the color aesthetics I want for my output and specify a group
, while using stat='count'
. This plot, for instance, nicely shows the gap in the data, but loses the color/distinction based on the genre
factor:
ggplot(data,aes(x=date,color=genre,group=group)) + geom_line(stat='count')
那么,这不可能吗?我想念什么吗?是否有更好的方法来执行此操作,还是需要summarize
或以其他方式更改日期,以便在绘图阶段不依赖stat='count'
?
So, is this not possible? Am I missing something? Is there a better way to do this, or do I need to summarize
or otherwise mutate my date so that I don't rely on stat='count'
at the plotting stage?
推荐答案
您可以组合流派"和组"以用作您的group
变量.在这里,我通过interaction
函数执行此操作.
You can combine "genre" and "group" to use as your group
variable. Here I do this via the interaction
function.
ggplot(data,aes(x = date, color = genre, group = interaction(genre, group))) +
geom_line(stat = 'count')
这篇关于使用stat ='count'时,用ggplot(geom_line)分隔行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!