# # Written by: # -- # John L. Weatherwax 2009-04-21 # # email: wax@alum.mit.edu # # Please send comments and especially bug reports to the # above email address. # #----- # # EPage 139 # save_plots = F Table_6_8_DF = read.csv( "../../Data/US_attitudes.csv", header=TRUE ) # Produce a spaghetti plot for this data: # Table_6_8_DF_long = reshape( Table_6_8_DF, varying=c( "Year_1999_2000", "Summer_2000", "March_2003", "June_2003" ), v.names=c( "attitudes" ), timevar="time", idvar="country", direction="long" ) rownames(Table_6_8_DF_long) = NULL # Lets check our long format is "correct": # inds = Table_6_8_DF_long$country == "Great Britain" print( Table_6_8_DF_long[inds,] ) ## country time attitudes ## 2 Great Britain 1 83 ## 22 Great Britain 2 75 ## 42 Great Britain 3 48 ## 62 Great Britain 4 70 inds = Table_6_8_DF_long$country == "Pakistan" print( Table_6_8_DF_long[inds,] ) ## country time attitudes ## 18 Pakistan 1 23 ## 38 Pakistan 2 10 ## 58 Pakistan 3 NA ## 78 Pakistan 4 13 # We now have our data in a format we can use to make a spaghetti plot: # # http://www.ats.ucla.edu/stat/r/faq/spagplot.htm # if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter6/ex_7_spaghetti_plot.eps", onefile=FALSE, horizontal=FALSE) } interaction.plot(Table_6_8_DF_long$time, Table_6_8_DF_long$country, Table_6_8_DF_long$attitudes, xlab="time", ylab="country attitude", main="spaghetti plot of attitude by country", legend=F) if( save_plots ){ dev.off() }