我试图将标签添加到堆叠的条形图中,但是标签与实际数据不匹配,并且放置不正确。我能够获得标签以正确显示单独的图形,但是我不确定这次尝试做错了什么。
以下代码对我来说正常工作:
figure1 <- ggplot(agent_q1, aes(x=Region, y = Proportion, fill = Answer,
label = ifelse(Proportion > .1, scales::percent(Proportion), ""))) +
geom_bar(position = "fill", stat = "identity") +
geom_text(family = "Calibri Light", size = 5, position =
position_stack(vjust = 0.5), check_overlap = TRUE) +
scale_fill_geico(palette = "bold") +
scale_y_continuous(labels = scales::percent_format())
但是当我尝试以下代码时,标签无法正确显示:
figure2 <- ggplot(agent_q2, aes(x = Region, y = Proportion, fill =
Answer, label = ifelse(Proportion > .1, scales::percent(Proportion),
""))) +
geom_bar(position = "fill", stat = "identity") +
facet_grid(vars(rows=Question)) +
scale_fill_geico(palette = "light") +
scale_y_continuous(labels = scales::percent_format()) +
geom_text(family = "Calibri Light", size = 5, position =
position_stack(vjust = 0.5)) +
coord_flip()
数据示例:
dput(agent_q2)
structure(list(Region = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Lakeland",
"Other"), class = "factor"), Impression = c("Dissatisfied", "Dissatisfied",
"Dissatisfied", "Dissatisfied", "Dissatisfied", "Dissatisfied",
"Dissatisfied", "Satisfied", "Satisfied", "Satisfied", "Satisfied",
"Satisfied", "Satisfied", "Dissatisfied", "Dissatisfied", "Dissatisfied",
"Dissatisfied", "Dissatisfied", "Dissatisfied", "Satisfied",
"Satisfied", "Satisfied", "Satisfied", "Satisfied", "Dissatisfied",
"Dissatisfied", "Dissatisfied", "Dissatisfied", "Dissatisfied",
"Dissatisfied", "Dissatisfied", "Satisfied", "Satisfied", "Satisfied",
"Satisfied", "Satisfied", "Satisfied", "Satisfied"), Answer = structure(c(1L,
2L, 3L, 4L, 5L, 6L, NA, 1L, 2L, 3L, 4L, 5L, NA, 1L, 2L, 3L, 4L,
5L, 6L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, NA, 1L, 2L,
3L, 4L, 5L, 6L, NA), .Label = c("Completely Satisfied", "Satisfied",
"Slightly Satisfied", "Slightly Dissatisfied", "Dissatisfied",
"Completely Dissatisfied"), class = "factor"), Counts = c(4L,
25L, 22L, 19L, 20L, 20L, 1L, 15L, 30L, 24L, 3L, 2L, 2L, 29L,
39L, 17L, 15L, 7L, 4L, 32L, 33L, 7L, 3L, 1L, 8L, 36L, 33L, 16L,
7L, 10L, 1L, 20L, 33L, 16L, 3L, 1L, 1L, 2L), Question = c("The formal training you\nreceived in EDGE",
"The formal training you\nreceived in EDGE", "The formal training you\nreceived in EDGE",
"The formal training you\nreceived in EDGE", "The formal training you\nreceived in EDGE",
"The formal training you\nreceived in EDGE", "The formal training you\nreceived in EDGE",
"The formal training you\nreceived in EDGE", "The formal training you\nreceived in EDGE",
"The formal training you\nreceived in EDGE", "The formal training you\nreceived in EDGE",
"The formal training you\nreceived in EDGE", "The formal training you\nreceived in EDGE",
"The in-person,\ninstructor-led training", "The in-person,\ninstructor-led training",
"The in-person,\ninstructor-led training", "The in-person,\ninstructor-led training",
"The in-person,\ninstructor-led training", "The in-person,\ninstructor-led training",
"The in-person,\ninstructor-led training", "The in-person,\ninstructor-led training",
"The in-person,\ninstructor-led training", "The in-person,\ninstructor-led training",
"The in-person,\ninstructor-led training", "The E-learning training\n(GU Courses)",
"The E-learning training\n(GU Courses)", "The E-learning training\n(GU Courses)",
"The E-learning training\n(GU Courses)", "The E-learning training\n(GU Courses)",
"The E-learning training\n(GU Courses)", "The E-learning training\n(GU Courses)",
"The E-learning training\n(GU Courses)", "The E-learning training\n(GU Courses)",
"The E-learning training\n(GU Courses)", "The E-learning training\n(GU Courses)",
"The E-learning training\n(GU Courses)", "The E-learning training\n(GU Courses)",
"The E-learning training\n(GU Courses)"), Proportion = c(0.0213903743315508,
0.133689839572193, 0.117647058823529, 0.101604278074866, 0.106951871657754,
0.106951871657754, 0.0053475935828877, 0.0802139037433155, 0.160427807486631,
0.128342245989305, 0.0160427807486631, 0.0106951871657754, 0.0106951871657754,
0.155080213903743, 0.20855614973262, 0.0909090909090909, 0.0802139037433155,
0.0374331550802139, 0.0213903743315508, 0.171122994652406, 0.176470588235294,
0.0374331550802139, 0.0160427807486631, 0.0053475935828877, 0.0427807486631016,
0.192513368983957, 0.176470588235294, 0.0855614973262032, 0.0374331550802139,
0.053475935828877, 0.0053475935828877, 0.106951871657754, 0.176470588235294,
0.0855614973262032, 0.0160427807486631, 0.0053475935828877, 0.0053475935828877,
0.0106951871657754)), row.names = c(NA, -38L), class = "data.frame")
答案 0 :(得分:0)
您的数据具有相同类别的2倍,因此我们需要首先解决该问题:
<br>
<br>
<br>
<h1 class="title">
Employee Search
</h1>
<p>Employee search screen</p>
然后调整显示的百分比和对齐方式:
library(dplyr)
agent_q2 <- agent_q2 %>%
group_by(Answer, Question) %>%
mutate(prop_sum = sum(Proportion))
agent_for_plot <- agent_q2[!duplicated(agent_q2[c(3,7)]),]
这让我:
请注意,我没有您的调色板,也无法识别“ Calibri Light”。另外,由于某种原因,它并没有使顶部图表的值居中对齐...但是,这开始朝正确的方向进行。