Problems with y axis - ggplot2

ggplot2
rstudio
#1

Hello,
I have recently started working in RStudio.
I'm using ggplot2 to have profile of elements concentration (x axis) with depth (y axis) but I have a little problem with Y axis.
1 - I need to intercept axis x and y at 0. How could I do it? Here's my code

DOCprofil <- ggplot(tab_Dora_PW_1, aes(x= DOC , y= Depth)) +
  geom_path(linetype= 3, size = 0.2) +
  geom_point(col= "slategray1", size = 2)+
  scale_x_continuous(name = "DOC (mg/L)", position= "top", limits = c(0, 40),expand=c(0,0)) +
  scale_y_continuous(limits = c(-30,30),expand=c(0,0))

2- Actually, my depth range is between 600 cm till - 30 cm and I need to present them in the same profile to describe a certain behavior. Yes, it's a huge marge of interval but I need my y axis to be from 600 to -30. On excel, I have these profile (see figure)
DOC%20plot

and my Rcode is:

DOCprofil <- ggplot(tab_Dora_PW_1, aes(x= DOC , y= Depth)) +
  geom_path(linetype= 3, size = 0.2) +
  geom_point(col= "slategray1", size = 2)+
  scale_x_continuous(name = "DOC (mg/L)", position= "top", limits = c(0, 40),expand=c(0,0)) +
  scale_y_discrete(limits = c(-30,-20,-10,0,10,20,30,100,350,700))

So I used scale_y_discrete by changing "limits" and "breaks" but It's always not clear like the profile on excel sheet.

So I would really appreciate some solutions for these issues.
Thanks a lot

0 Likes

#2

Axis breaks are not included in ggplot2 functionality. See here for more discussion.

1 Like

#3

Do I suppose also that I cannot use ggplo2 to intersect the axis (x and y at 0) especially my y axis goes from 30 to -30? There is any solution for this?
Thanks in advance

0 Likes

#4

Correct, it is also not possible to have your axes intersect at the origin in ggplot2.

You can add your own axes with horizontal and vertical lines.

library(tidyverse)

d <- tibble(x = runif(20, min = -30, max = 30),
            y = runif(20, min = -30, max = 30))

ggplot(d, aes(x = x, y = y)) + 
  geom_point() +
  geom_hline(aes(yintercept = 0)) + 
  geom_vline(aes(xintercept = 0))
1 Like

#5

Thanks a lot for your help.
It works.

0 Likes

#6

If your question's been answered, would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

2 Likes

closed #7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

0 Likes