简体   繁体   中英

ggplot2: how do I add a second plot line

Ok, [R3.4.2 + ggplot2] Using the data example listed below, how do I add a second data plot? I tried this example which I found on this site;

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

The first plot has x=rpt$JulianDate, y=rpt1$w; and the second plot has x1= rpt1$JDAy and y2=rpt1$wolf)

The data (use dget(_) to read it):

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

In your comment, you say that rpt and rpt1 have the same data. Therefore, I think this is what you are asking for

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM