更改R ggplot中的直方图条百分比标签

时间:2020-01-21 12:42:37

标签: r ggplot2 text label histogram

我使用以下代码在R中制作了直方图: (我尝试生成reprex。请尝试the code reprex here

progressiveNumber = c(1:50)

c = c(-0.22037439, -0.21536365, -0.34203720,  0.04501624, -0.13141665, -1.28155157, -0.08394700, -0.08484768, -0.12577287,  0.30402612, -0.40578251,
 0.00000000, -0.16849942, -0.04212114,  0.12577287,  0.57366312, -0.84766743, -1.03909659, -0.21536365, -0.46263648, -0.48181028, -0.38887381,
-0.38571106, -0.38571106, -0.26220026,  0.73227348, -0.38887381, -0.96590662, -0.29931065,  0.04272655,  0.04182587, -0.38571106, -0.13141665,
-0.34614726, -0.49063020, -0.08484768,  0.05249378,  0.08484768, -0.74591104,  0.46263648, -0.42081062,  0.00000000,  0.08394700, -0.38571106,
 -0.34203720, -0.04212114, -0.79517364,  0.25429442, -0.30402612, -0.08365173)

library(tidyverse)

# DEFINING BREAKS AND CUT A VECTOR INTO BINS
# set up cut-off values 
breaks <- c(-1.2816,-0.3881,-0.2154, 0.0000, 0.3 ,0.7323)
# specify interval/bin labels
tags <- c("[-1.2 / -0.3]","[-0.3 / -0.2]", "[-0.2 / 0]", "[0 / 0.3]","[0.3 / 0.7]")
# bucketing values into bins
group_tags <- cut(c, 
                  breaks=breaks, 
                  include.lowest=TRUE, 
                  right=FALSE, 
                  labels=tags)
# inspect bins
summary(group_tags)

# c_groups <- factor(group_tags,levels = labels, ordered = TRUE) # this line doesn't work for some reason
#tiff("percentageBinsC.tiff", units="in", width=5, height=5, res=300,)
p2 = ggplot(data = as_tibble(group_tags), mapping = aes(x=value)) + 
  geom_bar(fill="deepskyblue1",color="white",alpha=0.7, ) + 
  stat_count(geom="text", aes(label=sprintf("%.2f",..count../length(group_tags))), vjust=-0.5) +
  labs(y = 'Count',  x='C') +
  theme(text = element_text(size=20), axis.line.x = element_line(color = "black", size = 1),
        axis.line.y = element_line(color = "black", size = 1), axis.text.x = element_text(angle = 35, hjust = 1, vjust = 1),
        panel.background = element_blank(), panel.border = element_blank(),
        panel.grid.minor = element_blank(),panel.grid.major = element_blank())

p2  

#dev.off()

结果

enter image description here

我想将条形上的标签(不是x轴标签,而是每个条形顶部的标签)从例如0.26更改为26%,22%,依此类推。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以从比例尺使用percent_format,首先我们定义一个函数进行转换,然后使用sprintf进行四舍五入:

convert2perc = scales::percent_format(accuracy = 2)

您可以对其进行测试:

convert2perc(0.107)  
[1] "10%"

然后在绘图中使用它:

p2 = ggplot(data = as_tibble(group_tags), mapping = aes(x=value)) + 
  geom_bar(fill="deepskyblue1",color="white",alpha=0.7, ) + 
  stat_count(geom="text", aes(label=convert2perc(..count../length(group_tags))), vjust=-0.5) +
  labs(y = 'Count',  x='C') +
  theme(text = element_text(size=20), axis.line.x = element_line(color = "black", size = 1),
        axis.line.y = element_line(color = "black", size = 1), axis.text.x = element_text(angle = 35, hjust = 1, vjust = 1),
        panel.background = element_blank(), panel.border = element_blank(),
        panel.grid.minor = element_blank(),panel.grid.major = element_blank())

enter image description here