Chapter 3 Geom arc layers
Based the arcGrobs we have created, we can transform them into geom layers by using ggplot2 grammer for better visualisation. The following contents we will describe relative geom arc layers.
The basic arguments for each layer includes: start, end, r0, r1, clock.wise. You can re-specify them in each geom mapping parameters.
3.1 geom_arcpoint
geom_arcpoint draws scatter point like geom_point.
Simple scatter point:
Mapping with colors:
ggcirclize(data = mtcars,
mapping = aes(x = mpg,y = disp,end = 270)) +
geom_arcpoint(aes(color = factor(cyl)))
You can adjust the axis settings with yAxis.params and xAxis.params:
ggcirclize(data = mtcars,
mapping = aes(x = mpg,y = disp)) +
geom_arcpoint(yAxis.params = list(yscale = c(100,800)),
xAxis.params = list(xscale = c(-50,100)))
Mapping with size and color:
ggcirclize(data = mtcars,
mapping = aes(x = mpg,y = disp,
color = as.character(cyl),size = qsec)) +
geom_arcpoint()
Mapping with gradient color:
ggcirclize(data = mtcars,
mapping = aes(x = mpg,y = disp,
color = qsec,size = qsec)) +
geom_arcpoint(end = 270,r0 = 0.3,r1 = 0.8) +
scale_color_gradient(low = "blue",high = "red")
Control the background graphic settings:
3.2 geom_arcsegment
geom_arcsegment draws segment line like geom_segment.
df <- data.frame(x = c(rep(0,4),0:4),x1 = c(rep(4,4),0:4),
y = c(0:4,rep(0,4)),y1 = c(0:4,rep(4,4)),
g = LETTERS[1:9])
ggcirclize(df) +
geom_arcsegment(aes(xmin = x,xmax = x1,ymin = y,ymax = y1))
Add arrows:
3.4 geom_arcpath
geom_arcpath draws line like geom_path.
See what geom_arcpath output:
3.5 geom_arcpolygon
geom_arcpolygon draws polygon like geom_polygon.
First we see output with ggplot:
ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
values <- data.frame(
id = ids,
value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5)
)
positions <- data.frame(
id = rep(ids, each = 4),
x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
)
# Currently we need to manually merge the two together
datapoly <- merge(values, positions, by = c("id"))
ggplot(datapoly, aes(x = x, y = y)) +
geom_polygon(aes(fill = value, group = id))
geom_arcpolygon outputs:
The id is used to distinguish the groups:
3.6 geom_arcrect
geom_arcrect draws rectangle like geom_rect.
df <- data.frame(
x = rep(c(2, 5, 7, 9, 12), 2),
y = rep(c(1, 2), each = 5),
z = factor(rep(1:5, each = 2)),
w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2)
)
df <- df %>% mutate(xmin = x - w / 2, xmax = x + w / 2, ymin = y, ymax = y + 1)
ggplot(df, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)) +
geom_rect(aes(fill = z), colour = "grey50")
Mapping with color:
3.7 geom_arctile
geom_arctile draws rectangle like geom_tile.
data(USArrests)
mat <- scale(USArrests)
mat.long <- reshape2::melt(mat)
ggplot(mat.long,aes(x = Var1,y = Var2,fill = value)) +
geom_tile() +
scale_fill_gradient2(low = "green",mid = "black",high = "red",midpoint = 0) +
theme(axis.text.x = element_text(angle = 90,hjust = 1,vjust = 0.5))
ggcirclize(mat.long,aes(x = Var1,y = Var2,fill = value),mg.t = 2.5) +
geom_arctile() +
scale_fill_gradient2(low = "green",mid = "black",high = "red",midpoint = 0)
Adjust the width and height:
3.9 geom_arccol
geom_arccol draws barplot like geom_col.
3.10 geom_arcribbon
geom_arcribbon draws a y interval like geom_ribbon.
3.11 geom_arcviolin
geom_arcviolin draws violin plot like geom_violin.
Horizontal violin:
Turn off trim:
Mapping with color:
Multiple groups:
Draw quantiles:
ggcirclize(mtcars, aes(factor(cyl), mpg)) +
geom_arcviolin(draw_quantiles = c(0.25, 0.5, 0.75),
polar.every = T)
Multiple violins:
3.12 geom_arcboxplot
geom_arcboxplot draws boxplot like geom_boxplot.
Change the outlier point graphic settings:
Horizontal boxplot:
Add notch:
Multiple groups:
3.13 geom_arcarea
geom_arcarea draws a y interval like geom_area.
set.seed(1234)
df <- data.frame(
sex=factor(rep(c("F", "M"), each=200)),
weight=round(c(rnorm(200, mean=55, sd=5),
rnorm(200, mean=65, sd=5)))
)
ggplot(df, aes(x=weight)) +
geom_area(stat = "bin")
Multiple groups:
3.14 geom_arcdensity
geom_arcdensity draws kernel density estimate like geom_density.
Mapping with color:
Different position mode:
3.15 geom_archistogram
geom_archistogram draws frequency polygons like geom_histogram.
Change the binwidth: