【问题标题】:Line Plot in ggplot2 using Multiple data files使用多个数据文件在 ggplot2 中绘制线图
【发布时间】:2013-12-30 11:15:55
【问题描述】:

对象 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 来绘制我想要的图表。但我不知道如何使用 2 个不同的目标文件在 ggplot2 的单个图中绘制。

注意:我尝试将数据合并到一个对象中。但是由于列长度不匹配的不匹配而丢失了一些数据。

编辑:我是这样想的

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

我想在每个值中添加点,即在图表中的每个数据点中。 我该怎么做?

【问题讨论】:

    标签: r ggplot2 line


    【解决方案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))
    

    【讨论】:

    • 我已经这样做了 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") 。我需要更多的修改。我想在数据集中的每个数据点或值上都有一个点。我该怎么做?
    • @user3132179 不。我使用的是同一个对象,ggplot2 使用颜色 aes 自动拆分它。看看我的编辑点。
    • 好的,你的意思是说点是不可能的,我做对了吗?
    • @user3132179 不,我不是那个意思。我的意思是按我的方式做会更简单。
    猜你喜欢
    • 2021-11-04
    • 2023-04-08
    • 1970-01-01
    • 2014-06-18
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多