R 中的反向堆叠条形图导致奇怪的定位
Reversed Stacked bar chart in R leads to weird positioning
我正在尝试在 R 中创建堆积条形图,并颠倒顺序。
df <- structure(list(HouseholdEarnings = structure(c(2L, 2L, 3L, 3L,
3L, 2L, 2L, 1L, 4L, 2L, 3L, 6L, 5L, 4L, 2L, 1L, 2L, 3L, 1L, 3L,
3L, 3L, 2L, 2L, 2L, 6L, 2L, 5L, 2L, 6L, 2L, 2L, 3L, 1L, 3L, 2L,
4L, 2L, 1L, 3L, 2L, 1L, 5L, 3L, 3L, 3L, 2L, 2L, 3L, 2L, 1L, 3L,
3L, 4L, 2L, 2L, 3L, 2L, 3L, 3L, 2L, 2L, 6L, 4L, 3L, 3L, 2L, 3L,
4L, 2L, 2L, 3L, 2L, 4L, 1L, 1L, 2L, 2L, 2L, 4L, 4L, 6L, 3L, 4L,
2L, 4L, 4L, 2L, 4L, 6L, 3L, 4L, 1L, 2L, 4L, 2L, 2L, 5L, 3L, 2L
), .Label = c("Below ,000 per month", ",000 - ,999", ",000 - ,999",
",000 - ,999", ",000 - ,999", ",000 & above"), class = c("ordered",
"factor"))), row.names = c(NA, -100L), class = "data.frame")
根据其他线程的解决方案,输入 geom_col(position = position_stack(reverse = TRUE))
解决了问题。
df %>% group_by(HouseholdEarnings) %>% summarise(Count = n()) %>% mutate(Proportion=Count/sum(Count), group='All') %>%
as.data.frame() %>% mutate_if(is.numeric, round, digits = 2) %>%
ggplot(aes(fill=HouseholdEarnings, y=Proportion, x=group)) +
geom_col(position = position_stack(reverse = T), color = "black") +
geom_text(aes(label=paste0(Proportion*100, "%")),
position=position_stack(vjust=0.5), colour="white",size=3) +
coord_flip()
但是,现在我的标签错位了(还在原来的位置):
不使用也可以 reverse = T
:
df %>% group_by(HouseholdEarnings) %>% summarise(Count = n()) %>% mutate(Proportion=Count/sum(Count), group='All') %>%
as.data.frame() %>% mutate_if(is.numeric, round, digits = 2) %>%
ggplot(aes(fill=HouseholdEarnings, y=Proportion, x=group)) +
geom_col(position = position_stack(), color = "black") +
geom_text(aes(label=paste0(Proportion*100, "%")),
position=position_stack(vjust=0.5), colour="white",size=3) +
coord_flip()
编辑:我发现只有标签还固定在原来的位置,请问有什么办法可以翻转它们吗?
您也需要在 geom_text
中传递 reverse = TRUE
:
df %>% group_by(HouseholdEarnings) %>% summarise(Count = n()) %>% mutate(Proportion=Count/sum(Count), group='All') %>%
as.data.frame() %>% mutate_if(is.numeric, round, digits = 2) %>%
ggplot(aes(fill=HouseholdEarnings, y=Proportion, x=group)) +
geom_col(position = position_stack(reverse = T), color = "black") +
geom_text(aes(label=paste0(Proportion*100, "%")),
position=position_stack(reverse = TRUE, vjust = 0.5), colour="white",size=3) +
coord_flip()
它能回答您的问题吗?
我正在尝试在 R 中创建堆积条形图,并颠倒顺序。
df <- structure(list(HouseholdEarnings = structure(c(2L, 2L, 3L, 3L,
3L, 2L, 2L, 1L, 4L, 2L, 3L, 6L, 5L, 4L, 2L, 1L, 2L, 3L, 1L, 3L,
3L, 3L, 2L, 2L, 2L, 6L, 2L, 5L, 2L, 6L, 2L, 2L, 3L, 1L, 3L, 2L,
4L, 2L, 1L, 3L, 2L, 1L, 5L, 3L, 3L, 3L, 2L, 2L, 3L, 2L, 1L, 3L,
3L, 4L, 2L, 2L, 3L, 2L, 3L, 3L, 2L, 2L, 6L, 4L, 3L, 3L, 2L, 3L,
4L, 2L, 2L, 3L, 2L, 4L, 1L, 1L, 2L, 2L, 2L, 4L, 4L, 6L, 3L, 4L,
2L, 4L, 4L, 2L, 4L, 6L, 3L, 4L, 1L, 2L, 4L, 2L, 2L, 5L, 3L, 2L
), .Label = c("Below ,000 per month", ",000 - ,999", ",000 - ,999",
",000 - ,999", ",000 - ,999", ",000 & above"), class = c("ordered",
"factor"))), row.names = c(NA, -100L), class = "data.frame")
根据其他线程的解决方案,输入 geom_col(position = position_stack(reverse = TRUE))
解决了问题。
df %>% group_by(HouseholdEarnings) %>% summarise(Count = n()) %>% mutate(Proportion=Count/sum(Count), group='All') %>%
as.data.frame() %>% mutate_if(is.numeric, round, digits = 2) %>%
ggplot(aes(fill=HouseholdEarnings, y=Proportion, x=group)) +
geom_col(position = position_stack(reverse = T), color = "black") +
geom_text(aes(label=paste0(Proportion*100, "%")),
position=position_stack(vjust=0.5), colour="white",size=3) +
coord_flip()
但是,现在我的标签错位了(还在原来的位置):
不使用也可以 reverse = T
:
df %>% group_by(HouseholdEarnings) %>% summarise(Count = n()) %>% mutate(Proportion=Count/sum(Count), group='All') %>%
as.data.frame() %>% mutate_if(is.numeric, round, digits = 2) %>%
ggplot(aes(fill=HouseholdEarnings, y=Proportion, x=group)) +
geom_col(position = position_stack(), color = "black") +
geom_text(aes(label=paste0(Proportion*100, "%")),
position=position_stack(vjust=0.5), colour="white",size=3) +
coord_flip()
编辑:我发现只有标签还固定在原来的位置,请问有什么办法可以翻转它们吗?
您也需要在 geom_text
中传递 reverse = TRUE
:
df %>% group_by(HouseholdEarnings) %>% summarise(Count = n()) %>% mutate(Proportion=Count/sum(Count), group='All') %>%
as.data.frame() %>% mutate_if(is.numeric, round, digits = 2) %>%
ggplot(aes(fill=HouseholdEarnings, y=Proportion, x=group)) +
geom_col(position = position_stack(reverse = T), color = "black") +
geom_text(aes(label=paste0(Proportion*100, "%")),
position=position_stack(reverse = TRUE, vjust = 0.5), colour="white",size=3) +
coord_flip()
它能回答您的问题吗?