堆叠条形图,其中每个堆叠缩放为总和为 100% + geom_text(),ggplot geom_bar
stacked barplot where each stack is scaled to sum to 100% + geom_text(), ggplot geom_bar
我想扩展这里发布的问题:
Create stacked barplot where each stack is scaled to sum to 100%,参数 geom_bar
设置为 position = "fill"
的分散条形图。
library(ggplot2)
library(dplyr)
library(tidyr)
dat <- read.table(text = " ONE TWO THREE
1 23 234 324
2 34 534 12
3 56 324 124
4 34 234 124
5 123 534 654",sep = "",header = TRUE)
# Add an id variable for the filled regions and reshape
datm <- dat %>%
mutate(ind = factor(row_number())) %>%
gather(variable, value, -ind)
ggplot(datm, aes(x = variable, y = value, fill = ind)) +
geom_bar(position = "fill",stat = "identity") +
# or:
# geom_bar(position = position_fill(), stat = "identity")
scale_y_continuous(labels = scales::percent_format())
假设您想添加细分标签。如果我遗漏 position = "fill",
它对我有用(即使它弄乱了 y 轴刻度)。
ggplot(datm, aes(x = variable, y = value, fill = ind)) +
geom_bar(stat = "identity") +
# or:
# geom_bar(position = position_fill(), stat = "identity")
scale_y_continuous(labels = scales::percent_format())+
geom_text(label= 'bla')
但是,如果我添加 position = "fill",
,情节就会一团糟。条形消失,因为它们被边缘化到最大 100 的比例,并且标签看起来与灰色区域的条形分离。
ggplot(datm, aes(x = variable, y = value, fill = ind)) +
geom_bar(position = "fill", stat = "identity") +
# or:
# geom_bar(position = position_fill(), stat = "identity")
scale_y_continuous(labels = scales::percent_format())+
geom_text(label= 'bla')
为什么?如何?谢谢!
您还需要在文本图层上应用填充位置调整。您可以通过调整 vjust
参数来控制文本相对于边界的显示位置。
library(ggplot2)
library(dplyr)
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
dat <- structure(list(ONE = c(23L, 34L, 56L, 34L, 123L),
TWO = c(234L, 534L, 324L, 234L, 534L),
THREE = c(324L, 12L, 124L, 124L, 654L)),
class = "data.frame",
row.names = c("1", "2", "3", "4", "5"))
# Add an id variable for the filled regions and reshape
datm <- dat %>%
mutate(ind = factor(row_number())) %>%
gather(variable, value, -ind)
ggplot(datm, aes(x = variable, y = value, fill = ind)) +
geom_col(position = "fill") +
scale_y_continuous(labels = scales::percent_format())+
geom_text(label= 'bla', position = position_fill(vjust = 0.5))
由 reprex package (v0.3.0)
于 2021 年 1 月 19 日创建
我只是将此注册为答案,这样浏览未解答问题的人就不会偶然发现这个问题。在评论中建议 1 行修复对我来说更省力。
我想扩展这里发布的问题:
Create stacked barplot where each stack is scaled to sum to 100%,参数 geom_bar
设置为 position = "fill"
的分散条形图。
library(ggplot2)
library(dplyr)
library(tidyr)
dat <- read.table(text = " ONE TWO THREE
1 23 234 324
2 34 534 12
3 56 324 124
4 34 234 124
5 123 534 654",sep = "",header = TRUE)
# Add an id variable for the filled regions and reshape
datm <- dat %>%
mutate(ind = factor(row_number())) %>%
gather(variable, value, -ind)
ggplot(datm, aes(x = variable, y = value, fill = ind)) +
geom_bar(position = "fill",stat = "identity") +
# or:
# geom_bar(position = position_fill(), stat = "identity")
scale_y_continuous(labels = scales::percent_format())
假设您想添加细分标签。如果我遗漏 position = "fill",
它对我有用(即使它弄乱了 y 轴刻度)。
ggplot(datm, aes(x = variable, y = value, fill = ind)) +
geom_bar(stat = "identity") +
# or:
# geom_bar(position = position_fill(), stat = "identity")
scale_y_continuous(labels = scales::percent_format())+
geom_text(label= 'bla')
但是,如果我添加 position = "fill",
,情节就会一团糟。条形消失,因为它们被边缘化到最大 100 的比例,并且标签看起来与灰色区域的条形分离。
ggplot(datm, aes(x = variable, y = value, fill = ind)) +
geom_bar(position = "fill", stat = "identity") +
# or:
# geom_bar(position = position_fill(), stat = "identity")
scale_y_continuous(labels = scales::percent_format())+
geom_text(label= 'bla')
为什么?如何?谢谢!
您还需要在文本图层上应用填充位置调整。您可以通过调整 vjust
参数来控制文本相对于边界的显示位置。
library(ggplot2)
library(dplyr)
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
dat <- structure(list(ONE = c(23L, 34L, 56L, 34L, 123L),
TWO = c(234L, 534L, 324L, 234L, 534L),
THREE = c(324L, 12L, 124L, 124L, 654L)),
class = "data.frame",
row.names = c("1", "2", "3", "4", "5"))
# Add an id variable for the filled regions and reshape
datm <- dat %>%
mutate(ind = factor(row_number())) %>%
gather(variable, value, -ind)
ggplot(datm, aes(x = variable, y = value, fill = ind)) +
geom_col(position = "fill") +
scale_y_continuous(labels = scales::percent_format())+
geom_text(label= 'bla', position = position_fill(vjust = 0.5))
由 reprex package (v0.3.0)
于 2021 年 1 月 19 日创建我只是将此注册为答案,这样浏览未解答问题的人就不会偶然发现这个问题。在评论中建议 1 行修复对我来说更省力。