【问题标题】:Producing line graphs with multiple lines: X-axis is out of order and y-axis scales are not in even intervals生成多条线的折线图:X 轴无序且 y 轴刻度不等间隔
【发布时间】:2019-11-18 22:33:46
【问题描述】:

我正在绘制涉及不同施肥量的实验数据 (ggplot2)。我想注意一系列观察日期(处理后的天数,或 DAT)上的植物反应(植物指数,或数据中的 VI_Values):9、15、21、27 和 35 DAT。当我绘制这些点时,我的 DAT(x 轴)不是我想要的顺序,我的 y 轴值是乱序的。我怎样才能解决这两个问题,我怎样才能重组 y 轴以增加均匀的间隔?最好在 y 轴上仅显示整数(或至少仅显示整数的几位小数)。

运行此代码...

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

...生成以下图表:

我需要按如下顺序对 x 轴进行排序:9、15、21、27、35 DAT,并且我肯定想清理那个 y 轴。

这是数据集:

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))

输入输出:

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))

【问题讨论】:

  • 检查is.character(pio_final$DAT)是否返回TRUE
  • 在您的数据示例中,没有名为SR的列
  • @markus 我刚刚检查过,它确实返回为TRUE
  • @ihb 所以转换成数字,然后绘图。
  • 请使用dput(pio_final)分享您的数据;我假设is.character(pio_final$SR) 也是TRUE

标签: r ggplot2 graph


【解决方案1】:

要在 x 轴上设置您自己的值,请使用 scale_x_discrete 函数:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-24
    • 2015-03-24
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多