【问题标题】:Plotting multiple line using ggplot with a timeline使用带有时间线的 ggplot 绘制多条线
【发布时间】:2017-11-05 11:37:58
【问题描述】:

我想显示特定时间段内生物量的变化。在每个图中将有两条线,即湿生物质和干生物质。我想为每种治疗制作三个单独的图表。

除草0、1和2是处理。日期是指他们被放置在研究站点的时间。所以 June-July 意味着样本是在 6 月安装的,在 7 月被检索。 我试图绘制一个简单的 ggplot 图,但图中没有得到任何线条。

ggplot(data=data) + geom_line(aes(y = Weeding1, x = Date_1, colour = State))

示例数据

Weeding1    State   Date    Weeding2    State   Date    Weeding0    State   Date
5.65         Wet    June-July   5.4     Wet   June-July 5.5         Wet June-July
4.88         Dry    June-July   4.73    Dry   June-July 4.7         Dry June-July
5.38         Wet    June-sept   5.25    Wet   June-sept 5.05        Wet June-sept
3.93         Dry    June-sept   3.9     Dry   June-sept 3.78        Dry June-sept
5.5          Wet    July-sept   5.5     Wet   July-sept 5.33        Wet July-sept 
4.43         Dry    July-sept   4.35    Dry   July-sept 4.13       Dry  July-sept
7.03         Wet    July-oct    6.46    Wet   July-oct  6.77       Wet  July-oct  
5.06         Dry    July-oct    5.27    Dry   July-oct  5.2         Dry July-oct
6.31         Wet    June-oct    4       Wet   June-oct  4.86        Wet June-oct
3.24         Dry    June-oct    3.07    Dry   June-oct  3.11        Dry June-oct

感谢您的宝贵时间

编辑

我重新排列了我的数据

Date(weeding1)  Wet Dry
July        5.65    4.88
June        20      7.11
September   5.38    3.93
October     6.31    3.24


ggplot(Litter1, aes(x = Date))+ geom_point(aes(y = Wet, colour = "Wet"))+ geom_point(aes(y = Dry, colour = "Dry"))

有了这个我明白了,但即使我最后添加 +geom_line() 我也没有得到这条线。谢谢

我明白了。

 ggplot(Litter1, aes(x = Date,group=1))+ geom_point(aes(y = Wet, colour = "Wet"))+ geom_point(aes(y = Dry, colour = "Dry"))

我的哑铃代码

df <- structure(list(State = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Dry", "Wet"), class = "factor"), Date = structure(c(3L, 3L, 5L, 5L, 4L, 4L, 3L, 3L, 5L, 5L, 4L, 4L, 3L, 3L, 5L, 5L, 4L, 4L), .Label = c("July-oct", "July-sept", "June-July", "June-oct", "June-sept"), class = "factor"),treatment = c("Weeding1", "Weeding1", "Weeding1", "Weeding1", "Weeding1", "Weeding1", "Weeding2", "Weeding2", "Weeding2", "Weeding2", "Weeding2",  "Weeding2", "Weeding0", "Weeding0", "Weeding0", "Weeding0",  "Weeding0", "Weeding0"), value = c(5.65, 4.88, 5.38, 3.93, 6.31, 3.24, 5.4, 4.73, 5.25, 3.9, 4, 3.07, 5.5, 4.7, 5.05, 3.78,4.86, 3.11),row.names = c(NA, -30L), .Names = c("State", "Date", "treatment", "value"), class = "data.frame"))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    将您的数据重塑为适当的整洁数据:

    data = structure(list(Weeding = c(5.65, 4.88, 5.38, 3.93, 5.5, 4.43, 
    7.03, 5.06, 6.31, 3.24, 5.4, 4.73, 5.25, 3.9, 5.5, 4.35, 6.46, 
    5.27, 4, 3.07, 5.5, 4.7, 5.05, 3.78, 5.33, 4.13, 6.77, 5.2, 4.86, 
    3.11), State = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L), .Label = c("Dry", "Wet"), class = "factor"), 
        Date = structure(c(3L, 3L, 5L, 5L, 2L, 2L, 1L, 1L, 4L, 4L, 
        3L, 3L, 5L, 5L, 2L, 2L, 1L, 1L, 4L, 4L, 3L, 3L, 5L, 5L, 2L, 
        2L, 1L, 1L, 4L, 4L), .Label = c("July-oct", "July-sept", 
        "June-July", "June-oct", "June-sept"), class = "factor"), 
        group = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
        2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
        0L, 0L, 0L)), .Names = c("Weeding", "State", "Date", "group"
    ), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
    "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", 
    "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", 
    "28", "29", "30"))
    

    然后我强制ggplot 将日期视为数字。

    ggplot(data) + aes(y = Weeding, x = as.numeric(Date), col = State) + geom_line() + facet_wrap(~group) + 
      scale_x_continuous(labels=c("1" = "July-oct", "2" = "July-sept","3" = "June-July", "4" = "June-oct", "5" = "June-sept"))
    

    但老实说,这听起来很奇怪,对我来说这样做是错误的。但这是你问的

    【讨论】:

    • 不是这样的。我重新排列了我的数据并尝试在 X 轴上使用日期。请参阅主帖上的编辑。谢谢
    • 您不能画线,因为date 是一个因子,而不是数字或日期。所以画线没有意义。
    • 我无法复制您的代码。我创建了与您完全相同的文件,但出现此错误。 >>f(..., self = self) 中的错误:中断和标签的长度不同此外:警告消息:在 eval(expr, envir, enclos) 中:强制引入的 NA。 >>imgur.com/a/xLbfT
    • 您没有正确加载数据。我编辑了我的答案。
    • 非常感谢您的快速回复。很抱歉一直打扰你。但是,如果我想删除几行,这似乎很复杂。例如,我需要从我的文件中删除所有三种治疗的 July-Sept 和 July-oct 行。我该如何快速完成?还有为什么我不能在excel中制作这个文件并在R中以csv格式导入它。谢谢。
    猜你喜欢
    • 2018-03-14
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2020-04-18
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多