File: aes_colour_fill_alpha.Rd

package info (click to toggle)
r-cran-ggplot2 3.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,748 kB
  • sloc: sh: 15; makefile: 5
file content (116 lines) | stat: -rw-r--r-- 4,604 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/aes-colour-fill-alpha.r
\name{aes_colour_fill_alpha}
\alias{aes_colour_fill_alpha}
\alias{colour}
\alias{color}
\alias{fill}
\title{Colour related aesthetics: colour, fill, and alpha}
\description{
These aesthetics parameters change the colour (\code{colour} and \code{fill}) and the
opacity (\code{alpha}) of geom elements on a plot. Almost every geom has either
colour or fill (or both), as well as can have their alpha modified.
Modifying colour on a plot is a useful way to enhance the presentation of data,
often especially when a plot graphs more than two variables.
}
\section{Colour and fill}{


Colours and fills can be specified in the following ways:
\itemize{
\item A name, e.g., \code{"red"}. R has 657 built-in named colours, which can be
listed with \code{\link[grDevices:colors]{grDevices::colors()}}.
\item An rgb specification, with a string of the form \code{"#RRGGBB"} where each of the
pairs \code{RR}, \code{GG}, \code{BB} consists of two hexadecimal digits giving a value in the
range \code{00} to \code{FF}. You can optionally make the colour transparent by using the
form \code{"#RRGGBBAA"}.
\item An \code{NA}, for a completely transparent colour.
}
}

\section{Alpha}{


Alpha refers to the opacity of a geom. Values of \code{alpha} range from 0 to 1,
with lower values corresponding to more transparent colors.

Alpha can additionally be modified through the \code{colour} or \code{fill} aesthetic
if either aesthetic provides color values using an rgb specification
(\code{"#RRGGBBAA"}), where \code{AA} refers to transparency values.
}

\examples{
\donttest{

# Bar chart example
p <- ggplot(mtcars, aes(factor(cyl)))
# Default plotting
p + geom_bar()
# To change the interior colouring use fill aesthetic
p + geom_bar(fill = "red")
# Compare with the colour aesthetic which changes just the bar outline
p + geom_bar(colour = "red")
# Combining both, you can see the changes more clearly
p + geom_bar(fill = "white", colour = "red")
# Both colour and fill can take an rgb specification.
p + geom_bar(fill = "#00abff")
# Use NA for a completely transparent colour.
p + geom_bar(fill = NA, colour = "#00abff")

# Colouring scales differ depending on whether a discrete or
# continuous variable is being mapped. For example, when mapping
# fill to a factor variable, a discrete colour scale is used.
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar()

# When mapping fill to continuous variable a continuous colour
# scale is used.
ggplot(faithfuld, aes(waiting, eruptions)) +
  geom_raster(aes(fill = density))

# Some geoms only use the colour aesthetic but not the fill
# aesthetic (e.g. geom_point() or geom_line()).
p <- ggplot(economics, aes(x = date, y = unemploy))
p + geom_line()
p + geom_line(colour = "green")
p + geom_point()
p + geom_point(colour = "red")

# For large datasets with overplotting the alpha
# aesthetic will make the points more transparent.
set.seed(1)
df <- data.frame(x = rnorm(5000), y = rnorm(5000))
p  <- ggplot(df, aes(x,y))
p + geom_point()
p + geom_point(alpha = 0.5)
p + geom_point(alpha = 1/10)

# Alpha can also be used to add shading.
p <- ggplot(economics, aes(x = date, y = unemploy)) + geom_line()
p
yrng <- range(economics$unemploy)
p <- p +
  geom_rect(
    aes(NULL, NULL, xmin = start, xmax = end, fill = party),
    ymin = yrng[1], ymax = yrng[2], data = presidential
  )
p
p + scale_fill_manual(values = alpha(c("blue", "red"), .3))
}
}
\seealso{
\itemize{
\item Other options for modifying colour:
\code{\link[=scale_colour_brewer]{scale_colour_brewer()}},
\code{\link[=scale_colour_gradient]{scale_colour_gradient()}}, \code{\link[=scale_colour_grey]{scale_colour_grey()}},
\code{\link[=scale_colour_hue]{scale_colour_hue()}}, \code{\link[=scale_colour_identity]{scale_colour_identity()}},
\code{\link[=scale_colour_manual]{scale_colour_manual()}}, \code{\link[=scale_colour_viridis_d]{scale_colour_viridis_d()}}
\item Other options for modifying fill:
\code{\link[=scale_fill_brewer]{scale_fill_brewer()}},
\code{\link[=scale_fill_gradient]{scale_fill_gradient()}}, \code{\link[=scale_fill_grey]{scale_fill_grey()}},
\code{\link[=scale_fill_hue]{scale_fill_hue()}}, \code{\link[=scale_fill_identity]{scale_fill_identity()}},
\code{\link[=scale_fill_manual]{scale_fill_manual()}}, \code{\link[=scale_fill_viridis_d]{scale_fill_viridis_d()}}
\item Other options for modifying alpha: \code{\link[=scale_alpha]{scale_alpha()}}
\item Run \code{vignette("ggplot2-specs")} to see an overview of other aesthestics that
can be modified.
}
}