视觉属性在ggplot中也称为艺术属性,是人眼能够感知的几何图形的特征,包括大小、形状、颜色、填充等,其中颜色和填充色是所有几何图形都有的属性。
颜色和填充色可用名称来指定,ggplot内置657种有名称的颜色,可以直接用颜色名指定颜色。如"rea","green","blue"。这657种颜色名称可用colours()函数列出。 颜色也可用6位16进制数确定,即RGB值,如"#00ff00"代表绿色。如果使用透明度则使用RGBA格式如"#00ff00aa",最后的aa代表透明度。颜色值使用大小写都可以。
线型适用于直线、线段、多边形元素。线型也可用名称和数字表示:
0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash
lty <- c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash")
linetypes <- data.frame(
y = seq_along(lty),
lty = lty
)
ggplot(linetypes, aes(0, y)) +
geom_segment(aes(xend = 5, yend = y, linetype = lty)) +
scale_linetype_identity() +
geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2) +
scale_x_continuous(NULL, breaks = NULL) +
scale_y_reverse(NULL, breaks = NULL)
和颜色一样,线型也可以点的有无来表示,线看作有限个点连接而成,在一定位置点是否存在的规律性排列就形成了不同的线型。就某个点而言,存在与否用开关表示,连续的存在的点用一个16进制数表示,紧接其后的空点也用一个16进制数表示,这样点和空点形成两个16进制数对,就好似一个两位的16进制数,如"33"代表3个实点和3个空点,"f1"代表15个实点后跟一个空点。这个数可以是2位也可以是4、6、8位,这样可以定制更加复杂的线型。
lty <- c("11", "18", "1f", "81", "88", "8f", "f1", "f8", "ff")
linetypes <- data.frame(
y = seq_along(lty),
lty = lty
)
ggplot(linetypes, aes(0, y)) +
geom_segment(aes(xend = 5, yend = y, linetype = lty)) +
scale_linetype_identity() +
geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2) +
scale_x_continuous(NULL, breaks = NULL) +
scale_y_reverse(NULL, breaks = NULL)
上面标准的5个线型对应的16进制数分别为:44, 13, 1343, 73, 2262.
尺寸的单位是毫米(mm)。点的尺寸指的是点的直径,线的尺寸指的是线的宽度。(?)
线端和线连接是线的属性。 lineend有 “round”, “butt” (默认)”,“square”三种。
df <- data.frame(x = 1:3, y = c(4, 1, 9))
base <- ggplot(df, aes(x, y)) + xlim(0.5, 3.5) + ylim(0, 10)
base +
geom_path(size = 10) +
geom_path(size = 1, colour = "red")
base +
geom_path(size = 10, lineend = "round") +
geom_path(size = 1, colour = "red")
base +
geom_path(size = 10, lineend = "square") +
geom_path(size = 1, colour = "red")
线连接有:“round” (the default), “mitre”, or “bevel”三种。
df <- data.frame(x = 1:3, y = c(9, 1, 9))
base <- ggplot(df, aes(x, y)) + ylim(0, 10)
base +
geom_path(size = 10) +
geom_path(size = 1, colour = "red")
base +
geom_path(size = 10, linejoin = "mitre") +
geom_path(size = 1, colour = "red")
base +
geom_path(size = 10, linejoin = "bevel") +
geom_path(size = 1, colour = "red")
{r}
shapes <- data.frame(
shape = c(0:19, 22, 21, 24, 23, 20),
x = 0:24 %/% 5,
y = -(0:24 %% 5)
)
ggplot(shapes, aes(x, y)) +
geom_point(aes(shape = shape), size = 5, fill = "red") +
geom_text(aes(label = shape), hjust = 0, nudge_x = 0.15) +
scale_shape_identity() +
expand_limits(x = 4.1) +
scale_x_continuous(NULL, breaks = NULL) +
scale_y_continuous(NULL, breaks = NULL)
点可以有填充色和stroke两个属性,点的尺寸是两者的和。
sizes <- expand.grid(size = (0:3) * 2, stroke = (0:3) * 2)
ggplot(sizes, aes(size, stroke, size = size, stroke = stroke)) +
geom_abline(slope = -1, intercept = 6, colour = "white", size = 6) +
geom_point(shape = 21, fill = "red") +
scale_size_identity()
文本用geom_text函数绘制。
df <- data.frame(x = 1, y = 3:1, family = c("sans", "serif", "mono"))
ggplot(df, aes(x, y)) +
geom_text(aes(label = family, family = family))
中文字体的显示经常出问题,新版本的Rstudio可能已经解决了这个问题。但在不同的图形设备或不同的平台(windows、linux、mac)中可能还会有问题,关于如何解决中文问题,将专门介绍。
df <- data.frame(x = 1:4, fontface = c("plain", "bold", "italic", "bold.italic"))
ggplot(df, aes(1, x)) +
geom_text(aes(label = fontface, fontface = fontface))
水平:top = 1, middle = 0.5, bottom = 0 垂直:left = 0, center = 0.5, right = 1
just <- expand.grid(hjust = c(0, 0.5, 1), vjust = c(0, 0.5, 1))
just$label <- paste0(just$hjust, ", ", just$vjust)
ggplot(just, aes(hjust, vjust)) +
geom_point(colour = "grey70", size = 5) +
geom_text(aes(label = label, hjust = hjust, vjust = vjust))