# Chapter 6. Data Visualization in Julia

```julia
import Pkg;
Pkg.add(["Tidier", "TidierData", "TidierText", "TidierStrings", "TidierPlots", "DataFrames", "CSV", "Pipelines", "Makie", "StatsPlots", "RDatasets"]);
using Tidier, TidierData, TidierText, TidierStrings, TidierPlots, DataFrames, CSV, Pipelines, Makie, StatsPlots, RDatasets
import Makie.IntervalsBetween, Makie.Attributes

TidierPlots_set("plot_log", false) # I don't not want to see many logs
TidierPlots_set("plot_show", false) # and repetitive plots in VSCode
```

```
[32m[1m   Resolving[22m[39m package versions...
[32m[1m  No Changes[22m[39m to `~/.julia/environments/v1.11/Project.toml`
[32m[1m  No Changes[22m[39m to `~/.julia/environments/v1.11/Manifest.toml`





false
```

## Load Data

```julia
df = dataset("datasets", "iris")
first(df, 5)
```

5×5 DataFrame

| Row | SepalLength | SepalWidth | PetalLength | PetalWidth | Species |
| --- | ----------- | ---------- | ----------- | ---------- | ------- |
|     | Float64     | Float64    | Float64     | Float64    | Cat…    |
| 1   | 5.1         | 3.5        | 1.4         | 0.2        | setosa  |
| 2   | 4.9         | 3.0        | 1.4         | 0.2        | setosa  |
| 3   | 4.7         | 3.2        | 1.3         | 0.2        | setosa  |
| 4   | 4.6         | 3.1        | 1.5         | 0.2        | setosa  |
| 5   | 5.0         | 3.6        | 1.4         | 0.2        | setosa  |

## Basic Plots

### 1. Scatter Plot

```julia
ggplot(data=df) +
geom_point(@aes(x = PetalLength, y = PetalWidth)) +
labs(x="Petal Length", y="Petal Width") +
theme_minimal()
```

![](/files/e2wlYI4Y0ghMjUlm91Kn)

### 2. Bar Plot

```julia
ggplot(data=df) +
geom_bar(@aes(x = PetalWidth)) +
labs(x="Petal Width") +
theme_minimal()
```

![](/files/fvAkEBOOTZAd9K3V2ibE)

### 3. Line Plot

```julia
ggplot(data=df, @aes(x = PetalLength, y = PetalWidth)) +
geom_line() +
labs(x="Petal Length", y="Petal Width") +
theme_minimal()

```

![](/files/nHKHcd6iWiQRjIecEKuT)

### 4. Histogram

```julia
ggplot(data=df, @aes(x = PetalWidth)) +
geom_histogram() +
labs(x="Petal Width") +
theme_minimal()

```

![](/files/AwXdlQUvEZf3rYaMljU1)

## Color and themes

### 1. Color

```julia
ggplot(data=df, @aes(x = PetalLength, y = PetalWidth, color = SepalWidth)) +
geom_point() +
labs(x="Petal Length", y="Petal Width") +
theme_minimal()
```

![](/files/rqbtm28GmOrDkpJcBO1i)

### 2. Themes

```julia
ggplot(data=df, @aes(x = PetalLength, y = PetalWidth, color = SepalWidth)) +
geom_point() +
labs(x="Petal Length", y="Petal Width") +
theme_latexfonts()
```

![](/files/34CjfvtxzDvRYqVRMFXY)

## Facet

```julia
ggplot(data=df, @aes(x = PetalLength, y = PetalWidth, color = SepalWidth)) +
geom_point() +
facet_wrap("Species") +
labs(x="Petal Length", y="Petal Width") +
theme_latexfonts()
```

![](/files/JjbSQCforXYGHA0aiUjo)

However, the supports of ggplot-like visualization cannot be identical to that of `ggplot2` in `R`.

If you want to use R packages or Python packages for certain purposes, we can also simply [use them in Julia](https://github.com/reycn/data-analytics-in-julia/blob/main/gitbook/7.r.and.python.in.julia.jl.ipynb)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://data-julia.rongxin.me/6.visualize.jl.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
