---
title: "Data Communication"
author: 'DSC 365: Introduction to Data Science'
date: "February 10, 2026"
format: html
---

```{r}
#| echo: false
#| message: false
#| warning: false

library(tidyverse)
library(knitr)
library(RColorBrewer)
```

# Example: Hair Color

The data `HairEyeColor` provides the information of hair colors and eye colors in a statistics class. The data is recorded in a three-way table. The first step is to convert the data into a column-wise data frame.

```{r}
data(HairEyeColor)
#HairEyeColor[,,1] #Male
#HairEyeColor[,,2] #Female

newdata_male = as.data.frame(HairEyeColor[,,1]) 
newdata_female = as.data.frame(HairEyeColor[,,2]) 
newdata = rbind(newdata_male, newdata_female)
newdata = newdata %>% 
  mutate(Gender = rep(c("Male", "Female"), each  = 16))

hairData = newdata %>% 
  group_by(Hair) %>%
  summarise(Freq = sum(Freq))

hairData
```

Suppose now we want to see the distribution of hair color in this class. What kind of the plot we should use?

What is the difference between these two?
```{r}
hairData %>% 
  ggplot(aes(x=Hair, y=Freq)) + 
  geom_col(aes(color = Hair)) #<<
```

```{r}
hairData %>% 
  ggplot(aes(x=Hair, y=Freq)) +  
  geom_col(aes(fill = Hair)) #<<
```

It is a little weird that the color in the visualization does not match the color. Let’s try to define the color by ourselves.

```{r}

ggplot(hairData, aes(x = Hair, y = Freq)) + 
  geom_col(aes(fill = Hair)) +
  scale_fill_manual(breaks = c("Black", "Brown", "Red", "Blond"), #<<
                    values=c("black", "brown", "red", "yellow")) #<<
```


You may have realized that in the previous figure, the color red and brown are pretty close. For an extreme case, what if you have a reader who is color-blind? There are a lot of [research](https://jfly.uni-koeln.de/color/) on which palette then to use.

```{r}
cbPalette <- c("#000000","#E69F00","#56B4E9","#009E73","#F0E442")

ggplot(hairData, aes(x = Hair, y = Freq)) + 
  geom_col(aes(fill = Hair)) +
  scale_fill_manual(values=cbPalette)
```

### Try it for yourself!!

Now try by yourself (or with a group) with another type of figure.

(a). Use the iris data below. Plot the figure to show the relation between Sepal.Length and Petal.Length.

```{r}

```

(b). Use colors to distinguish the species. Which function you have used? fill or color?

```{r}


```


(c). Use red to represents `virginica`, blue to represents `versicolor`, and yellow to represents `setosa`.

```{r}


```


(d). Use the color blind friendly color to distinguish three different species

```{r}


```

(e). Besides of the color, use different shape of the points to represents the difference (Hint: Use Google or the Help Documentation).

```{r}


```



# Captions

There are two ways of writing a caption:

1). In-Figure Style

```{r}
#| fig-align: center

ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, group=Species)) + 
  geom_point(aes(color = Species, shape = Species)) + 
  xlab("Sepal Length") + 
  ylab("Patal Length") +
  ggtitle("Relation between Sepal Length and Petal Length") #<<
```


2). Formal report style: use descriptive caption

```{r}
#| label: fig-fig1
#| fig-cap: "Scatter plot between Iris sepal length and Iris petal length. Different species are indicated by different color and shapes. In general, the length of the sepal and petal for Iris have a strong positive linear relationship."

ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, group=Species)) + 
  geom_point(aes(color = Species, shape = Species)) + 
  xlab("Sepal Length") + 
  ylab("Patal Length") +
  ggtitle("Relation between Sepal Length and Petal Length") #<<
```


@fig-fig1 is a scatterplot (cross-reference example)
