R语言 如何正确定位带引线的标签在多面饼图上?

8i9zcol2  于 11个月前  发布在  其他
关注(0)|答案(3)|浏览(127)

我试图重新创建一个数字,但我无法得到饼图内的标签的位置的权利。
这是我现在的代码:

ggplot(flying63, aes(x = '', y = percentage, fill = baby_on_plane)) +
  geom_col(width = 1, position = position_fill()) +
  coord_polar('y') +
  facet_grid(gender ~ age) +
  geom_label_repel(aes(label = sprintf("%.2f%%", percentage)),
             position = position_fill(), size = 3, max.time = 8) +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank())

字符串
它看起来像这样:

但是,我希望标签看起来像这样:

更新:

输入数据:

flying63 <- structure(list(gender = c("Female", "Female", "Female", "Female", 
"Male", "Male", "Male", "Male", "Female", "Female", "Female", 
"Female", "Male", "Male", "Male", "Male", "Female", "Female", 
"Female", "Female", "Male", "Male", "Male", "Male"), age = structure(c(1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L), levels = c("18-29", "30-44", "45-60", 
"> 60"), class = c("ordered", "factor")), baby_on_plane = c("Yes, very rude", 
"Yes, very rude", "Yes, very rude", "Yes, very rude", "Yes, very rude", 
"Yes, very rude", "Yes, very rude", "Yes, very rude", "Yes, somewhat rude", 
"Yes, somewhat rude", "Yes, somewhat rude", "Yes, somewhat rude", 
"Yes, somewhat rude", "Yes, somewhat rude", "Yes, somewhat rude", 
"Yes, somewhat rude", "No, not at all rude", "No, not at all rude", 
"No, not at all rude", "No, not at all rude", "No, not at all rude", 
"No, not at all rude", "No, not at all rude", "No, not at all rude"
), n = c(5L, 5L, 6L, 6L, 8L, 11L, 15L, 5L, 23L, 11L, 14L, 10L, 
19L, 34L, 19L, 17L, 47L, 62L, 75L, 71L, 35L, 50L, 74L, 55L), 
    percentage = c(6.66666666666667, 6.41025641025641, 6.31578947368421, 
    6.89655172413793, 12.9032258064516, 11.5789473684211, 13.8888888888889, 
    6.49350649350649, 30.6666666666667, 14.1025641025641, 14.7368421052632, 
    11.4942528735632, 30.6451612903226, 35.7894736842105, 17.5925925925926, 
    22.0779220779221, 62.6666666666667, 79.4871794871795, 78.9473684210526, 
    81.6091954022989, 56.4516129032258, 52.6315789473684, 68.5185185185185, 
    71.4285714285714), label_position = c(3.33333333333333, 3.2051282051282, 
    3.1578947368421, 3.44827586206896, 6.4516129032258, 5.78947368421055, 
    6.94444444444445, 3.24675324675324, 15.3333333333333, 7.05128205128205, 
    7.3684210526316, 5.7471264367816, 15.3225806451613, 17.8947368421052, 
    8.7962962962963, 11.038961038961, 31.3333333333333, 39.7435897435898, 
    39.4736842105263, 40.8045977011494, 28.2258064516129, 26.3157894736842, 
    34.2592592592593, 35.7142857142857)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -24L), groups = structure(list(
    gender = c("Female", "Female", "Female", "Female", "Female", 
    "Female", "Female", "Female", "Female", "Female", "Female", 
    "Female", "Male", "Male", "Male", "Male", "Male", "Male", 
    "Male", "Male", "Male", "Male", "Male", "Male"), age = structure(c(1L, 
    1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 
    2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), levels = c("18-29", "30-44", 
    "45-60", "> 60"), class = c("ordered", "factor")), baby_on_plane = c("No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude"), .rows = structure(list(
        17L, 9L, 1L, 18L, 10L, 2L, 19L, 11L, 3L, 20L, 12L, 4L, 
        21L, 13L, 5L, 22L, 14L, 6L, 23L, 15L, 7L, 24L, 16L, 8L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -24L), .drop = TRUE, class = c("tbl_df", 
"tbl", "data.frame")))

ntjbwcob

ntjbwcob1#

library(tidyverse)
library(ggrepel)

datasets::iris %>% 
  transmute(age = as.numeric(cut_number(Sepal.Length,3)),
         gender = as.numeric(cut_number(Petal.Length,2)),
         respond = Species) %>% 
  mutate(n1 = n(), .by = c(age, gender, respond)) %>%
  mutate(n2 = n(), .by = c(age, gender)) %>%
  mutate(percentage = 100 * n1/n2, .by = c(age, gender)) %>% 
  unique() %>% 
  arrange(age, gender) %>% 
ggplot(aes(x = '', y = percentage, fill = respond)) +
  geom_col(width = 1, position = position_fill()) +
  coord_polar('y') +
  facet_grid(gender ~ age) +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank()) +
  geom_label_repel(aes(label = sprintf("%.0f%%", percentage)),
                   position = position_fill(vjust = 0.2), size = 3, max.time = 8, 
                   box.padding = 1)

字符串
x1c 0d1x的数据
创建于2023-12-28带有reprex v2.0.2

hpcdzsge

hpcdzsge2#

我捏造了一些数据,所以我可以在这方面的工作。我认为你正在寻找的是box.padding = ...参数。你可以发挥价值,以改变偏移量的长度。

flying63 <- tibble(percentage = rep(c(80,7,13),4),
                   baby_on_plane=rep(c('Not Rude','Somewhat','Very'),4),
                   gender = sample(c('M','F'),12, replace = T),
                   age = sample(c('18','30','45','60'),12,replace = T))

ggplot(flying63, aes(x = '', y = percentage, fill = baby_on_plane)) +
  geom_col(width = 1, position = position_fill()) +
  coord_polar('y') +
  facet_grid(gender ~ age) +
  geom_label_repel(aes(label = sprintf("%.2f%%", percentage)),
                   position = position_fill(), size = 3, max.time = 8,
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                   box.padding = 1) + # This is the change
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank())

