R 中的华夫饼图:图例文本仅由 "A"、"B"、"C" 组成;如何改变?
Waffle chart in R: Legend text merely consists of "A", "B", "C"; how to change?
我有这个数据框 DF
:
|total | n |
|------|----|
| 0 | 19 |
| 1 | 16 |
| 2 | 23 |
| 3 | 31 |
| 4 | 20 |
| 5 | 95 |
... 或者,使其可重现:
> dput(DF)
structure(list(total = c(0, 1, 2, 3, 4, 5), n = c(19L, 16L, 23L,
31L, 20L, 95L)), row.names = c(NA, 6L), class = "data.frame")
现在,如果我用它制作华夫饼图表:
library(waffle)
waffle(DF$n, rows=6, size=0.6,
colors=c("#EAECEC", "#DFE0E2", "#B3B6BC", "#888D96", "#4E5656",
"#0A0B0B"),
#title="Legal Databases",
xlab="1 square == 1 country")
...我得到一个由“A”、“B”、“C”等组成的图例文本:
如何更改图例文本?我希望它显示为“分数:0”、“分数:1”等,直到“分数:5”而不是“A”、“B”、“C”等
我尝试使用 中的 names()
解决方案,但没有成功。
根据waffle
If the vector is not named or only partially named, capital letters will be used instead.
因此,我们可以创建一个命名向量并传递
library(waffle)
v1 <- setNames(DF$n, paste0("Score: ", seq_along(DF$n)-1))
waffle(v1, rows=6, size=0.6,
colors=c("#EAECEC", "#DFE0E2", "#B3B6BC", "#888D96", "#4E5656",
"#0A0B0B"),
#title="Legal Databases",
xlab="1 square == 1 country")
-输出
与 akrun 类似的解决方案!在文档 https://cran.r-project.org/web/packages/waffle/waffle.pdf 中有参数 parts
= 用于图表的命名向量值:
library(waffle)
parts=c(DF$n)
names(parts) = paste0("Score: ", DF$total)
waffle(parts, rows=6, size=0.6,
colors=c("#EAECEC", "#DFE0E2", "#B3B6BC", "#888D96", "#4E5656",
"#0A0B0B"),
#title="Legal Databases",
xlab="1 square == 1 country")
我有这个数据框 DF
:
|total | n |
|------|----|
| 0 | 19 |
| 1 | 16 |
| 2 | 23 |
| 3 | 31 |
| 4 | 20 |
| 5 | 95 |
... 或者,使其可重现:
> dput(DF)
structure(list(total = c(0, 1, 2, 3, 4, 5), n = c(19L, 16L, 23L,
31L, 20L, 95L)), row.names = c(NA, 6L), class = "data.frame")
现在,如果我用它制作华夫饼图表:
library(waffle)
waffle(DF$n, rows=6, size=0.6,
colors=c("#EAECEC", "#DFE0E2", "#B3B6BC", "#888D96", "#4E5656",
"#0A0B0B"),
#title="Legal Databases",
xlab="1 square == 1 country")
...我得到一个由“A”、“B”、“C”等组成的图例文本:
如何更改图例文本?我希望它显示为“分数:0”、“分数:1”等,直到“分数:5”而不是“A”、“B”、“C”等
我尝试使用 names()
解决方案,但没有成功。
根据waffle
If the vector is not named or only partially named, capital letters will be used instead.
因此,我们可以创建一个命名向量并传递
library(waffle)
v1 <- setNames(DF$n, paste0("Score: ", seq_along(DF$n)-1))
waffle(v1, rows=6, size=0.6,
colors=c("#EAECEC", "#DFE0E2", "#B3B6BC", "#888D96", "#4E5656",
"#0A0B0B"),
#title="Legal Databases",
xlab="1 square == 1 country")
-输出
与 akrun 类似的解决方案!在文档 https://cran.r-project.org/web/packages/waffle/waffle.pdf 中有参数 parts
= 用于图表的命名向量值:
library(waffle)
parts=c(DF$n)
names(parts) = paste0("Score: ", DF$total)
waffle(parts, rows=6, size=0.6,
colors=c("#EAECEC", "#DFE0E2", "#B3B6BC", "#888D96", "#4E5656",
"#0A0B0B"),
#title="Legal Databases",
xlab="1 square == 1 country")