Robbie Voort
Robbie Voort

Reputation: 141

Change the layout of a plot, for example a negative plot

I have noticed that while creating any lineplot or any plot for that matter, it always creates the same layout for the graph whether the values on the x and y labels are positive or negative.

So it make me wonder, and my sincere apologies for the quality of this image but I had to create this example in paint, is there any other layout available while creating lineplots. image

It appears that, no matter the values, example 1 is always created. But when my data only contains negative values, I would like to create a layout like example 3. This means that the x axis would be on top (instead of bottom) and the y axis on the right (instead of left)

As I work a lot with ggplot2 it would ofcourse be preferable that the solution would be useable in ggplot2. If that is not the case then I am just wondering if it is possible at all and if so, how?

Upvotes: 1

Views: 712

Answers (1)

Sinh Nguyen
Sinh Nguyen

Reputation: 4487

It really depend on your setting. With ggplot2 you can set the position of the x-axis & y-axis accordingly. Here is an example

library(ggplot2)

set.seed(10)
data <- data.frame(line = c("a", "a", "a", "b", "b", "b"),
  x = runif(6, min = -5, max = 0),
  y = runif(6, min = -5, max = 0))

ggplot(data) +
  geom_line(aes(x = x, y = y, group = line)) +
  scale_y_continuous(limits = c(-6, 0), position = "right",
    expand = c(0, 0)) +
  scale_x_continuous(limits = c(-6, 0), position = "top",
    expand = c(0, 0))  +
  theme_bw() +
  theme(panel.border = element_blank(),
    axis.line = element_line())

Created on 2021-05-26 by the reprex package (v2.0.0)

Upvotes: 1

Related Questions