【问题标题】:ggplot - using annotate across facetsggplot - 跨方面使用注释
【发布时间】:2020-03-05 00:12:06
【问题描述】:

这是一个基本问题,但无法在此处找到答案。我正在使用 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 在绘图的两个方面的两个矩形之间留下一条线。目前它只在顶部(愤怒)方面的两者之间留下一条线。我认为在不指定方面的情况下提供矩形坐标应该在绘图的每个方面绘制相同的两个矩形......

关于如何使底部刻面看起来像顶部刻面有什么想法吗?

提前致谢!

【问题讨论】:

  • annotate rect 线为我显示(如果图沿垂直轴足够大)。如果你扩大情节大小,它会出现吗?或者对 ymin 和 ymax 使用不同的数字(尝试 590 和 610 而不是 599 和 602)?
  • 啊,是的,如果我更改 ymin 和 ymax 以使空间更大,它确实会显示出来!但是,如果我保留原始值,即使我全屏显示图像,它仍然不会显示在底部。很奇怪,但我可以改用更大的间隔 - 感谢您的帮助

标签: r ggplot2 rect facet-wrap annotate


【解决方案1】:

这是在设备上正确显示的问题,我建议您将其保存为 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)

【讨论】:

  • 我同意这也是我的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 2021-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多