ggplot2:在使用位置 == 填充时向百分比堆积图添加标签?
ggplot2: Adding a label to percent stacked plot when using position is == fill?
您好,标准化后我似乎无法正确地向堆栈图添加标签。例如,
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value)
data$value = round ( data$value, 2 )
# Stacked + percent
ggplot(data, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="fill", stat="identity")
以上将生成标准化堆栈图,但是当我添加
geom_text(aes(label=value),
position=position_stack(vjust=0.5))
不符合规范化。有办法解决这个问题吗?
它类似于 link 但不完全是因为我不需要计算而且我还添加了 stat="identity"
提前致谢。
当您使用 position="fill"
作为您的条形图时,您也必须在 geom_text
中使用 position=position_fill(vjust=0.5)
来定位您的标签:
注意:我切换到 geom_col
与 geom_bar(..., stat="identity")
相同
set.seed(123)
library(ggplot2)
# Stacked + percent
ggplot(data, aes(fill = condition, y = value, x = specie)) +
geom_col(position = "fill") +
geom_text(aes(label = value),
position = position_fill(vjust = 0.5)
)
您好,标准化后我似乎无法正确地向堆栈图添加标签。例如,
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value)
data$value = round ( data$value, 2 )
# Stacked + percent
ggplot(data, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="fill", stat="identity")
以上将生成标准化堆栈图,但是当我添加
geom_text(aes(label=value),
position=position_stack(vjust=0.5))
不符合规范化。有办法解决这个问题吗? 它类似于 link 但不完全是因为我不需要计算而且我还添加了 stat="identity"
提前致谢。
当您使用 position="fill"
作为您的条形图时,您也必须在 geom_text
中使用 position=position_fill(vjust=0.5)
来定位您的标签:
注意:我切换到 geom_col
与 geom_bar(..., stat="identity")
set.seed(123)
library(ggplot2)
# Stacked + percent
ggplot(data, aes(fill = condition, y = value, x = specie)) +
geom_col(position = "fill") +
geom_text(aes(label = value),
position = position_fill(vjust = 0.5)
)