ggplot - 在各个方面使用注释
ggplot - using annotate across facets
这是一个基本问题,但无法在此处找到答案。我正在从以下(过于简化的)数据中使用 ggplot 创建一个图形:
df.for.graph <- setNames(data.frame(matrix(ncol = 5,nrow = 8)), c("xp","yp","loc","cong","emotion"))
df.for.graph$xp <- c(948.7, 977.2, 1023.4, 953.3, 979.4,936.3, 911.6,877.2)
df.for.graph$yp <- c(923.0, 893.0, 294.9, 241.5, 898.6, 960.9, 154.4, 263.4)
df.for.graph$loc <- as.factor(c("Bottom", "Bottom", "Top", "Top", "Bottom", "Bottom", "Top", "Top"))
df.for.graph$cong <- as.factor(c("Incongruent","Congruent","Incongruent","Congruent", "Incongruent","Congruent","Incongruent","Congruent"))
df.for.graph$emotion <- as.factor(c("Angry", "Angry", "Angry", "Angry", "Happy","Happy", "Happy","Happy"))
我对ggplot的调用如下:
ggplot(df.for.graph,aes(x=xp,y=yp,color=loc,shape=cong)) +
geom_point() +
scale_color_manual(values=c("red","blue")) +
scale_shape_manual(values=c(1,4)) +
scale_fill_manual(values=c("green", "yellow")) +
scale_x_continuous(breaks = seq(from = 0, to = 1920, by = 160), limits=c(0,1920)) +
scale_y_reverse(breaks = seq(from = 0, to = 1200, by = 80), limits=c(1200,0)) +
labs(shape = "Congruence", color = "Probe Location",x = "X Position", y = "Y Position") +
facet_wrap(vars(emotion),nrow=2,ncol=1) +
theme(axis.title.x = element_text(face="bold",size=20),
axis.text.x = element_text(face="bold",size=15, color="black"),
axis.title.y = element_text(face="bold",size=20),
axis.text.y = element_text(face="bold",size=15, color="black"),
panel.background = element_rect(fill="white"),
panel.border = element_rect(colour = "black", fill=NA, size=2),
strip.text = element_text(face="bold",size=20),
legend.text = element_text(colour = "black", size=15),
legend.title = element_text(colour = "black", size=15)) +
annotate("rect",xmin=0, xmax=1920, ymin=0, ymax=599,alpha=.4) +
annotate("rect",xmin=0, xmax=1920, ymin=602, ymax=1200,alpha=.4)
结果如下:
enter image description here
但是我希望调用 annotate 在绘图的两个方面的两个矩形之间留下一条线。目前它只在顶部(愤怒)方面的两者之间留下一条线。我认为在不指定小平面的情况下提供矩形坐标应该在图的每个小平面上绘制相同的两个矩形...
关于如何使底部刻面看起来像顶部刻面有什么想法吗?
提前致谢!
这是它在设备上正确显示的问题,我建议您将其保存为 png 或 pdf。首先将绘图另存为对象:
g1 = ggplot(df.for.graph,aes(x=xp,y=yp,color=loc,shape=cong)) +
geom_point() +
scale_color_manual(values=c("red","blue")) +
scale_shape_manual(values=c(1,4)) +
scale_fill_manual(values=c("green", "yellow")) +
scale_x_continuous(breaks = seq(from = 0, to = 1920, by = 160), limits=c(0,1920)) +
scale_y_reverse(breaks = seq(from = 0, to = 1200, by = 80), limits=c(1200,0)) +
labs(shape = "Congruence", color = "Probe Location",x = "X Position", y = "Y Position") +
facet_wrap(vars(emotion),nrow=2,ncol=1) +
theme(axis.title.x = element_text(face="bold",size=20),
axis.text.x = element_text(face="bold",size=15, color="black"),
axis.title.y = element_text(face="bold",size=20),
axis.text.y = element_text(face="bold",size=15, color="black"),
panel.background = element_rect(fill="white"),
panel.border = element_rect(colour = "black", fill=NA, size=2),
strip.text = element_text(face="bold",size=20),
legend.text = element_text(colour = "black", size=15),
legend.title = element_text(colour = "black", size=15)) +
annotate("rect",xmin=0, xmax=1920, ymin=0, ymax=599,alpha=.4) +
annotate("rect",xmin=0, xmax=1920, ymin=602, ymax=1200,alpha=.4)
并保存:
ggsave(g1,file="g1.png",width=12,height=12)
这是一个基本问题,但无法在此处找到答案。我正在从以下(过于简化的)数据中使用 ggplot 创建一个图形:
df.for.graph <- setNames(data.frame(matrix(ncol = 5,nrow = 8)), c("xp","yp","loc","cong","emotion"))
df.for.graph$xp <- c(948.7, 977.2, 1023.4, 953.3, 979.4,936.3, 911.6,877.2)
df.for.graph$yp <- c(923.0, 893.0, 294.9, 241.5, 898.6, 960.9, 154.4, 263.4)
df.for.graph$loc <- as.factor(c("Bottom", "Bottom", "Top", "Top", "Bottom", "Bottom", "Top", "Top"))
df.for.graph$cong <- as.factor(c("Incongruent","Congruent","Incongruent","Congruent", "Incongruent","Congruent","Incongruent","Congruent"))
df.for.graph$emotion <- as.factor(c("Angry", "Angry", "Angry", "Angry", "Happy","Happy", "Happy","Happy"))
我对ggplot的调用如下:
ggplot(df.for.graph,aes(x=xp,y=yp,color=loc,shape=cong)) +
geom_point() +
scale_color_manual(values=c("red","blue")) +
scale_shape_manual(values=c(1,4)) +
scale_fill_manual(values=c("green", "yellow")) +
scale_x_continuous(breaks = seq(from = 0, to = 1920, by = 160), limits=c(0,1920)) +
scale_y_reverse(breaks = seq(from = 0, to = 1200, by = 80), limits=c(1200,0)) +
labs(shape = "Congruence", color = "Probe Location",x = "X Position", y = "Y Position") +
facet_wrap(vars(emotion),nrow=2,ncol=1) +
theme(axis.title.x = element_text(face="bold",size=20),
axis.text.x = element_text(face="bold",size=15, color="black"),
axis.title.y = element_text(face="bold",size=20),
axis.text.y = element_text(face="bold",size=15, color="black"),
panel.background = element_rect(fill="white"),
panel.border = element_rect(colour = "black", fill=NA, size=2),
strip.text = element_text(face="bold",size=20),
legend.text = element_text(colour = "black", size=15),
legend.title = element_text(colour = "black", size=15)) +
annotate("rect",xmin=0, xmax=1920, ymin=0, ymax=599,alpha=.4) +
annotate("rect",xmin=0, xmax=1920, ymin=602, ymax=1200,alpha=.4)
结果如下: enter image description here
但是我希望调用 annotate 在绘图的两个方面的两个矩形之间留下一条线。目前它只在顶部(愤怒)方面的两者之间留下一条线。我认为在不指定小平面的情况下提供矩形坐标应该在图的每个小平面上绘制相同的两个矩形...
关于如何使底部刻面看起来像顶部刻面有什么想法吗?
提前致谢!
这是它在设备上正确显示的问题,我建议您将其保存为 png 或 pdf。首先将绘图另存为对象:
g1 = ggplot(df.for.graph,aes(x=xp,y=yp,color=loc,shape=cong)) +
geom_point() +
scale_color_manual(values=c("red","blue")) +
scale_shape_manual(values=c(1,4)) +
scale_fill_manual(values=c("green", "yellow")) +
scale_x_continuous(breaks = seq(from = 0, to = 1920, by = 160), limits=c(0,1920)) +
scale_y_reverse(breaks = seq(from = 0, to = 1200, by = 80), limits=c(1200,0)) +
labs(shape = "Congruence", color = "Probe Location",x = "X Position", y = "Y Position") +
facet_wrap(vars(emotion),nrow=2,ncol=1) +
theme(axis.title.x = element_text(face="bold",size=20),
axis.text.x = element_text(face="bold",size=15, color="black"),
axis.title.y = element_text(face="bold",size=20),
axis.text.y = element_text(face="bold",size=15, color="black"),
panel.background = element_rect(fill="white"),
panel.border = element_rect(colour = "black", fill=NA, size=2),
strip.text = element_text(face="bold",size=20),
legend.text = element_text(colour = "black", size=15),
legend.title = element_text(colour = "black", size=15)) +
annotate("rect",xmin=0, xmax=1920, ymin=0, ymax=599,alpha=.4) +
annotate("rect",xmin=0, xmax=1920, ymin=602, ymax=1200,alpha=.4)
并保存:
ggsave(g1,file="g1.png",width=12,height=12)