Is there a way to plot a single origin symbol at the origin in base R?

Viewed 92

I am trying to plot a capital, italized O at the origin, but how do I plot in that area?

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, 4, las = 1)
axis(1, 0:10, c("", 1:10))
axis(2, 0:10, c("", 1:10), las = 1)

Origin location

2 Answers

If you want to use R-base try this:

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, 0:10, c("", 1:10), las = 1)

As per comment - you were asking for a base R solution, but here a fairly straight forward solution with ggplot2. Not working often with expressions, so not so sure about the warning which is produced.

This might be one of the moments where base R plotting shows more elegance than ggplot, so I am quite curious for a base R solution as well.

The ggplot2 solution below is a bit unideal, because you'll need to manually adjust the position of the annotation. There might be a more programmatic way to figure out the position of the other labels, by digging into the grobs, but I guess this would be maybe a bit of overdoing.

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, NA), breaks = 1:10) +
  coord_cartesian(xlim = c(0, NA), ylim = c(0, NA), 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'

Created on 2021-01-30 by the reprex package (v0.3.0)

Related