Return to Snippet

Revision: 33421
at October 8, 2010 14:57 by joelotz


Initial Code
# clear workspace
rm(list = ls())
 
# set libraries
library(ggplot2)
library(Cairo)
 
# read data
d <- read.csv("/eating_vs_sleeping.csv", header=TRUE, sep=",")
closeAllConnections()
 
# assign data
x <- data.frame(sleeping=d[,2], eating=d[,3])
 
# assign the labels
x$lab <- d[,1]
 
# plot it!
# CairoPNG('scatterplotExample.png', width=600, height=600) # for plotting
p <- ggplot(data=x, aes(x=sleeping, y=eating, label=lab))  +
    geom_point(size=5, color=rgb(192/255,80/255,77/255)) +  #creates "outside" of the circle
    geom_point(size=3, color="white") +             #creates "inside" of the circle
    geom_text(vjust=0, hjust=-0.2, size=5) +        #moves the label position
    xlab("Sleeping") + ylab("Eating and drinking") +
    xlim(460,540) + ylim(60,140)
 
p + opts(panel.background = theme_rect(fill = NA, colour = NA),
    panel.grid.minor = theme_line(colour = "grey", size = 0.2),
    panel.grid.major = theme_line(colour = "grey", size = 0.2),
    panel.border = theme_rect(fill = NA, colour = NA),
        plot.background = theme_rect(fill = NA, colour = NA),
    title = "Time spent eating and sleeping\n",
    plot.title = theme_text(size=16,hjust = 0.08),
    axis.line = theme_segment(colour = "grey20"),
    axis.title.x = theme_text(size = 13),
    axis.title.y = theme_text(size = 13, angle = -90),
    theme_text(family = 'courier') )
 
grid.text("2006, Minutes per day", x = unit(0.21,"npc"), y = unit(0.92, "npc"), just = "center",
         gp = gpar(col = "black", fontsize = 12))
# dev.off() # for plotting

Initial URL


Initial Description
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

Initial Title
Scatterplot Example

Initial Tags


Initial Language
R