假設我有以下 ggplot
library(zoo)
library(quantmod)
library(ggplot2)
Data = data.frame('class' = rep(c('a', 'b', 'c'), 2), 'Date' = as.yearqtr(rep(c('2021 Q3', '2021 Q4'), each = 3)), 'Value' = 1:6)
Met = 'Per'
FN = function(y, class) ifelse(y == 0, 'y', ifelse(grepl('Per', class), paste0(y * 100, ' aa1'), paste0(y / 100, ' aa2')))
ggplot(Data, aes(x = Date, y = Value, fill = class))
geom_bar(stat = 'identity', position = 'dodge', width = 0.1)
scale_y_continuous(labels = function(y) FN(y, Met))
有了這個,我得到了下面的情節:

在這個圖中,我所有的 Y 軸標簽都是0,而我想要"100 aa1" "200 aa1" "300 aa1" "400 aa1" "500 aa1" "600 aa1"
我的代碼中出現問題的任何指標都會非常有幫助。
uj5u.com熱心網友回復:
如果你的意思paste0(Data$Value * 100, ' %')是像200%, 400%,... 那么你可以使用scales::percent
ggplot(Data, aes(x = Date, y = Value, fill = class))
geom_bar(stat = 'identity', position = 'dodge', width = 0.1)
scale_y_continuous(labels = scales::percent)

FN(Data$Value, Met)
[1] "100 %" "100 %" "100 %" "100 %" "100 %" "100 %"
sapply(Data$Value, function(x) FN(x, Met))
[1] "100 %" "200 %" "300 %" "400 %" "500 %" "600 %"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371937.html
