r - ggplot - 跨方面使用注释
问题描述
这是一个基本问题,但无法在这里找到答案。我正在从以下(过于简化的)数据中使用 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)
这会产生以下结果: 在此处输入图像描述
但是,我希望调用 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)
推荐阅读
- python - 在变换之间保持恒定的线宽
- azure - 如何在 Azure 数据工厂中使用此 Rest API
- javascript - 通过 HOC 从单个 ES6 模块导出多个组件
- typescript - TypeScript 中的 .valueOf() 和 .toString()
- php - Magento 电子商务升级后产品未更新
- .net - Queryable 是从服务中获取元素的正确方法吗?
- python - 多变量的 Lambdifying 函数
- android - 获取“FirebaseAuthInvalidCredentialsException:电子邮件地址格式错误。” 使用 Firebase 进行身份验证时
- python-3.x - 如何在 Python3 中将十六进制编码字节转换为字符串?
- python-3.x - 狮身人面像不捕捉码头字符串