合并两个列表以生成两个级别的因数
Combine two lists to make a factor of two levels
我有两个列表:
t1.visit.attendance
[1] 77 74 71 62 59 56 55
t2.visit.attendance
[1] 37 34 33 31 27 26 24
我想做一个因子,级别是t1.visit.attendance和t2.visit.attendance。请注意:t1和t2是两种不同的治疗类型。
然后我想在同一图上根据数字 1 到 7(对应于第 n 次访问)绘制数据散点图,其中每种治疗类型都用颜色编码。
lattice 库有一个辅助函数,叫做 make.groups
。例如
t1.visit.attendance<-c(77,74,71,62,59,56,55)
t2.visit.attendance<-c(37,34,33,31,27,26,24)
lattice::make.groups(t1=t1.visit.attendance, t2=t2.visit.attendance)
# data which
# t11 77 t1
# t12 74 t1
# t13 71 t1
# t14 62 t1
# ...
# t26 26 t2
# t27 24 t2
另一种方式,只是为了显示一些选项,涉及 reshape2::melt
函数:
t1.visit.attendance <- c(77,74,71,62,59,56,55)
t2.visit.attendance <- c(37,34,33,31,27,26,24)
df <- data.frame(t1 = t1.visit.attendance,
t2 = t2.visit.attendance)
newdf <- melt(df, measure.vars = c("t1", "t2"), variable.name = "visits")
> newdf
visits value
1 t1 77
2 t1 74
3 t1 71
4 t1 62
5 t1 59
6 t1 56
7 t1 55
8 t2 37
9 t2 34
10 t2 33
11 t2 31
12 t2 27
13 t2 26
14 t2 24
要显示散点图以外的图,这是使用 dotplot
的另一种方法
ggplot(newdf) +
aes(x=value, color = visits) +
geom_dotplot() +
theme(axis.text.y=element_blank())
屈服
我有两个列表:
t1.visit.attendance
[1] 77 74 71 62 59 56 55
t2.visit.attendance
[1] 37 34 33 31 27 26 24
我想做一个因子,级别是t1.visit.attendance和t2.visit.attendance。请注意:t1和t2是两种不同的治疗类型。
然后我想在同一图上根据数字 1 到 7(对应于第 n 次访问)绘制数据散点图,其中每种治疗类型都用颜色编码。
lattice 库有一个辅助函数,叫做 make.groups
。例如
t1.visit.attendance<-c(77,74,71,62,59,56,55)
t2.visit.attendance<-c(37,34,33,31,27,26,24)
lattice::make.groups(t1=t1.visit.attendance, t2=t2.visit.attendance)
# data which
# t11 77 t1
# t12 74 t1
# t13 71 t1
# t14 62 t1
# ...
# t26 26 t2
# t27 24 t2
另一种方式,只是为了显示一些选项,涉及 reshape2::melt
函数:
t1.visit.attendance <- c(77,74,71,62,59,56,55)
t2.visit.attendance <- c(37,34,33,31,27,26,24)
df <- data.frame(t1 = t1.visit.attendance,
t2 = t2.visit.attendance)
newdf <- melt(df, measure.vars = c("t1", "t2"), variable.name = "visits")
> newdf
visits value
1 t1 77
2 t1 74
3 t1 71
4 t1 62
5 t1 59
6 t1 56
7 t1 55
8 t2 37
9 t2 34
10 t2 33
11 t2 31
12 t2 27
13 t2 26
14 t2 24
要显示散点图以外的图,这是使用 dotplot
ggplot(newdf) +
aes(x=value, color = visits) +
geom_dotplot() +
theme(axis.text.y=element_blank())
屈服