经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » R语言 » 查看文章
ggplot2 aes函数map到data笔记
来源:cnblogs  作者:ywliao  时间:2018/9/25 20:21:08  对本文有异议
  1. .all_aesthetics <- c("adj", "alpha", "angle", "bg", "cex", "col", "color", "colour", "fg", "fill", "group", "hjust", "label", "linetype", "lower",
  2. "lty", "lwd", "max", "middle", "min", "pch", "radius", "sample", "shape","size", "srt", "upper", "vjust", "weight", "width", "x", "xend", "xmax",
  3. "xmin", "xintercept", "y", "yend", "ymax", "ymin", "yintercept", "z")
  4. .base_to_ggplot <- c(
  5. "col" = "colour",
  6. "color" = "colour",
  7. "pch" = "shape",
  8. "cex" = "size",
  9. "lty" = "linetype",
  10. "lwd" = "size",
  11. "srt" = "angle",
  12. "adj" = "hjust",
  13. "bg" = "fill",
  14. "fg" = "colour",
  15. "min" = "ymin",
  16. "max" = "ymax"
  17. )
  18. aes <- function(x, y, ...) {
  19. aes <- structure(as.list(match.call()[-1]), class = "uneval")
  20. rename_aes(aes)
  21. }
  22. #' @export
  23. print.uneval <- function(x, ...) {
  24. values <- vapply(x, deparse2, character(1))
  25. bullets <- paste0("* ", format(names(x)), " -> ", values, "\n")
  26. cat(bullets, sep = "")
  27. }
  28. # Rename American or old-style aesthetics name
  29. rename_aes <- function(x) {
  30. # Convert prefixes to full names
  31. full <- match(names(x), .all_aesthetics)
  32. names(x)[!is.na(full)] <- .all_aesthetics[full[!is.na(full)]]
  33. plyr::rename(x, .base_to_ggplot, warn_missing = FALSE)
  34. }
  35. deparse2 <- function(x) {
  36. y <- deparse(x, backtick = TRUE)
  37. if (length(y) == 1) {
  38. y
  39. } else {
  40. paste0(y[[1]], "...")
  41. }
  42. }
  43. #map to data (just an example)
  44. data <- lapply(aes(x=mpg, y=cyl, color=cyl),eval, env=mtcars) #from compute_aesthetics function
  45. data

结果

  1. $x
  2. [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4
  3. [29] 15.8 19.7 15.0 21.4
  4. $y
  5. [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
  6. $colour
  7. [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

参考资料

ggplot2源代码

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号