-3

我有一个代码可以在 ggplot 中绘制多个变量和因子。我想将线条类型从简单线更改为破折号或点之类的东西,以更好地表示变量。此外,现在它们以不同的颜色呈现。

代码:

df <- data.frame(z25$avg,z28$avg,z29$avg,z31$avg,z32$avg,sdtt$dt,znode25$outside)
names(df) <-  c("Node 25","Node 28","Node 29","Node 31","Node 32","Time_Date", "Results")
df.m <- melt(df, names(df)[6:7], names(df)[1:5])
df.m$Results <- factor(df.m$Results)
df.m$Time_Date <- strptime(as.character(df.m$Time_Date), format = "%Y-%m-%d %H:%M:%S")
p <- ggplot(df.m, aes(x = Time_Date, y = value, group = variable, color = variable))
p <- p + geom_point(aes(shape = Results), cex=3)
p <- p + scale_shape_manual(values=c(15,2))
p <- p + geom_line()
p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1))
p <- p + scale_color_manual(values=c("#000099","red","green",'blue',"yellow"))
p <- p + ylim(-1,8)
p <- p + xlab('Date and Time') 
p <- p + ylab('Temprature') 
p <- p + ggtitle("Spatial Outliers of Node 25 ") + theme(plot.title = element_text(lineheight=3, face="bold", color="black"))
p

阴谋: 在此处输入图像描述

样本数据df.m:

            Time_Date Results variable       value
1 2007-09-30 01:00:00       1  Node 25 -0.35333333
2 2007-09-30 02:00:00       1  Node 25 -0.42333333
3 2007-09-30 03:00:00       1  Node 25 -0.73333333
4 2007-09-30 04:00:00       1  Node 25 -0.65000000
5 2007-09-30 05:00:00       1  Node 25 -0.40000000
6 2007-09-30 06:00:00       1  Node 25 -0.09166667

样本数据df:

      Node 25     Node 28   Node 29    Node 31     Node 32           Time_Date Results
1 -0.35333333 -0.21000000 0.1433333 -0.3533333 -0.19833333 2007-09-30 01:00:00       1
2 -0.42333333 -0.37500000 0.2783333 -0.4850000 -0.25000000 2007-09-30 02:00:00       1
3 -0.73333333 -0.62166667 0.2466667 -0.7650000 -0.56500000 2007-09-30 03:00:00       1
4 -0.65000000 -0.58833333 0.3400000 -0.7150000 -0.45166667 2007-09-30 04:00:00       1
5 -0.40000000 -0.34333333 0.5900000 -0.4666667 -0.21333333 2007-09-30 05:00:00       1
6 -0.09166667 -0.02666667 0.8400000 -0.1666667  0.09666667 2007-09-30 06:00:00       1
4

1 回答 1

3

感谢 user1317221_G :

znode25 = data.frame(second_data_results_node25[2:24,])

df <- data.frame(z25$avg,z28$avg,z29$avg,z31$avg,z32$avg,sdtt$dt,znode25$outside)
names(df) <-  c("Node 25","Node 28","Node 29","Node 31","Node 32","Time_Date", "Results")
df.m <- melt(df, names(df)[6:7], names(df)[1:5])
df.m$Results <- factor(df.m$Results)
df.m$Time_Date <- strptime(as.character(df.m$Time_Date), format = "%Y-%m-%d %H:%M:%S")
p <- ggplot(df.m, aes(x = Time_Date, y = value, group = variable, color = variable, linetype=variable))
p <- p + geom_point(aes(shape = Results), cex=4)
p <- p + scale_shape_manual(values=c(22,20))
p <- p + geom_line()
p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1))
p <- p + scale_color_manual(values=c("#000099","red","darkgray",'blue',"darkred"))
p <- p + ylim(-1,8)
p <- p + xlab('Date and Time') 
p <- p + ylab('Temprature') 
p <- p + ggtitle("Spatial Outliers of Node 25 ") + theme(plot.title = element_text(lineheight=3, face="bold", color="black"))
p
于 2013-01-16T15:45:26.357 回答