ggplot2:如何添加第二个绘图线

时间:2016-01-23 03:18:49

标签: r csv plot

确定, [R3.4.2 + ggplot2] 使用下面列出的数据示例,如何添加第二个数据图?我试过这个我在这个网站上找到的例子;

library(ggplot2]
** This is part of the origanl code ****
rpt<-read.csv(file="rpt.csv,header=T)
rpt1<-read.csv(file="rpt1.csv,header=T)
*** code starts here *****
ggplot(rpt,aes(JulianDate,w)) + geom_line(aes(color="First   line")) +
             geom_line(data=rpt1, aes(color="Second line")) + labs(color="Legend text")

第一个图有x = rpt $ JulianDate,y = rpt1 $ w;第二个图有x1 = rpt1 $ JDAy和y2 = rpt1 $ wolf)

数据(使用dget(_)读取它):

structure(list(
JDay = c(57023, 57024, 57027, 57028, 57029, 57031, 57032, 57035, 57037),
Obs = c(1, 1, 1, 1, 1, 1, 1, 1, 1),
w = c(71, 105, 64, 44, 45, 38, 66, 49, 28),
WStd = c(0, 0, 0, 0, 0, 0, 0, 0, 0),
wolf = c(91.59, 135.45, 82.56, 56.76, 58.05, 49.02, 85.14, 63.21, 36.12),
Adj = c(0, 0, 0, 0, 0, 0, 0, 0, 0)),
.Names = c("JDay", "Obs", "w", "WStd", "wolf", "Adj"),
class = "data.frame",
row.names =  c(NA, -9L))

1 个答案:

答案 0 :(得分:0)

在评论中,您说rptrpt1具有相同的数据。因此,我认为这就是你所要求的

library(ggplot2)
ggplot(rpt, aes(x=JDay)) + 
  geom_line(aes(y=w, color="First   line")) +
  geom_line(aes(y=wolf, color="Second line")) + 
  labs(color="Legend text")

enter image description here