这些是数据:
structure(list(Group.1 = c((name list)
), Group.2 = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Radio", "Video", "Engineering",
"800Mhz", "PSSRP", "Other"), class = "factor"), x = c(93.5, 208.75,
214, 48, 66.33, 71.5, 19.5, 64.75, 17, 39, 30.75, 96.75, 30,
19, 32.5, 12.75, 47.25, 14, 22.25, 12, 3, 128.5, 9.5, 303.2,
290.35, 364.05, 333.25, 11.75, 553.25, 423, 6, 496)), .Names = c("Group.1",
"Group.2", "x"), row.names = c(NA, -32L), class = "data.frame")
运行这个情节:
ggplot(data = HrSums, aes(x = Group.1, y = x, fill = Group.2)) +
geom_bar(stat = "sum", position = position_stack(reverse = TRUE)) +
coord_flip() +
labs(title = "Hours Billed, by Technician and Shop", y = "Hours Billed",
x = "Technician", fill = "Shop")
我得到这个条形图:
什么是“n”框,如何从图例中删除它(仅限)?
答案 0 :(得分:2)
我认为n框是因为geom_bar
期望计算Group.1
和Group.2
的每个组合出现的次数,而是您给出y
aes
中的值。 geom_bar
可以使用不同的统计数据而非计数,但如果您想要值的总和,则需要weight
美学。有两种方法可以执行此操作:一种使用weight = x
中的geom_bar
,另一种使用dplyr
函数预先计算总和,然后将其提供给y
。
library(tidyverse)
ggplot(df, aes(x = Group.1, fill = Group.2)) +
geom_bar(aes(weight = x), position = position_stack(reverse = T)) +
coord_flip()
df_sums <- df %>%
group_by(Group.1, Group.2) %>%
summarise(x = sum(x))
ggplot(df_sums, aes(x = Group.1, y = x, fill = Group.2)) +
geom_col(position = position_stack(reverse = T)) +
coord_flip()
答案 1 :(得分:0)
如果包括以下内容,则只会看到您期望的外观:
show.legend = c(
"x" = TRUE,
"y" = TRUE,
"alpha" = FALSE,
"color" = FALSE,
"fill" = TRUE,
"linetype" = FALSE,
"size" = FALSE,
"weight" = FALSE
)
请参见?geom_bar上的show.legend参数:
show.legend符合逻辑。该图层应包括在图例中吗? NA,默认值,包括是否映射了任何美学。永远不会 包含,并且TRUE始终包含。 它也可以是命名逻辑 向量,以精细选择要显示的美学效果。