使用带有时间轴的ggplot绘制多行

时间:2017-11-05 11:37:58

标签: r ggplot2

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

除草0,1和2是治疗方法。日期是指他们被放置在研究地点的时间。因此,6月至7月意味着样品已于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"))

我的DUMBELL代码

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

1 个答案:

答案 0 :(得分:0)

将数据重新整理为适当的整洁数据:

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

enter image description here

但是,这样做对我来说是个奇怪和错误的声音。但这就是你问的问题

相关问题