如何在 R 中的饼图上更改字体系列和文本位置

How to change Font Family and position of text on a Pie chart in R

我在 R 中有下面的饼图。
我想: 1- 将字体系列更改为 Arial Narrow。 2- 将内部图表百分比标签从中心移得更远。 我该怎么做?

此外,如果可能的话,我很想知道 R 选择图例的顺序背后的逻辑是什么?为什么不是从大到小?

mydf <- data.frame(
causes=c("a", "b", "c", "d", "other"),
freq=c(100, 80, 78, 75, 120),#
share= 100*c(100/sum(freq), 80/sum(freq), 78/sum(freq), 75/sum(freq), 120/sum(freq))
 )
sum(freq)
ggplot(mydf, aes("", share, fill = causes)) +



geom_bar(width = 1, size = 1, color = "white", stat = "identity") +
  coord_polar("y") +
  geom_text(aes(label = paste0(round(share), "%")), 
            position = position_stack(vjust = 0.5), size=5, col="white") +
  labs(x = NULL, y = NULL, fill = NULL, 
       title = "leading causes of death in US") +
  guides(fill = guide_legend(reverse = TRUE)) +
  scale_fill_manual(values = c("#003B6D", "#6699CC", "#BDBDBD", "#676767", "#EBEDF3")) +
  theme_classic() +
  theme(axis.line = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    plot.title = element_text(hjust = 0.5, color = "#666666"))

试试这个方法

mydf$Label  <- round(((mydf$freq/sum(mydf$freq))*100),2)
mydf <- mydf %>%
  mutate(end = 2 * pi * cumsum(Label)/sum(Label),
         start = lag(end, default = 0),
         middle = 0.5 * (start + end),
         hjust = ifelse(middle > pi, 1, 0),
         vjust = ifelse(middle < pi/2 | middle > 3 * pi/2, 0, 1))


ggplot(mydf) + 
  ggforce::geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
                   start = start, end = end, fill = causes)) +
  geom_text(aes(x = 1.05 * sin(middle), y = 1.05 * cos(middle), label = Label,
                hjust = hjust, vjust = vjust)) +
  coord_fixed() +
  labs(x = NULL, y = NULL, fill = NULL, 
       title = "leading causes of death in US") +
  guides(fill = guide_legend(reverse = TRUE)) +
  scale_fill_manual(values = c("#003B6D", "#6699CC", "#BDBDBD", "#676767", "#EBEDF3")) +
  theme_classic() +
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        plot.title = element_text(hjust = 0.5, color = "#666666"))

里面

mydf <- mydf %>%
  mutate(end = 2 * pi * cumsum(Label)/sum(Label),
         start = lag(end, default = 0),
         middle = 0.5 * (start + end),
         hjust = ifelse(middle > pi, 0, 1),
         vjust = ifelse(middle < pi/2 | middle > 3 * pi/2, 1, 0))


ggplot(mydf) + 
  ggforce::geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
                            start = start, end = end, fill = causes)) +
  geom_text(aes(x = 0.95 * sin(middle), y = 0.95 * cos(middle), label = Label,
                hjust = hjust, vjust = vjust)) +
  coord_fixed() +
  labs(x = NULL, y = NULL, fill = NULL, 
       title = "leading causes of death in US") +
  guides(fill = guide_legend(reverse = TRUE)) +
  scale_fill_manual(values = c("#003B6D", "#6699CC", "#BDBDBD", "#676767", "#EBEDF3")) +
  theme_classic() +
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        plot.title = element_text(hjust = 0.5, color = "#666666"))

字体

ggplot(mydf) + 
  ggforce::geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
                            start = start, end = end, fill = causes)) +
  geom_text(aes(x = 0.95 * sin(middle), y = 0.95 * cos(middle), label = Label,
                hjust = hjust, vjust = vjust), fontface = "bold") + #inside chart here
  coord_fixed() +
  labs(x = NULL, y = NULL, fill = NULL, 
       title = "leading causes of death in US") +
  guides(fill = guide_legend(reverse = TRUE)) +
  scale_fill_manual(values = c("#003B6D", "#6699CC", "#BDBDBD", "#676767", "#EBEDF3")) +
  theme_classic() +
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        plot.title = element_text(hjust = 0.5, color = "#666666"),
        text = element_text(family = "sans")) #title here

对于 fontface,“普通”、“粗体”、“斜体”、“bold.italic”可用,对于 element_text(family),单色

sans
serif
Courier
Helvetica
Times
AvantGarde
Bookman
Helvetica-Narrow
NewCenturySchoolbook
Palatino
URWGothic
URWBookman
NimbusMon
URWHelvetica
NimbusSan
NimbusSanCond
CenturySch
URWPalladio
URWTimes
NimbusRom

可用。