Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

STATS 787

SEMESTER ONE, 2021

STATISTICS

Data Visualisation

1. [10 marks]

(a)

library(ggplot2)

g  <- ggplot(singapore) +

[5 marks]

geom_bar(aes(age,  fill=after_stat(count)))  +

scale_fill_binned(breaks=0:4  +  .5)  +

facet_wrap(vars(gender), ncol=1)

print(g)

(b)

[5 marks]

Geoms:

bar

Stat:

count

Aesthetics:

x  =  Age

y  =  after_stat(count)

fill  =  Age

Scales:

x_continuous

y_continuous

fill_binned

Coords:

cartesian

Facet:

Gender

2. [10 marks]

library(grid)

n  <- length(singapore$age)

grid.newpage()

grid.rect(gp=gpar(col="grey"))

pushViewport(viewport(width=.8, height=.8,

xscale=c(1, n),

yscale=c(0,  80)))

grid.segments(unit(1:n,  "native"),  0,

unit(1:n,  "native"), unit(singapore$age,  "native"), gp=gpar(lty="dotted"))

grid.segments(0,  0,  1,  0,

gp=gpar(col="black"))

grid.circle (unit(1:n,  "native"), unit(singapore$age,  "native"),

r=unit(1,  "mm"),

gp=gpar(fill=c(2,  4)[(gender  ==  "Male")  +  1]))

grid.text(singapore$age,

unit(1:n,  "native"),

unit(singapore$age,  "native")  + unit(2,  "mm"),

just="bottom",

gp=gpar(cex=.7))

grid.text("Case number",

x=0,

y=unit(-1,  "lines"),

just="left",

gp=gpar(col="grey"))

grid.segments(stringWidth("Case number")  + unit(2,  "mm"), unit(-1,  "lines"),  stringWidth("Case number")  + unit(22,  "mm"), unit(-1,  "lines"), gp=gpar(col="grey",  fill="grey"),

arrow=arrow(angle=20,  length=unit(3,  "mm"),

type="closed"))

popViewport()

3. [10 marks]

(a)

(b)

[5 marks]

Small multiples are being used because this is a facetted plot with one panel for each gender.   This makes it easy to separately view the age distribution for female versus male cases.

Redundant coding is being used to represent the count variable, which is reflected in both the height of the bars AND the colour of the bars. This is not very effective because height or length on a common scale is already very effective, while perceiving relative shades is not, so adding colour does not help.

The principle of proportional ink is being obeyed in this plot because the bars all start at zero (so the amount of ink used to draw the bar is proportional to the value being represented).

The data-ink ratio is not great in this plot because a lot of ink is being spent on the background of each panel and on the panel strips or headers.

[5 marks]

Is there a trend towards younger or older ages over time?  This requires us to judge position on a common scale, to judge the heights of the points and compare them. We are good at this task, so we can easily see a slight downward trend in the ages from left to right.

Do the females tend to be younger or older than the males? This requires us to judge position on a common scale again AND at the same time identify differences in hue, to differentiate females from males.  Both of these tasks are quite easy, but it is difficult to perceive the distribution of all ages for each gender because there are so many points. This could be made easier if all of the female points were collected together (and ditto for males).

Which age occurs most often? Once again, we are required to judge posi- tion on a common scale, though this is backed up by the text labels. The latter is helpful because it might otherwise be difficult to tell that two points at a horizontal distance from each other are the same or different by one.  This is still not an easy task because again we have to hold lots of information in memory.  This would be much easier to ask using the ggplot2 plot because there we just have to identify the single tallest bar.

4. [10 marks]

(a)                                                                                                               [5 marks] Contrast has been used to reduce the impact of the “Time” label on the x-axis, by drawing it in grey where the rest of the plot uses black.  This emphasises the data aspects of the plot (which is the most important part of the plot). Contrast is also used to colour different symbols representing different genders. Repetition has been used to colour all data symbols corresponding to female data pinkish and all data symbols corresponding to male data blueish.  This allows us to easily identify and distinguish the two groups of data symbols. The use of a standard colour-to-gender mapping also means that we do not have to bother with a legend. Repetition is also used to draw each data point in the same way, with a coloured circle atop a dotted line, with a text label on top.

Alignment has been used to left-align the “Case number” label with the left edge of the plot.  This helps to give the plot a cleaner structure, which can make it easier to navigate to different parts of the plot.

Proximity has been used to group the age labels with the data symbols. This means that it is easy to associate each label with its corresponding data symbol.

(b)                                                                                                               [5 marks]   The code chunk options, dev  =  "png" and dev.args  =  list(type  =  "cairo") are important because they specify a Cairo-based PNG graphics device, which   means that we can specify a font family just by giving the font family name,   e.g., Ubuntu.

library(ggtext) loads the ggtext package, which allows us to draw text with more complex formatting on a ggplot2 plot.

g is "ggplot" object, so it will draw a plot.

The ggtitle() call defines a title for the plot. The only argument is the text for the title, but this is special because it includes an HTML span element with a style attribute.  This specifies that the word Singapore in the title (and only that word) should be drawn red using a font called Ubuntu.           The theme() call is important for the more complex formatting of the title to work because it says that the plot.title is a special element_markdown object (instead of the normal element_text).

Without the theme() call, the text would not be formatted specially and in fact all of the code would be drawn verbatim rather than affecting the formatting of the word Singapore.

5. [10 marks]

(a)

[7 marks]

The first two lines just load the required packages and the third line creates a data frame containing the two variables age and gender.

The next line creates a “shared” data frame, with gender as the “key” variable. This sets up a link between all rows of data that share the same gender value (which affects selections; see below).

