R语言ggplot2 您所在的位置:网站首页 r语言randomforest参数 R语言ggplot2

R语言ggplot2

2023-03-13 02:33| 来源: 网络整理| 查看: 265

📋文章目录 Percent() 函数介绍例子1,在向量中格式化百分比:例子2,格式化数据框列中的百分比:例子3,格式化多个数据框列中的百分比: 如何使用percent()函数在绘图过程展示    通常在绘图时,遇到小数点的数值会默认保留原格式。如何使用百分比来展示,可以借助scales包中的percent ( )函数来解决。

Percent() 函数介绍

   在 R 中将数字格式化为百分比的最简单方法是使用 scales 包中的 percent() 函数。

# 此函数使用以下语法: percent(x, accuracy = 1)

不懂的函数可以先help或者?函数名(前提需要先加载对应R包) 在这里插入图片描述在这里插入图片描述 介绍主要的参数: x : 要格式化为百分比的对象 accuracy : 要四舍五入的数字。例如,使用 .01 舍入到小数点后两位

例子1,在向量中格式化百分比: library(scales) data % summarise(employ_n = sum(employ_n)) %>% mutate(pct = employ_n/sum(employ_n)) %>% arrange(desc(pct)) %>% . [1:10, ] %>% ggplot(aes(x = pct , y = reorder(minor_occupation, pct)))+ geom_col(aes(fill = I(ifelse(pct == max(pct), "#4281a4", "#9cafb7"))), width = 0.85)+ geom_text(aes(label = percent(round(pct, 3)), color = I(ifelse(pct == max(pct), "white", "black"))), size = 2, hjust = 1.1) + scale_y_discrete(labels = function(x) str_wrap(x, 28)) + scale_x_continuous(labels = percent, limits=c(0, 0.30), expand = c(0, 0))+ theme_light() + theme(plot.margin = ggplot2::margin(0, 10, 0, 0), plot.title = element_text(face = "bold", size = 16)) + theme(axis.ticks.y = element_blank()) + labs(y = "Minor Occupation", x = "Percent")

在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有