ggpointless
is a small extension of the ggplot2
. The goal is to easily add minimal emphasis to your plots.
library(ggplot2)
library(ggpointless)
<- seq(-pi, pi, length.out = 100)
x <- outer(x, 1:5, function(x, y) sin(x*y))
y
<- data.frame(
df1 var1 = x,
var2 = rowSums(y)
)
<- ggplot(df1, aes(x = var1, y = var2))
p + geom_pointless(location = c("first", "last", "minimum", "maximum")) p
As you see, just adding geom_pointless()
to ggplot(...)
is not terribly useful on its own but when it teams up with geom_line()
and friends, hopefully.
<- p + geom_line()
p + geom_pointless(location = "all", size = 3) p
geom_pointless()
behaves like geom_point()
does with the addition of a location
argument. You can set it to "first"
, "last"
, the default, "minimum"
, "maximum"
, and "all"
; where "all"
is just shorthand to select "first"
, "last"
, "minimum"
and "maximum"
.
In addition, you can use the computed variable location
and map it to an aesthetic, e.g. color
.
+ geom_pointless(aes(color = after_stat(location)),
p location = "all",
size = 3) +
theme(legend.position = "bottom")
The locations are determined in the order in which they appear in the data, like geom_path()
does compared to geom_line()
. This can be seen in the next example, with sample data kindly taken from the geomtextpath
package:
<- seq(5, -1, length.out = 1000) * pi
x <- data.frame(var1 = sin(x) * 1:1000,
spiral var2 = cos(x) * 1:1000)
<- ggplot(spiral) +
p geom_path() +
coord_equal(xlim = c(-1000, 1000), ylim = c(-1000, 1000)) +
theme(legend.position = "none")
+ aes(x = var1, y = var2) +
p geom_pointless(aes(color = after_stat(location)),
location = "all",
size = 3) +
labs(subtitle = "orientation = 'x'")
+ aes(y = var1, x = var2) +
p geom_pointless(aes(color = after_stat(location)),
location = "all",
size = 3) +
labs(subtitle = "orientation = 'y'")
As you see from the first of the last two examples "first"
and "minimum"
overlap, and "first"
wins over "minimum"
. If location
is set to "all"
, then the order in which points are plotted from top to bottom is: "first"
> "last"
> "minimum"
> "maximum"
.
Otherwise, the order is determined as specified in the location
argument, which also applies to the order of the legend key labels.
<- data.frame(var1 = 1:2,
df2 var2 = 1:2)
<- ggplot(df2, aes(x = var1, y = var2)) +
p geom_path() +
coord_equal()
# same as location = 'all'
+ geom_pointless(aes(color = after_stat(location)),
p location = c("first", "last", "minimum", "maximum"),
size = 3) +
labs(subtitle = "same as location = 'all'")
# reversed order
+ geom_pointless(aes(color = after_stat(location)),
p location = c("maximum", "minimum", "last", "first"),
size = 3) +
labs(subtitle = "custom order")
# same as location = 'all' again
+ geom_pointless(aes(color = after_stat(location)),
p location = c("maximum", "minimum", "last", "first", "all"),
size = 3) +
labs(subtitle = "same as location = 'all' again")
Just like all stat_*
functions, stat_pointless()
has a default geom, which is "point"
. This means in reverse that you can highlight e.g. minimum and maximum in another way, for example with a horizontal line.
set.seed(42)
ggplot(data.frame(x = 1:10, y = sample(1:10)), aes(x, y)) +
geom_line() +
stat_pointless(
aes(yintercept = y, color = after_stat(location)),
location = c("minimum", "maximum"),
geom = "hline"
+
) guides(color = guide_legend(reverse = TRUE))
The ggpointless
package contains two data sets:
co2_ml
: CO2 records taken at Mauna Loacovid_vac
: COVID-19 Cases and Deaths by Vaccination StatusSee the vignette("examples")
for possible use cases.