简体   繁体   中英

How do I change the tick labels of x and y-axis in ggplot2, axis disappearing

I'm looking to change the tick names of my x and y-axis, I want my finished product to be as similar to this attached photo as possible: Muniz & Leivestad (1980)

Here is the code that I have written to try to re-create the figure:

time <- c(0, 120, 0, 10, 18, 0, 
      20, 22, 0, 42, 48, 62,
      0, 21, 38)
ph <- as.factor(c(6, 6, 5.5, 5.5, 5.5, 5.1,
    5.1, 5.1, 4.5, 4.5, 4.5, 4.5,
    4.3, 4.3, 4.3))
plasma <- c(130, 130, 130, 119, 101,
        130, 100,92, 130,
        115, 108, 102, 130, 115,
        111)
lab6 <- c(" ", "pH 6.0", " ", " ", " ", " ",
             " ", " ", " ", " ", " ", " ",
             " ", " ", "")
lab5 <- c(" ", " ", " ", " ", "pH 5.5 ", " ",
      " ", "pH 5.1 ", " ", " ", " ", " ",
      " ", " ", "")
lab4 <- c(" ", " ", " ", " ", " ", " ",
      " ", " ", " ", " ", " ", "pH 4.5 ",
      " ", " ", "pH 4.3")

d <- data.frame(time, ph, plasma, lab6, lab5, lab4)

ggplot(d, aes(time, plasma)) +
geom_line(aes(colour = ph)) +
geom_point(aes(colour = ph)) +
geom_text(nudge_x = -5, 
        nudge_y = -2,
        aes(label = lab6, 
            colour = ph)) +
geom_text(nudge_x = -7,
         nudge_y = 0,
         aes(label = lab5,
             colour = ph)) +
geom_text(nudge_x = -8,
         nudge_y = 0,
         aes(label = lab4,
             colour = ph)) +
scale_x_discrete(name = "Hrs", 
                 breaks = c(0, 50, 100, 130), 
                 labels = c("0", "50", "100", "Hrs")) +
scale_y_discrete(name = "me/l", 
             breaks = c(60, 80, 100, 120, 130),
             labels = c("60", "80", "100", "120", "me/l"))

My problem Is that when I add "scale_x_discrete" and "scale_y_discrete" the ticks on both the x and y-axis disappears. As shown in the attached photo: Disappearing axis

Is it possible to achieve the result as in the photo with the help of ggplot2? Or should I just use "scale_x_continuous" and "scale_y_continuous" and have the labels under / on the side of the tick text?

Thank you in advance for your help.

Use scale_x/y_continuous that your axis using continuos values.

ggplot(d, aes(time, plasma)) +
  geom_line(aes(colour = ph)) +
  geom_point(aes(colour = ph)) +
  geom_text(nudge_x = -5, 
            nudge_y = -2,
            aes(label = lab6, 
                colour = ph)) +
  geom_text(nudge_x = -7,
            nudge_y = 0,
            aes(label = lab5,
                colour = ph)) +
  geom_text(nudge_x = -8,
            nudge_y = 0,
            aes(label = lab4,
                colour = ph)) +
  scale_x_continuous(name = "Hrs", 
                   breaks = c(0, 50, 100, 130), 
                   labels = c("0", "50", "100", "Hrs")) +
  scale_y_continuous(name = "me/l", 
                   breaks = c(60, 80, 100, 120, 130),
                   labels = c("60", "80", "100", "120", "me/l"))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM