[英]Plotting every second data point in ggplot2
我有以下数据集:
Data <- data.frame(
week = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4),
variable = c("nuclear", "nuclear", "nuclear", "nuclear", "atomic", "atomic", "atomic", "atomic", "unemployment", "unemployment", "unemployment", "unemployment"),
value = c(2, 3, 1, 4, 5, 1, 2, 3, 3, 2, 5, 1)
)
我需要一张黑白图表。 所以我使用以下代码:
ggplot(Data, aes(week, value, group=variable)) + xlab("Year") + ylab("Frequency of searches") +
geom_line(aes(linetype=variable))+ # Line type depends on cond
geom_point(aes(shape=variable))+ # Shape depends on cond
scale_shape_manual(values=c(0,1,2)) + # Change shapes
scale_linetype_manual(values=c("solid", "dotted", "dotdash"))+
theme(legend.position="top", legend.title=element_blank(), panel.background = element_rect(fill = "#FFFFFF", colour="#000000"))+
stat_smooth(method="loess"))
这适用于这个小数据集。 但是当它更大时,很难区分线条和点。 这看起来非常相似。 有没有办法只绘制每秒或第十个数据点?
您可以使用子集覆盖geom_point
的数据:
geom_point(aes(shape=variable), data=subset(Data, week %% 2 == 1))
使用seq()
子集Data
:
Data[seq(1, nrow(Data), 4), ] # every 4th row
Data[seq(1, nrow(Data), 2), ] # every 2nd row
# ...
这是一种采用偶数行的方法,依赖于索引向量的循环:
Data[c(FALSE,TRUE),]
每隔10日:
Data[c(rep(FALSE, 9), TRUE),]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.