The next three lines create a ggplot2 scatterplot, with a symbol for each age value.  The text aesthetic is important because it sets up the content of the tooltips later on. The colour aesthetic is set so that the first two data symbols are green and blue, just so that they are easy to describe in the exam question text. The data symbols are also drawn a little larger than they normally would be.

The ggplotly() call turns the static ’ggplot2’ plot into an interactive ’plotly’ plot. This adds all sorts of default interactive features, such as tooltips, zoom- ing and panning, and selections.

The final line specifies that selections on the interactive plot will colour the selected points red and, because of the set up earlier in highlight_key(), if we select one point, all points with the same gender will be highlighted.

(b)                                                                                                               [3 marks]

● We hover the mouse cursor over the green data symbol in the plot.

This will elicit a tooltip that shows the x-value and y-value associated with the data point AND, because we set up the text aesthetic, the tooltip will also show the gender associated with the data point.

● We select the green data symbol in the plot (by clicking and dragging a rectangular selection around just that data symbol).

The data symbol will be drawn in red AND all other data symbols with the same gender will be drawn red.

● We select the blue data symbol (just below and to the right of the top-left data symbol).

The data symbol will be drawn in red AND all other data symbols with the same gender will be drawn red. This will be the inverse of the symbols highlighted in the previous step and the selection from the last step will disappear.

6. [10 marks]

(a)

[7 marks]

The ggplot() call just creates a "ggplot" object, with the asiaCounts object as the data to visualise.

The geom_sf() call draws the map regions for the three countries in asiaCounts. The freq variable is mapped to both the fill and the border colour of each re-  gion, which means that each country is filled with a different colour.   The  weight variable is mapped to the size aesthetic, which means that the region  for Singapore is drawn with a much thicker border (this allows us to see Sin-  gapore as a blob; it would otherwise be invisible because it is so small on the  scale of this map).

The geom_label() call draws the country name at the “centre” of each country region (with the bottom-left of the label at the country centre).  The label is drawn in white (colour) on a semitransparent black background (fill), with no border (label.size).

The rgb() function allows us to specify a colour in terms of amounts of red, blue, green, and alpha (for semitransparency).

The colours used to fill and draw the boundary of each country are determined by the number of cases that originated in each country. That is a continuous variable so we have a colour gradient;  the  scale_colour_gradient() call specifies that the gradient transitions from a dark, dull yellow (brown) to a lighter and more colourful yellow as the number of cases increases.

The hcl() function allows us to specify a colour in terms of its hue (0 to 360), chroma (colourfulness), and luminance (lightness).

(b)                                                                                                               [3 marks] If we are only interested in comparing the three countries in terms of the num- ber of COVID cases that originated in each country, a better data visualisation would be a simple bar plot of the number of cases for each country.                The following code would produce such a plot.

>  library(ggplot2)

>  ggplot(asiaCounts)  +

geom_col(aes(x=name,  y=freq))

7. [10 marks]

(a)

[5 marks]

The ggraph() call creates a visualisation of the graph object, using a "linear" layout (which means that the nodes are placed in a straight line).                   The geom_edge_arc() call specifies that curved edges should be drawn be- tween nodes (that are connected).

The geom_node_point() call specifies that circles should be drawn for each node, with the a different colour used for each gender, and it explicitly controls the size of the circles.

The geom_node_text() call specifies that text labels should be drawn beneath each circle, reflecting the case number of each node. Again, the size of the text for each label is controlled explicitly.

The scale_colour_manual() call specifies that the mapping from gender to the colour of the circles should use colour 2 (reddish-pink) for the female nodes and 4 (blue) for the male nodes.

(b)

What is the case number for each case?

[5 marks]

The Gestalt Law of proximity explains why we automatically associate each numeric label with its closest circle.

● Which cases are female and which are male?

The Gestalt Law of similarity explains why we can easily identify the female cases as separate from the male cases (because all female circles are pink and all male circles are blue).

This is also explained by preattentive colour perception, which means that we effortlessly differentiate the pink circles from the blue circles.

● Which cases are contacts of each other?

The Gestalt Law of connection explains why we can easily identify the cases that are contacts of each other (because they are connected by a line).

The fact that we can easily follow the lines between circles, even though they overlap with each other, is explained by the Gestalt Law of continuity.

8. [10 marks]

(a)

[5 marks]

The svg element declares that this is an SVG image and establishes the size of the image to be 310 by 310 pixels.

NOTE that the coordinate system within this image has (0, 0) at top-left, so y-values have to be inverted.

The first line element draws a black line 10 pixels off the bottom of the image from 100 pixels to 200 pixels.

The second and third line elements draw grey lines from the black line up to a height corresponding to the first two ages of Singapore COVID cases.         The circle elements draw a blue and pink circle atop the grey lines with radius 5 pixels.

The text elements draw labels above the circles.

(b)

##  !xml2 !  code  to write  SVG  code

library(xml2)

svg  <- xml_new_root("svg",

version="1.1",

[5 marks]

width="310", height="310",

xmlns="http://www.w3.org/2000/svg ")

##  y  =  300  -  3*age

##  Black  line  at bottom

xml_add_child(svg,  "line",

x1="10",  x2="300",  y1="300",  y2="300", stroke="black",  "stroke-width"="1")

for  (i  in  1:30)  {

x  <- i*10

y  <- 300 - 3*singapore$age[i]

xml_add_child(svg,  "line",

x1=x,  x2=x,  y1="300",  y2=y,             stroke="grey",  "stroke-width"="1")

fill  <- if (gender[i] == "Male") "#2297E6" else "#DF536B" xml_add_child(svg, "circle",

cx=x,  cy=y,  r="3",  fill=fill)

xml_add_child(svg,  "text",  singapore$age[i],

x=x,  y=y  -  5,  "font-size"="8",             "text-anchor"="middle",  fill="black")

}

writeLines(as.character(svg),  "xml.svg")