如何将指南的标签转换为自定义格式?

时间:2014-02-06 14:38:35

标签: r plot gradient

我有这个代码来构建我的情节

这里是merged1.data

  

结构(列表(时间= c)(1391525413022,1391525438998,1391525440903,   1391525446466,1391525451770,1391525456050,1391525472635,1391525489437,   1391525502903,1391525512793,1391525524136,1391525549132,1391525562656,   1391525570842,1391525578937,1391525603513,1391525858760,1391525988388,   1391525991792,1391526065759),time_print = structure(c(1391525413,   1391525438,1391525440,1391525446,1391525451,1391525456,1391525472,   1391525489,1391525502,1391525512,1391525524,1391525549,1391525562,   1391525570,1391525578,1391525603,1391525858,1391525988,1391525991,   1391526065),class = c(“POSIXct”,“POSIXt”),tzone =“”),elapsed = c(2016,   1830,2494,2717,2837,1093,1216,2536,2656,2416,2093,2684,   1878,2808,2294,1179,1291,1166,1244,1039),threads = c(7,   7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7)版本=结构(c(1L,   1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,   1L,1L,1L),. Label =“025.00”,class =“factor”)),。Name = c(“time”,   “time_print”,“elapsed”,“threads”,“version”),row.names = c(NA,   -20L),class =“data.frame”)

这里是merged2.data

  

结构(列表(时间= c)(1391525413022,1391525438998,1391525440903,   1391525446466,1391525451770,1391525456050,1391525472635,1391525489437,   1391525502903,1391525512793,1391525524136,1391525549132,1391525562656,   1391525570842,1391525578937,1391525603513,1391525858760,1391525988388,   1391525991792,1391526065759),time_print = structure(c(1391525413,   1391525438,1391525440,1391525446,1391525451,1391525456,1391525472,   1391525489,1391525502,1391525512,1391525524,1391525549,1391525562,   1391525570,1391525578,1391525603,1391525858,1391525988,1391525991,   1391526065),class = c(“POSIXct”,“POSIXt”),tzone =“”),elapsed = c(2016,   1830,2494,2717,2837,1093,1216,2536,2656,2416,2093,2684,   1878,2808,2294,1179,1291,1166,1244,1039),threads = c(6,   6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6),版本=结构(c(1L,   1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,   1L,1L,1L),. Label =“025.02”,class =“factor”)),。Name = c(“time”,   “time_print”,“elapsed”,“threads”,“version”),row.names = c(NA,   -20L),class =“data.frame”)

和情节函数

combined_plot <- ggplot(merged1.data, aes(x=threads, y=elapsed)) + 
                     geom_point(aes(size = elapsed,shape=version, colour=time), show_guide = TRUE) +
                     scale_size(guide="none") + 
                     scale_color_gradient(high = "black", low="green", name="Time") + 
                     geom_point(data=merged2.data, aes(size = elapsed, shape=version, colour=time), show_guide = TRUE) + 
                     scale_shape_manual(values=c(15,17), name="Versions")

这里我正在绘制响应时间与当前运行的线程数量和渐变应该是2d屏幕上的第三个维度,以指示我们开始捕获数据的位置以及我们在哪里结束这样做。

time是一个时间戳,如1.391525e+12(在控制台中显示),当我使用此值计算渐变颜色时,它在图例上看起来很难看。

所以我想要的是根据这个数值列表计算渐变颜色,但是要将它们转换(例如,转换为人类可读的日期/时间)作为渐变图例上的标签。现在看起来如下图所示:

elapsed versus threads

1 个答案:

答案 0 :(得分:1)

在Cookbook中将格式化程序函数定义为shown

datetime_formatter <- function(x) {
  x <- as.POSIXct(x/1000, origin = "1970-01-01")  
  lab <- format(x, "%d.%m. %H:%M:%S")
  lab
}

combined_plot <- ggplot(merged1.data, aes(x=threads, y=elapsed)) + 
  geom_point(aes(size = elapsed,shape=version, colour=time), show_guide = TRUE) +
  scale_size(guide="none") + 
  scale_color_gradient(high = "black", low="green", name="Time", label=datetime_formatter) + 
  geom_point(data=merged2.data, aes(size = elapsed, shape=version, colour=time), show_guide = TRUE) + 
  scale_shape_manual(values=c(15,17), name="Versions")

print(combined_plot)

enter image description here

修改

以下是我认为提供此数据的更好方法(假设您的实际数据不如您在此处显示的那样冗余)。

DF <- rbind(merged1.data, merged2.data)
combined_plot <- ggplot(DF, 
                        aes(x=time_print, y=elapsed, colour=factor(threads), 
                            shape=version)) + 
                    geom_point(size=5)
print(combined_plot)

enter image description here

然而,如果没有所有事实,很难推荐替代品。