在旧数据和更新数据之间绘制不同颜色的图表

时间:2017-07-12 06:27:19

标签: r scatter-plot

我重新提出了我的问题,希望现在有点清楚了:

         Here is my data:
            ID  Type        X               Y       Sex
            a1  Test    -12.12609861    208.6810478 XY
            a2  Test    -1.32366642     63.0574351  XXY
            a3  Test    -9.02867948     114.1501293 XY
            b4  NewTest 0.01101428      0.87207664  XX
            b5  Test    -1.14651604     -0.86714741 XX
            b6  Test    -13.05848944    155.5109551 XY
            x7  NewTest -4.74479593     80.82528931 XY
            x8  Test    -8.17386444     124.4765311 XY
            x9  Test    1.14870262      -0.36606683 XX
            x10 Test    1.20879037      0.80972607  XX
            x11 Test    -1.04261274     0.35654895  XX
            x12 Test    -11.73602       185.5326725 XY

我想根据数据是新数据还是旧数据来绘制不同颜色的数据。 每天或每周添加新数据,因此颜色变化需要是动态的。 N.B新数据始终以“TYPE”

列中的“newTest”开头

代码:

     for_loop_start<- (nrow(whole_data)-1)
            len_of_whole_data<- nrow(whole_data)
            for (j in c(for_loop_start:1)){

              if (whole_data[j,2] == "NewTest"){

                break
              }

            }
            new_data <- with(whole_data,whole_data[j:len_of_whole_data,])
  > p <- ggplot(data=whole_data,aes(x=X,y=Y)) +  geom_point(colour = "black")
  > ggplotly(p)
  > p <- p + geom_point(data= new_data,
              mapping=aes(x=X,y=Y,text=SampleID,colour = "darkgoldenrod2"))
  > ggplotly(p)

2 个答案:

答案 0 :(得分:1)

回答问题的编辑版本

如果“类型”列中的最后一个“NewType”值始终启动“新数据”,则应该有效:

dat <- structure(list(ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 10L, 
11L, 12L, 7L, 8L, 9L), .Label = c("a1", "a2", "a3", "b4", "b5", 
"b6", "x10", "x11", "x12", "x7", "x8", "x9"), class = "factor"), 
    Type = structure(c(2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("NewTest", "Test"), class = "factor"), 
    X = c(-12.12609861, -1.32366642, -9.02867948, 0.01101428, 
    -1.14651604, -13.05848944, -4.74479593, -8.17386444, 1.14870262, 
    1.20879037, -1.04261274, -11.73602), Y = c(208.6810478, 63.0574351, 
    114.1501293, 0.87207664, -0.86714741, 155.5109551, 80.82528931, 
    124.4765311, -0.36606683, 0.80972607, 0.35654895, 185.5326725
    ), Sex = structure(c(3L, 2L, 3L, 1L, 1L, 3L, 3L, 3L, 1L, 
    1L, 1L, 3L), .Label = c("XX", "XXY", "XY"), class = "factor")), .Names = c("ID", 
"Type", "X", "Y", "Sex"), class = "data.frame", row.names = c(NA, 
-12L))

lim.id <- max(which(dat$Type == "NewTest")) - 1

dat$Age <- c(rep("old", lim.id), rep("new", nrow(dat) - lim.id))

ggplot(dat, aes(x=X, y=Y, color = Age)) + geom_point() +
scale_color_manual(values = c("darkgoldenrod2", "black"))

enter image description here

旧答案

您可以尝试创建一个读取修改时间的脚本(请参阅?file.mtime)并使用该脚本创建一个列,该列指定条目是“新”还是“旧”

dat <- structure(list(ID = 1:12, Type = structure(c(2L, 2L, 2L, 2L, 
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("control", "Test"
), class = "factor"), X = c(-12.12609861, -1.32366642, -9.02867948, 
0.01101428, -1.14651604, -13.05848944, -4.74479593, -8.17386444, 
1.14870262, 1.20879037, -1.04261274, -11.73602), Y = c(208.6810478, 
63.0574351, 114.1501293, 0.87207664, -0.86714741, 155.5109551, 
80.82528931, 124.4765311, -0.36606683, 0.80972607, 0.35654895, 
185.5326725), Sex = structure(c(3L, 2L, 3L, 1L, 1L, 3L, 3L, 3L, 
1L, 1L, 1L, 3L), .Label = c("XX", "XXY", "XY"), class = "factor")), .Names = c("ID", 
"Type", "X", "Y", "Sex"), class = "data.frame", row.names = c(NA, 
-12L))

dat$Time <- seq(as.Date("2017-07-12"), as.Date("2017-06-12"), length = nrow(dat))
dat$Time.type <- ifelse(as.Date(Sys.time()) - dat$Time < 12, "new", "old")

library(ggplot2)    
ggplot(dat, aes(x=X, y=Y, color = Time.type)) + geom_point() +
scale_color_manual(values = c("black", "darkgoldenrod2"))

答案 1 :(得分:0)

你也可以设置一个变量来定义哪个ID来分割你的数据帧以进行绘图(假设df1是你的数据帧):

lim.id <- 7 #here you can put whatever value you would like to split your data.frame on
plot1 <- ggplot() +
    geom_point(data = df1[df1$ID < lim.id, ], aes(x = X, y = Y), colour =  "black")
plot1 <- plot1 +
    geom_point(data = df1[df1$ID >= lim.id, ], aes(x = X, y = Y), colour = "darkgoldenrod2")
plot2 <- ggplotly(plot2)