# 15  Fun Plots (part 1)

This chapter and the next one are dedicated to create a couple of fun projects in which you get to apply what we have covered so far.

The idea is to produce some plots with some text on it. But not any kind of text.

## 15.1 Me & You Plot

The first plot is intended to be a postcard—e.g. for Saint Valentine’s day. After reading this kind of tutorial, you should be able to make youw own plot, print it and give it to your significant other.

The idea is to make a chart with your name and the name of your significant other, adding a touch of randomness in the location of the text, the sizes, and the colors.

First we generate the x-y coordinates. We’ll use 100 points, and set the random seed to 333:

``````# random seed
set.seed(333)

# x-y coordinates
n <- 100
x <- rnorm(n)
y <- rnorm(n, 1, 2)``````

The first step involves producing a very basic-raw plot (nothing fancy). We use `plot()` for this purpose:

``plot(x, y)``

The `plot()` produces a scatter diagram with a 100 points on it.

The following step consists of replacing the dots by some text: your name and the one of your significant other. To hide the dots, we set the parameter `type = "n"`, which means that we don’t want anything to be plotted. To show the text, we use the low level plotting function `text()`. We use the same coordinates, but this time we specify the displayed `labels`:

``````plot(x, y, type = "n")
text(x, y, labels = "me & you")``````

Again, this is a very preliminary plot; something basic that allows us to start getting a feeling of how the chart looks like.

The second step is to change the background color. One way to do this is by specifying the `bg` graphical parameter inside the `par()` function:

``````# graphical parameters
op <- par(bg = "gray10")
# plot text
plot(x, y, type = "n")
text(x, y,
labels = "me & you",
col = "white")``````

``````# reset default parameters
par(op)``````

`par()` has default settings. Everytime you call `par()` and change one of the associated parameters, the subsequent plots will be displayed with those values. To set parameters in a temporary way, you can assign them to an object: i.e. `op`. After the plot is produced, we reset the default graphical parameters with the instruction `par(op)`.

We are getting closer to the desired look of the postcard. The final stage is to add some color to the text, and change their size. The size of the labels will also be random with a uniform distribution.

R provides several ways to specify colors. In this example we will use the `hsv()` function (i.e. hue-saturation-value). This function requires three parameters: hue (color), saturation, and value. Hues are specified with a range from 0 to 1. We generate some random numbers in the interval 0.85 - 0.95 to get some hues red, pink, fuchsia colors. `hsv()` also takes the optional parameters `alpha` to determined the alpha transparency.

``````# text size
sizes <- runif(n, 0.5, 3)

# text color
hues <- runif(n, 0.85, 0.95)
alphas <- runif(n, 0.1, 1)

op <- par(bg = "gray10", mar = rep(0, 4))
plot(x, y, type = "n", axes = FALSE,
xlab = '', ylab = '')
text(x, y,
labels = "me & you",
font = 3,
col = hsv(hues, 1, 1, alphas),
cex = sizes)``````

``par(op)``

To save the image, we call `pdf()`. To give the image the dimensions of a standard postcard, you can specify `width = 5` and `height = 3.5`, that is, 5 inches wide and 3.5 inches tall (you can choose other dimensions if you want):

``````pdf(file = "figure/me-and-you3.pdf", width = 5, height = 3.5)
op <- par(bg = "gray10",
mar = rep(0, 4))
plot(x, y, type = "n", axes = FALSE,
xlab = '', ylab = '')
text(x, y,
labels = "me & you",
font = 3,
col = hsv(hues, 1, 1, alphas),
cex = sizes)
par(op)
dev.off()``````

If you save the image in png format, you could also use it as a wallpaper for your computer: