对象1:
structure(list(Date = structure(c(16026, 16027, 16028, 16029,
16030, 16031, 16032, 16034, 16035, 16036, 16037, 16038, 16039,
16040, 16041, 16042, 16043, 16044, 16045, 16046, 16048, 16049,
16050, 16051, 16052, 16053, 16055, 16056), class = "Date"), Catagory = 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), .Label = c("build",
"client"), class = "factor"), User_Name = c(1L, 5L, 6L, 6L, 6L,
7L, 5L, 5L, 3L, 5L, 2L, 4L, 5L, 1L, 6L, 4L, 5L, 4L, 6L, 5L, 12L,
4L, 4L, 3L, 5L, 5L, 3L, 3L)), .Names = c("Date", "Catagory",
"User_Name"), row.names = c(NA, 28L), class = "data.frame")
对象2:
structure(list(Date = structure(c(16026, 16027, 16028, 16029,
16030, 16031, 16032, 16034, 16035, 16036, 16037, 16038, 16039,
16041, 16042, 16043, 16044, 16045, 16046, 16048, 16049, 16050,
16051, 16052, 16053, 16055, 16056), class = "Date"), Catagory = 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), .Label = c("build",
"client"), class = "factor"), User_Name = c(1L, 4L, 6L, 6L, 5L,
7L, 5L, 5L, 3L, 5L, 2L, 4L, 5L, 2L, 3L, 2L, 2L, 5L, 5L, 7L, 3L,
4L, 3L, 4L, 3L, 2L, 2L)), .Names = c("Date", "Catagory", "User_Name"
), row.names = c(NA, 27L), class = "data.frame")
这里我想绘制单线图,其中x轴表示时间,y轴表示变量User_Name。两个对象中的时间数据几乎相同,而User_Name计数不同。我试图使用ggplot2来使用我想要的图表。但我不知道如何在ggplot2的单个图中使用2个不同的目标文件进行绘图。
注意:我尝试将数据合并到一个对象中。但由于色谱柱长度不匹配不匹配,一些数据会被遗漏。
编辑:我这样想了
ggplot()+geom_line(data=build_9,aes(x=Date,y=User_Name),color="red")+geom_line(data=build_10,aes (x=Date,y=User_Name),color="blue")
我想在每个值中添加点,即图中每个数据点。 我该怎么办?
答案 0 :(得分:1)
这里不需要合并,只需为每个对象(data.frame)添加一列来表征它。
obj1$type <- 'obj1'
obj2$type <- 'obj2'
dat <- rbind(obj1,obj2)
然后使用ggplot
即可:
ggplot(dat,aes(x=Date,y=User_Name))+
geom_point()+
geom_line(aes(color=type))