How can I control the centerline lengths in ggplot?

This question is not related to controlling the axis limits (I think), but rather to controlling the length of the axes themselves. I am trying to make ggplot (so that I can use its good faceting capabilities), which is similar to the basic R plot, where the axes are added manually. These axes apply only to the last axis label.

Some of these settings and the basic type of the R-type that I am trying to simulate:

library("ggplot2")
library("cowplot")
library("grid")

set.seed(5)
x <- rnorm(10)
y <- rnorm(10)
D <- data.frame(x, y)

plot(x, y, axes = FALSE)
axis(1)
axis(2)

R-graphics base

ggplot(D, aes(x, y)) +
  geom_point()

By default, the changes to ggplot from the package are cowplotpretty close:

enter image description here

But how can I tell ggplot to only draw lines to the last label of the label, even if the points are outside this value (as in the basic R plot)?

+4
source share
1 answer

, @Gregor , , . , , . , , x y annotate coord_cartesian.

  my_theme = list(theme_bw(),
                  theme(panel.border=element_blank(), 
                        panel.grid.major=element_blank(),
                        panel.grid.minor=element_blank()),
                  labs(y="",x=""))

  ggplot(D, aes(x,y)) +
    geom_point() +
    coord_cartesian(xlim=c(-1.4,1.4), ylim=c(-2.4,1.4)) +
    my_theme +
    annotate(x=-1.4, xend=-1.4, y=-2, yend=1, colour="red", lwd=0.75, geom="segment") +
    annotate(x=-1, xend=1, y=-2.4, yend=-2.4, colour="red", lwd=0.75, geom="segment") 

enter image description here

+7

Source: https://habr.com/ru/post/1615812/


All Articles