我有以下示例数据:
my.list <- vector('list',1000)
for(i in 1:1000)
{
temp <- sample(c("type1","type2"),1)
my.list[[i]] <- data.frame(time=i,type=temp)
}
df <- do.call('rbind',my.list)
我想绘制类型变量随时间的变化。我使用了以下内容:
ggplot(df,aes(x=time,y=type)) + geom_line()
使用此命令,我没有得到预期的结果:
注意如何在图中不显示从类型1到类型2的转换,反之亦然。我错过了什么吗?
另外,在这个图中,似乎在时间x,类型变量同时将type1
和type2
作为与数据框内容相矛盾的值
答案 0 :(得分:2)
对于这两项工作,您必须使用group
参数。
ggplot(df,aes(x=time,y=type, group=1)) + geom_line()
但请注意,由于在使用1000次观测时线条非常密集,因此结果难以解释。如果你只使用100个观测值,那么
set.seed(1)
my.list <- vector('list',100)
for(i in 1:100)
{
temp <- sample(c("type1","type2"),1)
my.list[[i]] <- data.frame(time=i,type=temp)
}
df <- do.call('rbind',my.list)
结果如下:
XCode shows warning after adding App Groups (Add the “App Groups” entitlement to your App ID)