geom_point用于添加带有线条的形状

时间:2015-03-06 19:19:35

标签: r

我正在尝试绘制一个包含两个变量的数据集,其中一个是一个因子。我想绘制第一个和它们的时间序列,我想根据另一个变量添加形状。一个数据样本如下:

data<-dput(df)
structure(list(date = structure(c(16222, 16223, 16224, 16225, 
16226, 16227, 16228, 16229, 16230, 16231, 16232, 16233, 16234, 
16235, 16236, 16237, 16238, 16239, 16240, 16241, 16242, 16243, 
16244, 16245, 16246, 16247, 16248, 16249, 16250, 16251, 16222, 
16223, 16224, 16225, 16226, 16227, 16228, 16229, 16230, 16231, 
16232, 16233, 16234, 16235, 16236, 16237, 16238, 16239, 16240, 
16241, 16242, 16243, 16244, 16245, 16246, 16247, 16248, 16249, 
16250, 16251), class = "Date"), variable = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Max_MC042", 
"num.mat"), class = "factor"), value = c(47.0650382725,      42.1888689548142, 
38.127626260776, 51.8085414143142, 34.4172440113333, 56.574347284566, 
71.2399296488298, 76.9233328034323, 69.6930713630278, 68.8762542598976, 
46.8176020713247, 38.7843392530365, 30.009713269441, 31.4613202695885, 
41.486617878276, 42.2721220091406, 40.0745304951927, 44.0143880374705, 
25.5258039194635, 27.6989381602917, 24.0256586862847, 35.0593654842014, 
40.5504283288785, 29.1995231111962, 15.3054932857274, 26.5951353410035, 
50.2912910172587, 52.5364360635746, 29.7705762576771, 35.6527102804705, 
10, 10, 11, 11, 9, 10, 10, 10, 10, 10, 10, 10, 10, 1, 10, 11, 
2, 10, 8, 8, 8, 8, 8, 11, 2, 10, 11, 9, 9, 8)), .Names = c("date", 
"variable", "value"), row.names = c(NA, -60L), class = "data.frame")

然后,我正在尝试:

pl <-ggplot() + 
     geom_line(data=df[df$variable=="Max_MC042",],
               aes(date,y=value,group = variable,colour=variable)) +
               scale_colour_manual(values=c("red"))+
               labs(x = "days", y = "ppb") + theme_bw()+
scale_x_date(labels = date_format("%m %b"))

添加geom_point:

pl+pl+geom_point(aes(shape = factor(df[df$variable=="num.mat",]))) + 
  scale_shape(solid = FALSE)

我有点失落,我不确定最好的方式是什么。 另外,我想问一下,如果因子变量是字符(然后为每个变量设置一个形状),它是否可以工作?

1 个答案:

答案 0 :(得分:0)

您的解释和代码有点令人困惑,但这是您之后的目标吗?

df <- tidyr::spread(df, variable, value)
gg <- ggplot(data=df)
gg <- gg + geom_line(aes(x=date, y=Max_MC042), color="red")
gg <- gg + geom_point(aes(x=date, y=Max_MC042, shape=factor(num.mat)))
gg <- gg + labs(x="days", y="ppb")
gg <- gg + scale_x_date(labels=date_format("%m %b"))
gg <- gg + scale_shape(solid=FALSE)
gg <- gg + theme_bw()
gg

enter image description here