Is there a way to get the axes, with labels in the center of a ggplot2 plot, like a traditional graphing calculator? I\'ve looked through the docs and there doesn\'t seem to
There are some other useful answers, but the following comes closer to the target visual and avoids looping:
library(ggplot2)
library(magrittr)
# constants
axis_begin <- -2
axis_end <- 2
total_ticks <- 21
# DATA ----
# point to plot
my_point <- data.frame(x=1,y=1)
# chart junk data
tick_frame <-
data.frame(ticks = seq(axis_begin, axis_end, length.out = total_ticks),
zero=0) %>%
subset(ticks != 0)
lab_frame <- data.frame(lab = seq(axis_begin, axis_end),
zero = 0) %>%
subset(lab != 0)
tick_sz <- (tail(lab_frame$lab, 1) - lab_frame$lab[1]) / 128
# PLOT ----
ggplot(my_point, aes(x,y)) +
# CHART JUNK
# y axis line
geom_segment(x = 0, xend = 0,
y = lab_frame$lab[1], yend = tail(lab_frame$lab, 1),
size = 0.5) +
# x axis line
geom_segment(y = 0, yend = 0,
x = lab_frame$lab[1], xend = tail(lab_frame$lab, 1),
size = 0.5) +
# x ticks
geom_segment(data = tick_frame,
aes(x = ticks, xend = ticks,
y = zero, yend = zero + tick_sz)) +
# y ticks
geom_segment(data = tick_frame,
aes(x = zero, xend = zero + tick_sz,
y = ticks, yend = ticks)) +
# labels
geom_text(data=lab_frame, aes(x=lab, y=zero, label=lab),
family = 'Times', vjust=1.5) +
geom_text(data=lab_frame, aes(x=zero, y=lab, label=lab),
family = 'Times', hjust=1.5) +
# THE DATA POINT
geom_point(color='navy', size=5) +
theme_void()