包括条形标签 "inside" ggplot 绘制条形图
Include bar labels "inside" ggplot plot bar chart
我需要将所有标签放在 ggplot 的条形“内部”。
我遇到了问题,因为我需要对变量进行分组,然后将标签包含在条形图中。
这是我的代码:
df <- structure(list(name = c("Player 1", "Player 2", "Player 3",
"Player 4", "Player 5", "Player 6", "Player 7", "Player 8",
"Player 9", "Player 10", "Player 11", "Player 12", "Player 13"
), pos = c("Lateral", "Lateral", "Central", "Central",
"Meio Campo", "Extremo", "Extremo", "Meio Campo", "Avancado",
"Meio Campo", "Avancado", "Meio Campo", "Lateral"), total = c(9263L,
10451L, 9845L, 9719L, 11650L, 10831L, 11143L, 8816L, 7666L, 9587L,
2703L, 2085L, 1282L)), row.names = c(NA, -13L), class = c("tbl_df",
"tbl", "data.frame"))
这是我到目前为止尝试过的:
df %>% mutate(name = fct_reorder(name,total)) %>% ggplot(aes(pos, total,fill = pos, group = total,label = name)) +
geom_col(show.legend = FALSE,
position = position_dodge2(),width = .5) +
geom_label(aes(group = name),color = "white",
position = position_dodge2(width = .9),
show.legend = FALSE) + coord_flip()
一个选项是使用 geom_text
并在栏内显示标签。您可以使用以下代码:
library(tidyverse)
df %>% mutate(name = fct_reorder(name,total)) %>% ggplot(aes(pos, total,fill = pos, group = total,label = name)) +
geom_col(show.legend = FALSE,
position = position_dodge2(),width = .5) +
geom_text(aes(group = name),color = "white",
position = position_dodge2(width = 0.5),
show.legend = FALSE, hjust = 1.2, size = 2) + coord_flip()
输出:
要在条内移动标签,请将标签的闪避宽度设置为与条的宽度相同,并使用 hjust=1
将标签右对齐。此外,对于小条,我建议减小字体大小,并根据个人喜好删除标签的轮廓:
library(ggplot2)
library(dplyr)
library(forcats)
df %>%
mutate(NOME = fct_reorder(NOME, TOTAL.DISTANCE)) %>%
ggplot(aes(PLAYER.POSITION, TOTAL.DISTANCE, fill = PLAYER.POSITION, label = NOME)) +
geom_col(aes(group = NOME),
show.legend = FALSE,
position = position_dodge2(), width = .5
) +
geom_label(aes(group = NOME),
color = "white", hjust = 1, label.size = 0,
position = position_dodge2(width = .5),
show.legend = FALSE, size = 8 / .pt, label.padding = unit(2, "pt")
) +
coord_flip()
我需要将所有标签放在 ggplot 的条形“内部”。 我遇到了问题,因为我需要对变量进行分组,然后将标签包含在条形图中。
这是我的代码:
df <- structure(list(name = c("Player 1", "Player 2", "Player 3",
"Player 4", "Player 5", "Player 6", "Player 7", "Player 8",
"Player 9", "Player 10", "Player 11", "Player 12", "Player 13"
), pos = c("Lateral", "Lateral", "Central", "Central",
"Meio Campo", "Extremo", "Extremo", "Meio Campo", "Avancado",
"Meio Campo", "Avancado", "Meio Campo", "Lateral"), total = c(9263L,
10451L, 9845L, 9719L, 11650L, 10831L, 11143L, 8816L, 7666L, 9587L,
2703L, 2085L, 1282L)), row.names = c(NA, -13L), class = c("tbl_df",
"tbl", "data.frame"))
这是我到目前为止尝试过的:
df %>% mutate(name = fct_reorder(name,total)) %>% ggplot(aes(pos, total,fill = pos, group = total,label = name)) +
geom_col(show.legend = FALSE,
position = position_dodge2(),width = .5) +
geom_label(aes(group = name),color = "white",
position = position_dodge2(width = .9),
show.legend = FALSE) + coord_flip()
一个选项是使用 geom_text
并在栏内显示标签。您可以使用以下代码:
library(tidyverse)
df %>% mutate(name = fct_reorder(name,total)) %>% ggplot(aes(pos, total,fill = pos, group = total,label = name)) +
geom_col(show.legend = FALSE,
position = position_dodge2(),width = .5) +
geom_text(aes(group = name),color = "white",
position = position_dodge2(width = 0.5),
show.legend = FALSE, hjust = 1.2, size = 2) + coord_flip()
输出:
要在条内移动标签,请将标签的闪避宽度设置为与条的宽度相同,并使用 hjust=1
将标签右对齐。此外,对于小条,我建议减小字体大小,并根据个人喜好删除标签的轮廓:
library(ggplot2)
library(dplyr)
library(forcats)
df %>%
mutate(NOME = fct_reorder(NOME, TOTAL.DISTANCE)) %>%
ggplot(aes(PLAYER.POSITION, TOTAL.DISTANCE, fill = PLAYER.POSITION, label = NOME)) +
geom_col(aes(group = NOME),
show.legend = FALSE,
position = position_dodge2(), width = .5
) +
geom_label(aes(group = NOME),
color = "white", hjust = 1, label.size = 0,
position = position_dodge2(width = .5),
show.legend = FALSE, size = 8 / .pt, label.padding = unit(2, "pt")
) +
coord_flip()