cat("颜色可以是英文单词colors(),可以是rgb,rgb()可以是数字1表示的颜色是colors()[1],可以是颜色十六进制代码Hex code:#69b3a2\n")
## 颜色可以是英文单词colors(),可以是rgb,rgb()可以是数字1表示的颜色是colors()[1],可以是颜色十六进制代码Hex code:#69b3a2
library(ggplot2)
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)
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)
ggplot(iris,aes(Sepal.Length,Sepal.Width))+geom_line(lwd=0.1)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
ggplot(iris,aes(Sepal.Length,Sepal.Width))+geom_line(lwd= 0.5)
ggplot(iris,aes(Sepal.Length,Sepal.Width))+geom_line(linewidth= 2)
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) +
theme_void()
形状的名字
shape_names <- c(
"circle", paste("circle", c("open", "filled", "cross", "plus", "small")), "bullet",
"square", paste("square", c("open", "filled", "cross", "plus", "triangle")),
"diamond", paste("diamond", c("open", "filled", "plus")),
"triangle", paste("triangle", c("open", "filled", "square")),
paste("triangle down", c("open", "filled")),
"plus", "cross", "asterisk"
)
shapes <- data.frame(
shape_names = shape_names,
x = c(1:7, 1:6, 1:3, 5, 1:3, 6, 2:3, 1:3),
y = -rep(1:6, c(7, 6, 4, 4, 2, 3))
)
ggplot(shapes, aes(x, y)) +
geom_point(aes(shape = shape_names), fill = "red", size = 5) +
geom_text(aes(label = shape_names), nudge_y = -0.3, size = 3.5) +
scale_shape_identity() +
theme_void()
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))
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))
df <- data.frame(x = 1:4, fontface = c("plain", "bold", "italic", "bold.italic"),size=1:4)
ggplot(df, aes(1, x)) +
geom_text(aes(label = fontface, fontface = fontface,size=size))
中文无法修改font face,只能够通过需改字体实现。
library(showtext)
font_families() # 列出可用字体,没有的字体手动 font_add 添加
showtext_auto() # 调用 pdf cairo_pdf 等设备的时候 自动调用字体
font_add('SourceHanSerifCN-Bold',regular = 'C:/Windows/fonts/SourceHanSerifCN-Bold.otf') # 字体的路径,添加下载的字体
font_add('SourceHanSerifCN-Regular',regular = 'C:/Windows/fonts/SourceHanSerifCN-Regular.otf')
# font_install(source_han_serif()) 网络好直接通过这个函数下载
下载字体的url:https://github.com/adobe-fonts/source-han-serif/tree/release#downloading-source-han-serif
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))
以条形图为例子:
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(ggcharts)
dreaded_lang <- tibble::tribble(
~language, ~pct,
"VBA", 75.2,
"Objective-C", 68.7,
"Assembly", 64.4,
"C", 57.5,
"PHP", 54.2,
"Erlang", 52.6,
"Ruby", 49.7,
"R", 48.3,
"C++", 48.0,
"Java", 46.6
)
chart <- dreaded_lang %>%
bar_chart(language, pct) %>%
print()
当需要在条形图中添加注释时,你可以使用geom_text() 或者 geom_label()。这两个函数都需要x和y的坐标值,它将告诉ggplot2在哪里显示文本。 也可以使用ggrepel包中的geom_text_repel和geom_label_repel
library(patchwork)
p1 <- chart + geom_text(aes(label = pct))
p2 <- chart + geom_label(aes(label = pct))
p3 <- chart + ggrepel::geom_text_repel(aes(label = pct))
p4 <- chart + ggrepel::geom_text_repel(aes(label = pct))
p1+p2+p3+p4
默认情况下,标签是以y值居中对齐的。 此种对齐方式浏览起来不够美观。 如果要使标签左对齐,可以将hjust参数设置为0或”left”。
p1 <- chart + geom_text(aes(label = pct, hjust = "left"))
p2 <- chart + geom_label(aes(label = pct, hjust = 0))
p3 <- chart + ggrepel::geom_text_repel(aes(label = pct, hjust = "left"))
p4 <- chart + ggrepel::geom_text_repel(aes(label = pct, hjust = 0))
p1+p2+p3+p4
把hjust设置为负数,让标签与条形保持距离;同时增加y轴与条形头部之间的距离,使得图像更加美观。
p1 <- chart + geom_text(aes(label = pct, hjust = -0.2))
p2 <- chart + geom_label(aes(label = pct, hjust = -0.2))
p3 <- chart + ggrepel::geom_text_repel(aes(label = pct, hjust = -0.2))
p4 <- chart + ggrepel::geom_text_repel(aes(label = pct, hjust = -0.2))
p1+p2+p3+p4
可以将标签至于条形之中
p1 <- chart + geom_text(aes(label = pct, hjust = 1))
p2 <- chart + geom_label(aes(label = pct, hjust = 1))
p3 <- chart + ggrepel::geom_text_repel(aes(label = pct, hjust = 1))
p4 <- chart + ggrepel::geom_text_repel(aes(label = pct, hjust = 1))
p1+p2+p3+p4
修改颜色,大小,color是在aes()之外定义的。
p1 <- chart + geom_text(aes(label = pct, hjust = 1.2), color = "yellow",size=1)
p2 <- chart + geom_label(aes(label = pct, hjust = 1.2), color = "yellow",size=1)
p3 <- chart + ggrepel::geom_text_repel(aes(label = pct, hjust = 1.2), color = "yellow",size=1)
p4 <- chart + ggrepel::geom_text_repel(aes(label = pct, hjust = 1.2), color = "yellow",size=1)
p1+p2+p3+p4