有没有办法在基数 R 的原点处绘制单个原点符号?

问题描述

我正在尝试在原点绘制一个大写字母O,但如何在该区域绘制?

MWE:

par(mar = c(6,6,4,3) + 0.1,mgp = c(4,1,0))
x <- c(0,10)
y <- c(0,10)
plot(x,y,axes = FALSE,xaxs = "i",yaxs = "i",type = "n",ylab = "")
mtext(expression(y),2,las = 1)
axis(1,0:10,c("",1:10))
axis(2,1:10),las = 1)

Origin location

解决方法

如果你想使用 R-base 试试这个:

plot(x,y,axes = FALSE,xaxs = "i",yaxs = "i",type = "n",ylab = "")
mtext(expression(italic('O')),side=1,line=0,at=0,adj = 1.5)
mtext(expression(y),2,4,las = 1)
axis(1,0:10,c("",1:10))
axis(2,1:10),las = 1)
,

根据评论 - 您要求的是基本的 R 解决方案,但这里是一个相当简单的 ggplot2 解决方案。不经常使用表达式,所以不太确定产生的警告。

这可能是基础 R 绘图比 ggplot 更优雅的时刻之一,所以我也很好奇基础 R 解决方案。

下面的 ggplot2 解决方案有点不理想,因为您需要手动调整注释的位置。可能有一种更程序化的方法来找出其他标签的位置,通过深入研究 grob,但我想这可能有点过头了。

library(ggplot2)
x <- y <- 0:10
mydat <- data.frame(x,y)

ggplot(mydat,aes(x,y)) +
  geom_point() +
  scale_x_continuous(expand = c(0,NA),breaks = 1:10) +
  scale_y_continuous(expand = c(0,breaks = 1:10) +
  coord_cartesian(xlim = c(0,ylim = c(0,clip = "off") +
  annotate(geom = "text",x = -.2,y = -.2,label = expression(italic("O"))) +
  theme_classic()
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'

reprex package (v0.3.0) 于 2021 年 1 月 30 日创建

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...