Skip to content

Instantly share code, notes, and snippets.

@jacoby
Created December 6, 2012 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacoby/4225023 to your computer and use it in GitHub Desktop.
Save jacoby/4225023 to your computer and use it in GitHub Desktop.
Making a PNG plot with R and libCairo
# R file template in Template-Toolkit
library( Cairo )
w_avg <- c( 270.0000,268.5000,268.0000,268.0000,268.0000,267.6667,267.5714,267.5000,267.1111,267.4000,267.5455,267.5000,267.3846,267.3571,267.4667,267.1250,266.8824,266.7778,266.6316,266.3684,266.2632,266.1053,265.7895,265.7000,265.5000,265.5000,265.4000,265.2381,265.1000,264.7000,264.4000,264.2000,264.0500,263.8500,263.6000,263.6190,263.6190,263.5238,263.3333,263.2857,263.2500,263.2000,263.1000,262.8500,262.6000,262.2105,262.2632,262.1667,262.1111,262.0556,262.0556,261.9444,261.6471,261.3529,261.2353,261.0000,260.8889,260.5000,260.2778,260.2778,260.2941,260.1667,260.0556,259.8333,259.7368,259.5000,259.3500,259.1500,258.8500,258.5714,258.5455,258.4545,258.4783,258.4783,258.4091,258.4091,258.3636,258.4091,258.3182,258.2083,258.1250,258.1667,258.2308,258.1538,258.0769,257.9615,257.8800,257.7500,257.7391,257.6667,257.5833,257.4167,257.2174,257.2174,257.0870,257.0000,256.7391,256.6087,256.5238,256.3636,255.9524,255.6818,255.2609,255.0833,254.9583,254.8400,254.7692,254.5200,254.4400,254.0833,253.9583,254.0800,253.9200,253.5000,253.3333,253.2917,253.1250,252.9600,252.6538,252.3200,252.2692,252.1600,252.2917,252.1304,252.0870,252.1304,252.1304,252.1739,252.0870,252.0870,251.6000,251.5714,251.6429,251.3846,251.1538,250.8462,250.5000,250.0000,249.3333,248.5833,248.1667,247.6667,247.5385,247.7143,247.7333,247.7500,247.7647,247.7222,247.6842,247.6500,247.5000,247.4000,247.1000,246.9500,246.8500,246.6000,246.5238,246.3810,246.2857,246.3333,246.3333,246.1905,246.1905,246.0952,245.8571,245.5909,245.5455,245.5000,245.4545,245.3043,245.0870,244.9565,244.9130,244.8696,244.8261,244.7826,244.5217,244.3478,244.2609,244.2174,244.1304,243.8750,243.7083,243.5833,243.4583,243.3750,243.2917,243.2174,243.0870,242.9565,242.8261,242.8696,242.9130,242.8696,242.7826,242.6087,242.1818,242.0909,241.9524,241.7143,241.5714,241.2500,241.1000,241.0000,240.8571,240.6667,240.4286,240.1905,239.8571,239.5238,239.3333,239.0952,238.8571,238.6667,238.5714,238.3810,238.3182,238.3182,238.2727,238.2174,238.1364,238.0000,238.0000,238.0000,237.9091,237.6667,237.6190,237.6667,237.7619,237.8571,238.0952,238.0952,238.0952,238.1429,238.1429,238.0000,238.1429,238.1905,238.1429,238.0476,238.0000,238.0000,238.0000,238.0000,237.9500,238.0000,238.1000,238.0455,237.8182,237.7273,237.4545,237.3636 )
date <- c( "2012-02-23","2012-02-24","2012-02-25","2012-02-26","2012-02-28","2012-02-29","2012-03-01","2012-03-03","2012-03-04","2012-03-05","2012-03-06","2012-03-07","2012-03-08","2012-03-09","2012-03-10","2012-03-12","2012-03-13","2012-03-14","2012-03-15","2012-03-16","2012-03-17","2012-03-18","2012-03-19","2012-03-20","2012-03-21","2012-03-22","2012-03-23","2012-03-24","2012-03-26","2012-03-27","2012-03-28","2012-03-29","2012-03-30","2012-03-31","2012-04-01","2012-04-02","2012-04-03","2012-04-04","2012-04-05","2012-04-06","2012-04-08","2012-04-09","2012-04-10","2012-04-11","2012-04-12","2012-04-14","2012-04-16","2012-04-18","2012-04-19","2012-04-20","2012-04-21","2012-04-22","2012-04-24","2012-04-26","2012-04-27","2012-04-28","2012-04-29","2012-04-30","2012-05-01","2012-05-02","2012-05-04","2012-05-05","2012-05-07","2012-05-08","2012-05-09","2012-05-10","2012-05-11","2012-05-12","2012-05-13","2012-05-14","2012-05-15","2012-05-16","2012-05-17","2012-05-18","2012-05-20","2012-05-21","2012-05-22","2012-05-23","2012-05-24","2012-05-25","2012-05-26","2012-05-27","2012-05-28","2012-05-29","2012-05-30","2012-05-31","2012-06-01","2012-06-04","2012-06-05","2012-06-06","2012-06-07","2012-06-08","2012-06-09","2012-06-11","2012-06-12","2012-06-13","2012-06-14","2012-06-15","2012-06-17","2012-06-18","2012-06-19","2012-06-20","2012-06-21","2012-06-22","2012-06-23","2012-06-24","2012-06-25","2012-06-26","2012-06-27","2012-06-28","2012-06-29","2012-06-30","2012-07-02","2012-07-04","2012-07-05","2012-07-06","2012-07-07","2012-07-08","2012-07-09","2012-07-10","2012-07-11","2012-07-12","2012-07-13","2012-07-14","2012-07-15","2012-07-16","2012-07-17","2012-07-18","2012-07-19","2012-07-20","2012-07-30","2012-07-31","2012-08-01","2012-08-02","2012-08-03","2012-08-04","2012-08-06","2012-08-07","2012-08-08","2012-08-09","2012-08-10","2012-08-11","2012-08-12","2012-08-13","2012-08-14","2012-08-15","2012-08-16","2012-08-17","2012-08-19","2012-08-20","2012-08-21","2012-08-22","2012-08-23","2012-08-24","2012-08-25","2012-08-26","2012-08-27","2012-08-28","2012-08-29","2012-08-30","2012-08-31","2012-09-01","2012-09-02","2012-09-03","2012-09-04","2012-09-05","2012-09-06","2012-09-07","2012-09-08","2012-09-09","2012-09-10","2012-09-11","2012-09-12","2012-09-13","2012-09-14","2012-09-15","2012-09-16","2012-09-17","2012-09-18","2012-09-19","2012-09-20","2012-09-21","2012-09-22","2012-09-23","2012-09-24","2012-09-25","2012-09-26","2012-09-27","2012-09-28","2012-09-29","2012-09-30","2012-10-01","2012-10-02","2012-10-03","2012-10-04","2012-10-05","2012-10-07","2012-10-08","2012-10-10","2012-10-11","2012-10-12","2012-10-13","2012-10-14","2012-10-15","2012-10-16","2012-10-17","2012-10-18","2012-10-19","2012-10-20","2012-10-21","2012-10-22","2012-10-23","2012-10-24","2012-10-25","2012-10-26","2012-10-27","2012-10-28","2012-10-29","2012-10-30","2012-10-31","2012-11-02","2012-11-03","2012-11-04","2012-11-05","2012-11-06","2012-11-07","2012-11-08","2012-11-09","2012-11-10","2012-11-11","2012-11-12","2012-11-13","2012-11-14","2012-11-15","2012-11-16","2012-11-17","2012-11-18","2012-11-19","2012-11-20","2012-11-21","2012-11-23","2012-11-25","2012-11-26","2012-11-27","2012-11-28","2012-11-29","2012-11-30","2012-12-01","2012-12-02","2012-12-03","2012-12-04","2012-12-05" )
w_count <- c( 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,19,19,19,19,20,20,20,20,21,20,20,20,20,20,20,20,21,21,21,21,21,20,20,20,20,20,19,19,18,18,18,18,18,17,17,17,17,18,18,18,18,17,18,18,18,19,20,20,20,20,21,22,22,23,23,22,22,22,22,22,24,24,24,26,26,26,26,25,24,23,24,24,24,23,23,23,23,23,23,21,22,21,22,23,24,24,25,26,25,25,24,24,25,25,24,24,24,24,25,26,25,26,25,24,23,23,23,23,23,23,23,15,14,14,13,13,13,12,12,12,12,12,12,13,14,15,16,17,18,19,20,20,20,20,20,20,20,21,21,21,21,21,21,21,21,21,22,22,22,22,23,23,23,23,23,23,23,23,23,23,23,23,24,24,24,24,24,24,23,23,23,23,23,23,23,23,23,22,22,21,21,21,20,20,20,21,21,21,21,21,21,21,21,21,21,21,21,22,22,22,23,22,22,22,22,22,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,20,20,20,20,20,20,22,22,22,22,22 )
weight <- c( 270,267,267,268,268,266,267,267,264,270,269,267,266,267,269,262,263,265,264,265,265,264,262,264,264,266,265,262,263,262,263,263,263,263,264,264,262,261,261,263,266,263,260,259,259,261,263,261,262,262,263,261,261,259,259,259,259,259,259,260,258,258,259,259,258,258,259,258,257,257,258,259,259,259,258,259,258,260,258,257,256,259,259,257,257,255,256,257,256,257,257,255,256,258,256,256,254,255,255,256,253,253,251,253,253,252,253,252,254,251,254,256,252,250,252,253,251,249,250,251,252,252,254,250,252,253,253,253,252,251,249,248,252,249,249,250,247,247,245,244,247,245,246,250,248,248,248,247,247,247,246,246,246,246,247,245,245,244,245,246,244,244,245,244,245,244,247,247,246,242,242,244,245,245,245,245,241,241,243,243,243,242,240,241,242,242,243,243,244,244,243,243,243,243,243,241,238,239,239,238,240,236,237,239,240,238,238,238,237,237,239,238,238,239,241,237,237,238,238,237,237,237,236,237,237,237,237,239,240,239,242,239,238,239,239,238,240,238,237,236,236,236,236,237,236,238,239,238,235,237,236,237 )
obese <- c( 240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240 )
names <- c( "Weight" , "Average" , "Overweight/Obese" )
p_cols <- c( "blue" , "red" , "green" )
legend_y <- 250
legend_x <- 1
CairoPNG(
filename="data.png" ,
width = 600 ,
height = 400 ,
pointsize = 12
)
plot (
weight ,
col=p_cols[1] ,
type="l" ,
xlab="time" ,
ylab="weight in lbs"
)
lines(
w_avg ,
col=p_cols[2] ,
type="l"
)
lines(
obese ,
col=p_cols[3] ,
type="l"
)
legend(
legend_x, legend_y, names, col=p_cols ,
cex=0.8 , lty=c(1,1,1)
)
box()
title( main="Weight Record - Dave Jacoby" )
graphics.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment