简体   繁体   中英

Producing line graphs with multiple lines: X-axis is out of order and y-axis scales are not in even intervals

I am working on graphing experimental data (ggplot2) involving different fertilizer rates. I wish to note the plant response (vegetative index, or VI_Values in the data) over a series of observation dates (days after treatment, or DAT): 9, 15, 21, 27, and 35 DAT. When I graph the points, my DATs (x-axis) are not in the order I want them, and my y-axis values are out of order. How can I fix these two things, and how can I restructure the y-axis to increase in even intervals? Preferable showing only whole numbers on the y-axis (or at least just a few decimals past the whole number).

Running this code...

ggplot(data=dataset, aes(x=DAT, y=SR, group = Rate, colour = as.factor(Rate)))+
  geom_line()+
  geom_point()

...produces the following graph:

在此处输入图像描述

I need to order the x-axis as follows: 9, 15, 21, 27, 35 DAT, and I definitely want to clean up that y-axis.

Here's the dataset:

dataset <- data.frame(Cultivar = c("pio", "pio", "pio", "pio", "pio", "pio", 
"pio", "pio", "pio", "pio", "pio", "pio", "pio", "pio", "pio", "pio", "pio", "pio", "pio", "pio"),
                      Rate = c(0,
                               1,
                               4,
                               8,
                               0,
                               1,
                               4,
                               8,
                               0,
                               1,
                               4,
                               8,
                               0,
                               1,
                               4,
                               8,
                               0,
                               1,
                               4,
                               8),
                      DAT = c(9,
                              9,
                              9,
                              9,
                              15,
                              15,
                              15,
                              15,
                              21,
                              21,
                              21,
                              21,
                              27,
                              27,
                              27,
                              27,
                              35,
                              35,
                              35,
                              35),
                      SR = c(5.443664,
                                   4.91077766666667,
                                   3.615712,
                                   2.81359,
                                   10.4675316666667,
                                   9.65146,
                                   7.646191,
                                   7.138025,
                                   8.24739066666667,
                                   7.85872166666667,
                                   6.14369533333333,
                                   5.83806466666667,
                                   13.3828463333333,
                                   12.3525363333333,
                                   11.0249503333333,
                                   10.5858046666667,
                                   13.8856,
                                   12.7933703333333,
                                   10.966898,
                                   11.194905),
                      Error = c(0.138439743861123,
                                0.445183750289448,
                                0.716154295933728,
                                0.209298947911833,
                                0.485569061785356,
                                0.870274032427143,
                                0.92619068130992,
                                0.896274542793855,
                                0.225475438285661,
                                0.863429277269874,
                                0.522656438625583,
                                0.827932691360905,
                                0.741721042845025,
                                1.2532188075592,
                                1.01358403281381,
                                1.16022067736693,
                                0.262671210179824,
                                1.02721331514967,
                                0.626616072499209,
                                0.669908769))

dput output:

structure(list(Cultivar = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "pio", class = "factor"), 
    Rate = c(0, 1, 4, 8, 0, 1, 4, 8, 0, 1, 4, 8, 0, 1, 4, 8, 
    0, 1, 4, 8), DAT = c(9, 9, 9, 9, 15, 15, 15, 15, 21, 21, 
    21, 21, 27, 27, 27, 27, 35, 35, 35, 35), SR = c(5.443664, 
    4.91077766666667, 3.615712, 2.81359, 10.4675316666667, 9.65146, 
    7.646191, 7.138025, 8.24739066666667, 7.85872166666667, 6.14369533333333, 
    5.83806466666667, 13.3828463333333, 12.3525363333333, 11.0249503333333, 
    10.5858046666667, 13.8856, 12.7933703333333, 10.966898, 11.194905
    ), Error = c(0.138439743861123, 0.445183750289448, 0.716154295933728, 
    0.209298947911833, 0.485569061785356, 0.870274032427143, 
    0.92619068130992, 0.896274542793855, 0.225475438285661, 0.863429277269874, 
    0.522656438625583, 0.827932691360905, 0.741721042845025, 
    1.2532188075592, 1.01358403281381, 1.16022067736693, 0.262671210179824, 
    1.02721331514967, 0.626616072499209, 0.669908769)), class = "data.frame", row.names = c(NA, 
-20L))

For setting your own values on the x-axis, use scale_x_discrete function:

scale_x_discrete(limits = c(9, 15, 21, 27, 35))

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