# Drawing a Fish Curve using R and ggplot2

## Intro

Recently, I wondered whether there is a way to draw a fish shape using a mathematical function. Since I did not find a ready-made R function, I tried to write the function by myself. The equations, I've used for writing this function can be found on WolframMathWorld.

## The function

The `fish_curve()` function requires the `ggplot2` and the `dplyr` package. It creates a data frame with two variables (x and y) and 10.000 observations. Finally, the data points are plotted using `ggplot2`.

```fish_curve <- function(colour='black', size = 5){
library(ggplot2)
library(dplyr)
data.frame(
x = cos(1:10000) - sin(1:10000)^2 / sqrt(2),
y = cos(1:10000) * sin(1:10000)
) %>%
ggplot(., aes(x, y)) +
geom_point(colour = colour, size = size) +
theme_void()
}
```

## Function call with default parameters

With `colour` and `size` the `fish_curve()` function allows the user to specify two parameters; that is colour and size of the plotted points. The default values are black for `colour` and 5 for `size`.

```(p1 <- fish_curve())
``` ## Customization

In the following example, we customize colour and size of the fish shape:

```(p2 <- fish_curve(colour = 'blue', size = 1))
``` And finally, we place the two plots side by side using the `patchwork` package:

```library(patchwork)
p1 + p2
```  ## Author: norbert

Biometrician at Clinical Trial Centre, Leipzig University (GER), with degrees in sociology (MA) and public health (MPH).

This site uses Akismet to reduce spam. Learn how your comment data is processed.