.chapter10<-function(i=0){ " i Chapter 10: Simple graphs i Explanations - ------------------------------------- -- ----------------------------- 1 A simple x, y graph 21 Saving our plot: click graph 2 Column 22 : pdf() 3 Line 23 : snipping tool 4 Pie and Bar 24 : paint software 5 histogram 25 : ggsave() 6 Area 26 7 (x,y) scatter 27 8 stock chart 28 9 Present your data in a surface chart 29 10 Doughnut 30 11 Present your data in a bubble chart 31 12 Present your data in a radar chart 32 13 Other charts 33 14 Win/loss 34 15 one graph: 2 sets of lines (x,y) and (a,b) 35 animation in R 16 Macro 4 pie 36 : one example 17 Macro 4 x,y 37 : Manipulate data 18 Macro 4 more......... 38 : stock returns comparison 19 Videos 39 : GDP dataset and picture 20 Links 40 : GPD animation Example #1:>.c10 # find out the list Example #2:>.c10() # the same as the above Example #3:>.c10(1) # see the first explanation ";.zchapter10(i)} .n10chapter<-40 .zchapter10<-function(i){ if(i==0){ print(.c10) }else{ .printEachQ(10,i,.n10chapter) } } .c10<-.chapter10 .C10EXPLAIN1<-"A simple x, y graph (x as horizontal axis while y is for vertical axis) //////////////////////////// Assume that we have the following two columns. where y= 3+3x x y 1 6 2 9 3 12 4 15 5 18 6 21 7 24 8 27 9 30 10 33 11 36 Step 1: highlight both columns Step 2: Click \"Insert\" -> Line -> All chart types Step 3: choose \"X Y (scatter\" Step 4: click \" OK\" //////////////////////////// " .C10EXPLAIN2<-" Column. Assume that we have the following data //////////////////////////// A,B,C 10,12,30 Highlight our data -> Insert -> column //////////////////////////// " .C10EXPLAIN3<-" line //////////////////////////// Assume that we have the following two columns. x,y 1,6 2,9 3,12 4,15 5,17 Step 1: highlight the above data Step 2: go to Excel and paste Step 3: If they occupy just one column, highlight this column Step 4: Click \"Data\", -> Text to column -> use comma as delimiter Step 5: highlight both columns Step 6: Click \"Insert\" -> Line [note: we will see two lines. This example is different from (x,y) scatter which has one line. ] //////////////////////////// " .C10EXPLAIN4<-" Pie and Bar //////////////////////////// Assume that we have the following two columns. x,10 y,20 z,40 a,30 b,10 Step 1: high-light both columns Step 2: insert -> Pie Bar ----------------- Assume that we have the following frequencies for different excuses for late for a class. excuses for late,freq trafic jam,30 sick,10 dog or pet,5 family member,10 others,20 Step 1: high-light both columns Step 2: instert -> Bar [note: we should not choose the last one!] //////////////////////////// " .C10EXPLAIN5<-"Histogram //////////////////////////// Assume that we have the following grades and their corresponding frequencies. We want to generate a histogram. Grade Freq 90 10 80 23 70 24 60 5 50 2 Step 1: Click \"Data\" -> Data Analysis -> Histogram Step 2: For [Input range] choose the second column (including cell of \"Freq\") Step 3: For [Bin Range], choose the first column (including the cell of \"Grade\") Step 4: check \"Labels\" Step 5: choose a location on the same spreadsheet or a new one Step 6: choose Pareto (sorted histogram) Cumulative Percentage Chart output Step 7: click \"OK\" //////////////////////////// " .C10EXPLAIN6<-" Draw a area //////////////////////////// Assume that we have the following two columns. x,10 y,20 z,40 a,30 b,10 Step 1: high-light both columns Step 2: insert -> Area //////////////////////////// " .C10EXPLAIN7<-"For (x,y) scatter, x is used as the horizontal axis while y is used as the vertical axis: //////////////////////////// Assume that we have the following two columns. where y= 3+3x x y 1 6 2 9 3 12 4 15 5 18 6 21 7 24 8 27 9 30 10 33 11 36 Step 1: highlight both columns Step 2: Click \"Insert\" -> Line -> All chart types Step 3: choose \"X Y (scatter\" Step 4: click \" OK\" //////////////////////////// " .C10EXPLAIN8<-"Draw a stock chart //////////////////////////// Assume that we have the following data. Date Open High Low Close 4/1/2016 150.509995 153.520004 149.649994 150 3/1/2016 132.240005 153.100006 132.029999 151.449997 2/1/2016 124.400002 134.919998 116.900002 131.029999 1/4/2016 135.600006 136.889999 118.0000 124.790001 12/1/2015 139.580002 141.399994 133.910004 137.619995 11/2/2015 140.5000 142.800003 131.649994 139.419998 10/1/2015 145.309998 153.149994 137.330002 140.080002 9/1/2015 144.910004 149.679993 141.149994 144.970001 8/3/2015 161.699997 161.850006 140.619995 147.889999 7/1/2015 163.970001 173.779999 158.5000 161.990005 6/1/2015 170.210007 171.559998 162.119995 162.660004 5/1/2015 173.199997 176.300003 168.839996 169.649994 4/1/2015 160.229996 175.130005 158.389999 171.289993 3/2/2015 161.940002 165.350006 153.399994 160.5 2/2/2015 154.0000 164.990005 151.509995 161.940002 1/2/2015 161.309998 163.309998 149.520004 153.309998 12/1/2014 161.639999 164.520004 150.5000 160.440002 11/3/2014 164.2500 164.970001 159.800003 162.169998 10/1/2014 189.910004 190.889999 161.100006 164.399994 Step 1: copy and paste it to your Excel Note: parse it into 5 columns if they are in one column. Step 2: Highlight the data set Step 3: click \"Insert\" Step 4: choose \"Other charts\" or any chart Step 5: choose All charts types step 6: choose Stock -> any appropriate one. //////////////////////////// " .C10EXPLAIN9<-"Present your data by using a surface chart. //////////////////////////// A surface chart shows a three-dimensional surface that connects a set of data points Date High Low 4/1/2016 10 3 3/1/2016 12 6 2/1/2016 11 3 1/4/2016 10 7 12/1/2015 9 4 11/2/2015 12 5 Step 1: copy and paste the above data set Step 2: highlight the data set Step 3: insert -> Column Step 4: choose \"All Chart Types\" Step 5: choose \"Surface\" //////////////////////////// " .C10EXPLAIN10<-" Draw a doughnut chart Assume that we have the following data //////////////////////////// excuses,freq trafic,30 sick,10 pet,5 family,10 others,20 Step 1: highlight the data Step 2: click \"Insert\" Step 3: clikc \"Other charts\" Step 4: choose \"Doughnut\" //////////////////////////// " .C10EXPLAIN11<-"Present your data in a bubble chart //////////////////////////// Step 0: generate our data set Let type the following values in a row. (assume that they are in A1, B1 until F1. 1 5 10 20 30 40 In A1, we type the following formula. -fv(0.1,A1,0,100) The copy the formula to B2, C2 until F2. Eventually, we have the following two rows. 1 5 10 20 30 40 110.00 161.051 259.37 672.75 1744.94 4525.93 Step 1: hight the above two rows Step 2: click \"Insert\" Step 3: choose \"Column\" step 4: choose All Chart types Step 5: choose Bubble //////////////////////////// " .C10EXPLAIN12<-"Present your data in a radar chart //////////////////////////// IBM MSFT C WMT Jan 100 1200 200 10 Feb 200 1300 500 1400 Mar 100 2000 100 2000 Apr 300 2300 100 300 May 400 4500 2000 2000 Jun 0 50 2000 1230 Jul 0 40 1000 900 Aug 1500 20 200 600 Sep 5000 100 500 1000 Oct 8500 300 1200 2000 Nov 3500 200 200 400 Dec 500 100 300 1000 Step 1: copy and paste the above data set to Excel step 2: copy the data set Step 3: click \"Insert\" Step 4: choose a type --> All Charts Types Step 5: choose \"Radar\" //////////////////////////// " .C10EXPLAIN13<-"Other charts //////////////////////////// This category includes other charts " .C10EXPLAIN14<-"Win/Loss //////////////////////////// Assume that we have the following sales numbers. Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec apple 267 46 286 26 288 211 270 121 392 34 300 34 orange 483 387 380 442 275 376 215 492 353 244 250 244 plum 363 107 485 465 187 14 19 314 163 229 163 229 banana 456 89 191 248 27 27 188 122 464 23 400 50 We could sum them by using the Excel sum() functions, see below. sum=> 1569 629 1342 1181 777 628 692 1049 1372 530 1113 557 goal 1000 1000 1000 1000 2000 3000 3000 2000 2000 1000 1000 1000 win/loss 569 -371 342 181 -1223 -2372 -2308 -951 -628 -470 113 -443 Assume further that we have sale goals for each month (pay attention that during summer time, the goals higher). Step 1: we calculate win/loss Step 2: Click \"Win/Loss\" Step 3: choose win/loss row (the last row above) Step 4: choose a location. //////////////////////////// " .C10EXPLAIN15<-" One x with two y's (y1 and y2) Assume that we have the following three columns. //////////////////////////// x y1 y2 1 1 2 2 2.2 3 3.2 4 7 4 6 5 8 10 1 Step 1: high-light three columns Step 2: click \"Insert\" Step 3: choose \"Lines\" Step 4: choose \"Other charts\" Step 5: choose one type " .C10EXPLAIN16<-"Macro 4 pie //////////////////////////// Using Macro could make our lives easier. Assume that we have three values in A1, A2 and A3. We plan to draw a pie, see the right image below. Step 1: Copy the following macro Sub PieA1A3() Range(\"A1:A3\").Select ActiveSheet.Shapes.AddChart.Select ActiveChart.ChartType = xlPie ActiveChart.SetSourceData Source:=Range(\"Sheet1!$A$1:$A$3\") End Sub Step 2: Click \"Developer\", \"Macro\", enter any name, such as \"tt\". Then click \"Create\". You will see the following two lines. Sub tt() End Sub Step 3: Replace those two lines with your coped macro in Step 1. Click \"File\", then \"Close and Return to Microsoft Excel Alt+Q\". Step 4: To use it, Click \"Developer\", \"Macro\", find name of PieA1A3, \"Run\". Note that eventually we could generate a button to simplify the step 4. Simply click the button. >c27(15) //////////////////////////// " .C10EXPLAIN17<-"output an image using Ctrl-Print //////////////////////////// Sometimes, we want to copy what we see, i.e., the whole screen. For this case, we could use Ctrl-Print, i.e., hit those two keys same time. If you have picPick software installed, you will see the following instruction \"Click on the left mouse button to select area.\" //////////////////////////// " .C10EXPLAIN19<-"YouTube videos //////////////////////////// //////////////////////////// " .C10EXPLAIN20<-"Links //////////////////////////// Bhalla, Deepanshu, 2019, Create Animation In R : Learn by Examples, https://www.listendata.com/2019/05/create-animation-in-r-learn-with.html Mariana Maroto, Mariana, 2021, How to create an animated chart (gif) in R for beginners, https://marianamarotob.medium.com/how-to-create-an-animated-chart-gif-in-r-for-beginners-de17f3cc059f Peng, Roger D., R Programming for Data Science, https://bookdown.org/rdpeng/rprogdatascience/ R Documentation, https://stat.ethz.ch/R-manual/R-devel/library/base/html/00Index.html R-bloggers, https://www.r-bloggers.com/ R Gallary, https://r-graph-gallery.com/271-ggplot2-animated-gif-chart-with-gganimate.html Stack Overflow, https://stackoverflow.com/questions/tagged/r Wickham, Hadley and Garrett Grolemund, R for Data Science, https://r4ds.had.co.nz Wickham, Hadley, Advanced R, http://adv-r.had.co.nz/ R: Polygon Drawing, http://127.0.0.1:30330/library/graphics/html/polygon.html s://towardsdatascience.com/create-animated-bar-charts-using-r-31d09e5841da //////////////////////////// " .C10EXPLAIN21<-"save our graph //////////////////////////// # step 1: generate our graph plot(sin,-pi,pi,lwd=4) # step 2: click the graph //////////////////////////// " .C10EXPLAIN23<-"pdf() function to save a pdf file //////////////////////////// pdf(\"t.pdf\") plot(sin,-pi,pi,lwd=4) dev.off() //////////////////////////// " .C10EXPLAIN22<-" Snipping tool //////////////////////////// Method I: using snipping tool ------------------------------- Step 1: Generate your image Step 2: Launch Snipping Tool Step 3: click \"New\" Step 4: Highlight the area from your Excel workbook Step 5: save as an image. //////////////////////////// " .C10EXPLAIN23<-" //////////////////////////// //////////////////////////// " .C10EXPLAIN24<-" Using paint softare //////////////////////////// Method II: using paint ---------- Step 1: Launch R generate your image Step 2: Click the image Step 3: choose method to save //////////////////////////// " .C10EXPLAIN25<-"the ggsave() function //////////////////////////// library(ggplot2) data(mtcars) ggplot(mtcars, aes(mpg, wt))+ geom_point() #setwd(\"c:/temp\") # here, the device is inferred from the filename extension ggsave(\"mtcars.pdf\") ggsave(\"mtcars.png\") # unlink(\"mtcars.pdf\") unlink(\"mtcars.png\") //////////////////////////// " .C10EXPLAIN26<-" //////////////////////////// //////////////////////////// " .C10EXPLAIN27<-" //////////////////////////// //////////////////////////// " .C10EXPLAIN28<-" //////////////////////////// //////////////////////////// " .C10EXPLAIN29<-" //////////////////////////// //////////////////////////// " .C10EXPLAIN30<-" //////////////////////////// //////////////////////////// " .C10EXPLAIN31<-" //////////////////////////// //////////////////////////// " .C10EXPLAIN32<-" //////////////////////////// //////////////////////////// " .C10EXPLAIN33<-" //////////////////////////// //////////////////////////// " .C10EXPLAIN34<-" //////////////////////////// //////////////////////////// " .C10EXPLAIN35<-"animation in R //////////////////////////// Using R for animation offers several advantages, including robust data visualization capabilities through packages like gganimate, which seamlessly integrates with ggplot2. This allows for the creation of dynamic, informative, and visually appealing animations directly from data analysis workflows. Additionally, R's extensive library ecosystem supports a wide range of customization and output options, making it easy to tailor animations for specific needs, whether for research, presentations, or educational purposes. The ability to create reproducible and programmatically generated animations ensures consistency and accuracy in representing complex data over time. //////////////////////////// " .C10EXPLAIN36<-" //////////////////////////// library(gapminder) library(ggplot2) library(gganimate) # Make a ggplot, but add frame=year: one image per year ggplot(gapminder,aes(gdpPercap,lifeExp,size=pop,color=continent)) + geom_point() + scale_x_log10() + theme_bw() + labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') + transition_time(year)+ease_aes('linear') anim_save(\"abc.gif\") # Save at gif: //////////////////////////// " .C10EXPLAIN37<-"three columns //////////////////////////// df0 = read.table(text = \" Year ret_A ret_B 1984 0.79 0.1 1989 0.38 0 1991 0.45 0.17 1996 0.27 0.23 1998 0.27 0.31 1999 0.22 0.35 2004 0.28 0.27 2009 0.4 0.23 2014 0.09 0.52 2019 0.07 0.56 \", header=TRUE) names<-sub(\"ret_\",\"\", colnames(df0)) a<-data.frame(df0[,c(1,2)],names[2]) b<-data.frame(df0[,c(1,3)],names[3]) colnames(a)<-c(\"Year\",\"ret\",\"stock\") colnames(b)<-c(\"Year\",\"ret\",\"stock\") df<-rbind(a,b) //////////////////////////// " .C10EXPLAIN38<-"animation: stock return comparison //////////////////////////// library(ggplot2) library(tidyverse) library(gganimate) library(directlabels) library(png) library(transformr) library(grid) # Read Data df0 = read.table(text = \" Year ret_A ret_B 1984 0.79 0.1 1989 0.38 0 1991 0.45 0.17 1996 0.27 0.23 1998 0.27 0.31 1999 0.22 0.35 2004 0.28 0.27 2009 0.4 0.23 2014 0.09 0.52 2019 0.07 0.56 \", header=TRUE) names<-sub(\"ret_\",\"\", colnames(df0)) a<-data.frame(df0[,c(1,2)],names[2]) b<-data.frame(df0[,c(1,3)],names[3]) colnames(a)<-c(\"Year\",\"ret\",\"stock\") colnames(b)<-c(\"Year\",\"ret\",\"stock\") df<-rbind(a,b) t<-'Comparison between stocks A and B' # Set Theme theme_set(theme_minimal()) # Plot and animate title<-\"Comparison between returns of stocks A and B\" p = ggplot(data = df, aes(x= factor(Year), y=ret, group=stock, colour=stock)) + geom_line(size=2, show.legend = FALSE) + scale_color_manual(values=c(\"#ff9933\", \"#006400\")) + scale_x_discrete(position = \"top\") + scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + labs(title =title, x = NULL, y = NULL) + geom_text(aes(label=scales::percent(ret, accuracy = 1),vjust= -2), show.legend = FALSE) + theme(plot.title = element_text(hjust = 0.5)) + geom_dl(aes(label=stock), method=\"last.points\") + transition_reveal(Year) + coord_cartesian(clip = 'off') + ease_aes('cubic-in-out') animate(p, fps = 5, duration = 20, width = 800, height = 400) anim_save(\"my.gif\", p) //////////////////////////// " .C10EXPLAIN39<-"GDP dataset and picture //////////////////////////// //////////////////////////// " .C10EXPLAIN40<-"GDP manipulation //////////////////////////// //////////////////////////// "