luc*_*cho 183
使用:
+ scale_y_continuous(labels = scales::percent)
Run Code Online (Sandbox Code Playgroud)
或者,为百分比指定格式参数:
+ scale_y_continuous(labels = scales::percent_format(accuracy = 1))
Run Code Online (Sandbox Code Playgroud)
(该命令labels = percent自ggplot2的2.2.1版本后已过时)
Dee*_*ena 50
原则上,您可以将任何重新格式化函数传递给labels参数:
+ scale_y_continuous(labels = function(x) paste0(x*100, "%")) # Multiply by 100 & add %
Run Code Online (Sandbox Code Playgroud)
要么
+ scale_y_continuous(labels = function(x) paste0(x, "%")) # Add percent sign
Run Code Online (Sandbox Code Playgroud)
可重复的例子:
library(ggplot2)
df = data.frame(x=seq(0,1,0.1), y=seq(0,1,0.1))
ggplot(df, aes(x,y)) +
geom_point() +
scale_y_continuous(labels = function(x) paste0(x*100, "%"))
Run Code Online (Sandbox Code Playgroud)
Pen*_*ght 39
ggplot2和scales包可以做到这一点:
y <- c(12, 20)/100
x <- c(1, 2)
library(ggplot2)
library(scales)
myplot <- qplot(as.factor(x), y, geom="bar")
myplot + scale_y_continuous(labels=percent)
Run Code Online (Sandbox Code Playgroud)
似乎该stat()选项已被取消,导致错误消息.试试这个:
library(scales)
myplot <- ggplot(mtcars, aes(factor(cyl))) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(labels=percent)
myplot
Run Code Online (Sandbox Code Playgroud)
借用上面的@Deena,标签的功能修改比您想象的更通用。例如,我有一个 ggplot,其中计数变量的分母是 140。我因此使用了她的示例:
scale_y_continuous(labels = function(x) paste0(round(x/140*100,1), "%"), breaks = seq(0, 140, 35))
Run Code Online (Sandbox Code Playgroud)
这让我能够得到 140 分母的百分比,然后以 25% 的增量打破比例,而不是默认的奇怪数字。这里的关键是,比例分隔符仍然由原始计数设置,而不是由百分比设置。因此,中断必须从零到分母值,“breaks”中的第三个参数是分母除以您想要的标签中断数量(例如 140 * 0.25 = 35)。
| 归档时间: |
|
| 查看次数: |
103175 次 |
| 最近记录: |