在ggplot2的单个图上绘制3个变量 [英] plotting 3 variables on a single plot in ggplot2
问题描述
您好有一个由三个变量组成的实验,我想将它们全部绘制在一张图上。
这是我的df:
$ $ p $
AB < - data.frame(block = c(A,A,A,A,B ,B,B,B),
熟悉度= c(fam,fam,unfam,unfam),
prime = c(P ,UP),
RT = c(570.6929,628.7446,644.6268,607.4312,556.3581,645.4821,623.5624,604.4113)) $ b
现在我只能将其中一个变量分解为两个独立的图,如A和B是两个级别的第三个变量:
pre $ A < - AB [which(AB $ block ==A),]
B < - AB [其中(AB $ block ==B),]
pa < - ggplot(data = A,aes(x = prime,y = RT ,group = familiarity))+
geom_line(aes(linetype = familiarity),size = 1)+
expand_limits(y = c(500,650))
pb < - ggplot(data = B,aes(x = prime,y = RT,group = familiarity))+
geom_line(aes(linetype = familiarity),size = 1)+
expand_limits(y = c(500,650))
我想将图A叠加在图B上,并将这第三个变量用颜色标识。
有什么想法?
这是你的意思吗? code> p_all < - ggplot(AB,aes(x = prime,y = RT,group = interaction(familiarity,block)))+
geom_line(aes(linetype = familiarity,color = block) )
使用的数据:
AB < - 结构(list(block = structure(c(1L, 1L,1L,1L,2L,2L,2L,
2L),。标签= c(A,B),class =factor),熟悉度=结构(c(1L,
1L,2L,2L,1L,1L,2L,2L),class =factor,.Label = c(fam,
unfam)),prime = structure(c(1L, 2L,1L,2L,1L,2L,1L,2L
),class =factor,.Label = c(P,UP)),RT = c(570.6929,
628.7446,644.6268,607.4312,556.3581,645.4821,623.5624,604.4113
)),.Names = c(block,familiarity,prime,RT),row.names = c(N A,
-8L),class =data.frame)
Hi have an experiment which consists of three variables, and I would like to plot them all on a single plot.
This is my df:
AB <- data.frame(block=c("A", "A", "A", "A", "B", "B", "B", "B" ),
familiarity=c("fam", "fam", "unfam", "unfam" ),
prime=c("P", "UP" ),
RT=c("570.6929", "628.7446", "644.6268", "607.4312", "556.3581", "645.4821", "623.5624", "604.4113"))
Right now I can only break one of the variables into two separate plots, like this where A and B are the two levels of the third variable:
A <- AB[which(AB$block == "A"),]
B <- AB[which(AB$block == "B"),]
pa <- ggplot(data=A, aes(x=prime, y=RT, group=familiarity)) +
geom_line(aes(linetype=familiarity), size=1) +
expand_limits(y=c(500,650))
pb <- ggplot(data=B, aes(x=prime, y=RT, group=familiarity)) +
geom_line(aes(linetype=familiarity), size=1) +
expand_limits(y=c(500,650))
I would like to superimpose plot A over plot B, and have this third variables to be identified by color.
Any ideas?
Is this what you mean?
p_all <- ggplot(AB, aes(x=prime,y=RT,group=interaction(familiarity,block))) +
geom_line(aes(linetype=familiarity,color=block))
Data used:
AB <- structure(list(block = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("A", "B"), class = "factor"), familiarity = structure(c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L), class = "factor", .Label = c("fam",
"unfam")), prime = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), class = "factor", .Label = c("P", "UP")), RT = c(570.6929,
628.7446, 644.6268, 607.4312, 556.3581, 645.4821, 623.5624, 604.4113
)), .Names = c("block", "familiarity", "prime", "RT"), row.names = c(NA,
-8L), class = "data.frame")
这篇关于在ggplot2的单个图上绘制3个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!