Scatterplot Example


/ Published in: R
Save to your folder(s)

This is my R implementation of Paresh Shah's MS Excel chart found here -> http://www.visualquest.in/2010/09/severalsimple-and-very-useful.html


Copy this code and paste it in your HTML
  1. # clear workspace
  2. rm(list = ls())
  3.  
  4. # set libraries
  5. library(ggplot2)
  6. library(Cairo)
  7.  
  8. # read data
  9. d <- read.csv("/eating_vs_sleeping.csv", header=TRUE, sep=",")
  10. closeAllConnections()
  11.  
  12. # assign data
  13. x <- data.frame(sleeping=d[,2], eating=d[,3])
  14.  
  15. # assign the labels
  16. x$lab <- d[,1]
  17.  
  18. # plot it!
  19. # CairoPNG('scatterplotExample.png', width=600, height=600) # for plotting
  20. p <- ggplot(data=x, aes(x=sleeping, y=eating, label=lab)) +
  21. geom_point(size=5, color=rgb(192/255,80/255,77/255)) + #creates "outside" of the circle
  22. geom_point(size=3, color="white") + #creates "inside" of the circle
  23. geom_text(vjust=0, hjust=-0.2, size=5) + #moves the label position
  24. xlab("Sleeping") + ylab("Eating and drinking") +
  25. xlim(460,540) + ylim(60,140)
  26.  
  27. p + opts(panel.background = theme_rect(fill = NA, colour = NA),
  28. panel.grid.minor = theme_line(colour = "grey", size = 0.2),
  29. panel.grid.major = theme_line(colour = "grey", size = 0.2),
  30. panel.border = theme_rect(fill = NA, colour = NA),
  31. plot.background = theme_rect(fill = NA, colour = NA),
  32. title = "Time spent eating and sleeping\n",
  33. plot.title = theme_text(size=16,hjust = 0.08),
  34. axis.line = theme_segment(colour = "grey20"),
  35. axis.title.x = theme_text(size = 13),
  36. axis.title.y = theme_text(size = 13, angle = -90),
  37. theme_text(family = 'courier') )
  38.  
  39. grid.text("2006, Minutes per day", x = unit(0.21,"npc"), y = unit(0.92, "npc"), just = "center",
  40. gp = gpar(col = "black", fontsize = 12))
  41. # dev.off() # for plotting

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.