Skip to content

Commit

Permalink
write a brief informative commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Maryam-NouriAiin committed May 6, 2024
1 parent c21dda7 commit c6b96c4
Show file tree
Hide file tree
Showing 3 changed files with 2,481 additions and 549 deletions.
12 changes: 6 additions & 6 deletions homework--12.html

Large diffs are not rendered by default.

243 changes: 200 additions & 43 deletions howework#12.Rmd
Original file line number Diff line number Diff line change
@@ -1,65 +1,222 @@
---
title: "Vignette Title"
author: "Vignette Author"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
title: "Homework #12"
mainfont: DejaVu Sans
output:
html_document: default
pdf_document:
latex_engine: xelatex
word_document: default
font-family: Times New Roman
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

Vignettes are long form documentation commonly included in packages. Because they are part of the distribution of the package, they need to be as compact as possible. The `html_vignette` output type provides a custom style sheet (and tweaks some options) to ensure that the resulting html is as small as possible. The `html_vignette` format:
## Pckages

- Never uses retina figures
- Has a smaller default figure size
- Uses a custom CSS stylesheet instead of the default Twitter Bootstrap style
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(ggstatsplot)
library(palmerpenguins)
library(tidyverse)
library(patchwork)
library(plotly)
## Vignette Info
```

Note the various macros within the `vignette` section of the metadata block above. These are required in order to instruct R how to build the vignette. Note that you should change the `title` field and the `\VignetteIndexEntry` to match the title of your vignette.

## Styles
## Experimenting with rendering

```{r echo=FALSE, message=FALSE, warning=FALSE}
data("penguins", package = "palmerpenguins")
penguins <- drop_na(penguins)
```

The `html_vignette` template includes a basic CSS theme. To override this theme you can specify your own CSS in the document metadata as follows:
## Histogram
```{r echo=FALSE, message=FALSE, warning=FALSE}
hist <- ggplot(data = penguins, aes(x = flipper_length_mm)) +
geom_histogram(aes(fill = species),
color = "black", # Add black outline to bars
alpha = 0.5,
position = "identity",
binwidth = 5) + # Adjust bin width as needed
scale_fill_manual(values = c("darkblue", "red", "cyan4")) +
labs(x = "Flipper length (mm)",
y = "Frequency",
title = "Flipper") +
theme(panel.background = element_rect(fill = "lightgray"), # Add background color
panel.grid.major = element_line(color = "white", size = 0.5), # Add grid lines
panel.grid.minor = element_line(color = "white", size = 0.25))
hist
# # Convert ggplot to interactive plot
# hist_interactive <- ggplotly(hist)
#
# hist_interactive
output:
rmarkdown::html_vignette:
css: mystyles.css
```

## Figures
## Interactive plot
```{r echo=FALSE, message=FALSE, warning=FALSE}
# Initial bin width
initial_binwidth <- 5
# Function to create histogram
create_histogram <- function(binwidth) {
ggplot(data = penguins, aes(x = flipper_length_mm)) +
geom_histogram(aes(fill = species),
color = "black",
alpha = 0.5,
position = "identity",
binwidth = binwidth) +
scale_fill_manual(values = c("darkblue", "red", "cyan4")) +
labs(x = "Flipper length (mm)",
y = "Frequency",
title = "Flipper ") +
theme(panel.background = element_rect(fill = "lightgray"),
panel.grid.major = element_line(color = "white", size = 0.5),
panel.grid.minor = element_line(color = "white", size = 0.25))
}
# Create initial plot
histogram <- create_histogram(initial_binwidth)
# Convert ggplot to plotly
histogram_plotly <- ggplotly(histogram)
# Add slider for bin width
histogram_plotly <- histogram_plotly %>%
layout(
updatemenus = list(
list(
active = 0,
buttons = list(
list(
label = "Bin Width",
method = "update",
args = list(list(visible = c(TRUE, FALSE)),
list(title = "Bin Width",
xaxis = list(title = "Flipper length (mm)"),
yaxis = list(title = "Frequency")))),
list(
label = "5",
method = "update",
args = list(list(visible = c(TRUE, FALSE)),
list(title = "Bin Width",
xaxis = list(title = "Flipper length (mm)"),
yaxis = list(title = "Frequency")),
list(binwidth = 5))),
list(
label = "10",
method = "update",
args = list(list(visible = c(TRUE, FALSE)),
list(title = "Bin Width",
xaxis = list(title = "Flipper length (mm)"),
yaxis = list(title = "Frequency")),
list(binwidth = 10)))
)
)
)
)
# Show plot
histogram_plotly
```
## Scatter plot
```{r echo=FALSE, message=FALSE, warning=FALSE}
point <- ggplot(data = penguins,
aes(x = flipper_length_mm,
y = body_mass_g)) +
geom_point(aes(color = species,
shape = species),
size = 6,
alpha = 0.8) +
geom_smooth(method = "lm", se = FALSE, color = "black", linetype = "dashed") + # Add a trend line
scale_color_manual(values = c("darkblue","red","cyan4")) +
scale_shape_manual(values = c(16, 17, 15)) + # Modify point shapes
labs(title = "Penguin size",
subtitle = "Flipper length and body mass",
x = "Flipper length (mm)",
y = "Body mass (g)",
color = "Penguin species",
shape = "Penguin species") +
theme(legend.position = "right", # Adjust legend position
plot.title.position = "plot",
plot.caption = element_text(hjust = 0, face= "italic"),
plot.caption.position = "plot")
point
ggplotly(point)
The figure sizes have been customised so that you can easily put two images side-by-side.
```

```{r, fig.show='hold'}
plot(1:10)
plot(10:1)
## Statistical plot
```{r echo=FALSE, message=FALSE, warning=FALSE}
stat_plot <- ggbetweenstats(
data = penguins,
x = species,
y = bill_length_mm
)
stat_plot <- stat_plot +
# Add labels and title
labs(
x = "Penguins Species",
y = "Bill Length",
title = "Distribution of bill length across penguins species"
) +
# Customizations
theme(
# This is the new default font in the plot
text = element_text(family = "Roboto", size = 8, color = "black"),
plot.title = element_text(
family = "Lobster Two",
size = 20,
face = "bold",
color = "#2a475e"
),
# Statistical annotations below the main title
plot.subtitle = element_text(
family = "Roboto",
size = 15,
face = "bold",
color="#1b2838"
),
plot.title.position = "plot", # slightly different from default
axis.text = element_text(size = 10, color = "black"),
axis.title = element_text(size = 12)
)
# stat_plot
stat_plot <- stat_plot +
theme(
axis.ticks = element_blank(),
axis.line = element_line(colour = "grey50"),
panel.grid = element_line(color = "#b4aea9"),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(linetype = "dashed"),
panel.background = element_rect(fill = "#fbf9f4", color = "#fbf9f4"),
plot.background = element_rect(fill = "#fbf9f4", color = "#fbf9f4")
)
stat_plot + facet_wrap(~species, scales = "free_y")
```

You can enable figure captions by `fig_caption: yes` in YAML:

output:
rmarkdown::html_vignette:
fig_caption: yes
## Multiple plots in ggplot2 with patchwork

```{r echo=FALSE, message=FALSE, warning=FALSE}
point + hist + stat_plot + plot_layout(ncol = 1, nrow = 3)
Then you can use the chunk option `fig.cap = "Your figure caption."` in **knitr**.
stat_plot + {
hist +
point +
plot_layout(ncol = 1)
} +
plot_layout(ncol = 1, heights = c(2,1,1))
## More Examples
(stat_plot / hist) | point
You can write math expressions, e.g. $Y = X\beta + \epsilon$, footnotes^[A footnote here.], and tables, e.g. using `knitr::kable()`.
```{r, echo=FALSE, results='asis'}
knitr::kable(head(mtcars, 10))
(stat_plot | hist) / point
```

Also a quote using `>`:

> "He who gives up [code] safety for [code] speed deserves neither."
([via](https://twitter.com/hadleywickham/status/504368538874703872))
2,775 changes: 2,275 additions & 500 deletions howework-12.html

Large diffs are not rendered by default.

0 comments on commit c6b96c4

Please sign in to comment.