字符串


的数据

e4yzc0pl

e4yzc0pl3#

要在堆叠条形图中的每个条形中间放置标签,您需要设置position_stack()的vjust参数:
你需要的是position = position_stack(vjust = 0.5)这里是原始的例子:

library(dplyr)
library(ggplot2)

flying63 %>% 
  ggplot(aes(x = 1, y = percentage, fill = baby_on_plane)) +
  geom_col() +
  geom_text(aes(label = paste0(round(percentage, 1), "%")), 
            position = position_stack(vjust = 0.5),
            size = 6)+
  coord_polar("y", start=0) +
  facet_grid(gender ~ paste("Age: ",age)) +
  labs(fill = "Baby on plane")+
  theme_void()+
  theme(text = element_text(size=16),
        legend.position = "bottom")

字符串


的数据
数据类型:

flying63 <- structure(list(gender = c("Female", "Female", "Female", "Female", 
"Male", "Male", "Male", "Male", "Female", "Female", "Female", 
"Female", "Male", "Male", "Male", "Male", "Female", "Female", 
"Female", "Female", "Male", "Male", "Male", "Male"), age = structure(c(1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L), levels = c("18-29", "30-44", "45-60", 
"> 60"), class = c("ordered", "factor")), baby_on_plane = c("Yes, very rude", 
"Yes, very rude", "Yes, very rude", "Yes, very rude", "Yes, very rude", 
"Yes, very rude", "Yes, very rude", "Yes, very rude", "Yes, somewhat rude", 
"Yes, somewhat rude", "Yes, somewhat rude", "Yes, somewhat rude", 
"Yes, somewhat rude", "Yes, somewhat rude", "Yes, somewhat rude", 
"Yes, somewhat rude", "No, not at all rude", "No, not at all rude", 
"No, not at all rude", "No, not at all rude", "No, not at all rude", 
"No, not at all rude", "No, not at all rude", "No, not at all rude"
), n = c(5L, 5L, 6L, 6L, 8L, 11L, 15L, 5L, 23L, 11L, 14L, 10L, 
19L, 34L, 19L, 17L, 47L, 62L, 75L, 71L, 35L, 50L, 74L, 55L), 
    percentage = c(6.66666666666667, 6.41025641025641, 6.31578947368421, 
    6.89655172413793, 12.9032258064516, 11.5789473684211, 13.8888888888889, 
    6.49350649350649, 30.6666666666667, 14.1025641025641, 14.7368421052632, 
    11.4942528735632, 30.6451612903226, 35.7894736842105, 17.5925925925926, 
    22.0779220779221, 62.6666666666667, 79.4871794871795, 78.9473684210526, 
    81.6091954022989, 56.4516129032258, 52.6315789473684, 68.5185185185185, 
    71.4285714285714), label_position = c(3.33333333333333, 3.2051282051282, 
    3.1578947368421, 3.44827586206896, 6.4516129032258, 5.78947368421055, 
    6.94444444444445, 3.24675324675324, 15.3333333333333, 7.05128205128205, 
    7.3684210526316, 5.7471264367816, 15.3225806451613, 17.8947368421052, 
    8.7962962962963, 11.038961038961, 31.3333333333333, 39.7435897435898, 
    39.4736842105263, 40.8045977011494, 28.2258064516129, 26.3157894736842, 
    34.2592592592593, 35.7142857142857)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -24L), groups = structure(list(
    gender = c("Female", "Female", "Female", "Female", "Female", 
    "Female", "Female", "Female", "Female", "Female", "Female", 
    "Female", "Male", "Male", "Male", "Male", "Male", "Male", 
    "Male", "Male", "Male", "Male", "Male", "Male"), age = structure(c(1L, 
    1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 
    2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), levels = c("18-29", "30-44", 
    "45-60", "> 60"), class = c("ordered", "factor")), baby_on_plane = c("No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude", "No, not at all rude", 
    "Yes, somewhat rude", "Yes, very rude"), .rows = structure(list(
        17L, 9L, 1L, 18L, 10L, 2L, 19L, 11L, 3L, 20L, 12L, 4L, 
        21L, 13L, 5L, 22L, 14L, 6L, 23L, 15L, 7L, 24L, 16L, 8L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -24L), .drop = TRUE, class = c("tbl_df", 
"tbl", "data.frame")))

相关问题