我想要将y轴的格式更改为百分比,而我的数据是十进制数字的形式.

这就是我试过的

par(mar = c(7,3, 1, 2), xpd = TRUE)

plot(my_data_cumulative$Date, my_data_sub$Compensation, type = "l", lwd = 3, ylim = c(-0.06, 0.1), lty = 1, col = "green", xlab = "", ylab = "Cumulative Change")

lines(my_data_cumulative$Date, my_data_sub$Depreciation, type = "l", lty = 2, col = "grey40",  lwd = 3)
lines(my_data_cumulative$Date, my_data_sub$Taxes, type = "l", lty = 3, col = "grey60",  lwd = 3)
lines(my_data_cumulative$Date, my_data_sub$Interest, type = "l", lty = 4, col = "grey80",  lwd = 3)
lines(my_data_cumulative$Date, my_data_sub$Corpotaxes, type = "l", lty = 5, col = "black",  lwd = 3)
lines(my_data_cumulative$Date, my_data_sub$Profits, type = "l", lty = 2, col = "blue",  lwd = 3)
legend("bottom", 
       legend = c("Compensation", "Depreciation", "Taxes", "Interest", "Corpotaxes", "Profits"), 
       inset = c(0.8,-0.5),
       xpd = TRUE,
       col = c("green", "grey40", "grey60", "grey80", "black", "blue"),
       lty = c(1, 2, 3, 4, 1, 2),
       ncol = 3,
       lwd = 3,
       cex =1)
axis(side = 2, at = pretty(ylim), labels = percent(pretty(ylim)))

推荐答案

您可以乘以paste(或者做任何适当地将您的数据zoom 到百分比的操作),然后再乘以paste"%".

> ylim <- range(dat)
> par(mar=c(5, 5, 2, 2)+.1)
> matplot(dat, type='l', yaxt='n', xlab='foo', ylab='percent', ylim=0:1)
> axis(side=2, at=pretty(ylim), labels=paste0(pretty(ylim*100), '%'), las=2)

enter image description here


Data:

set.seed(42)
dat <- data.frame(matrix(runif(30), 10, 3))

R相关问答推荐

更新合适的R mgcv::bam模型报告无效类型(关闭).'';错误

将向量组合到一个数据集中,并相应地命名行

将数据集中的值增加到当前包含的最大值

计算具有奇数日期的运行金额

是否可以创建一个ggplot与整洁判断的交互作用

将小数分隔符放在R中的前两位数字之后

如何使用ggplot对堆叠条形图进行嵌套排序?

在嵌套列表中查找元素路径的最佳方法

如何删除最后一个可操作对象

Select 季度月值

合并后返回列表的数据帧列表

仅在R中的数据集开始和结束时删除所有 Select 列的具有NA的行

将项粘贴到向量中,并将它们分组为x的倍数,用空格分隔

使用geom_sf跨越日期线时的闭合边界

如何在R中创建这些列?

在REST API中使用参数R

根据排名的顶点属性调整曲线图布局(&Q)

从字符串01JAN2021创建日期

使用点图调整离散轴比例

根据部分名称匹配获取多组列的行求和