具体地说,这是在一个facet_grid中。我在谷歌上广泛搜索过类似的问题,但不清楚其语法或去向。我想要的是y轴上的每个数字在小数点后都有两位数,即使尾随的是0。这是scale_y_continuous或element_text中的参数还是...?
row1 <- ggplot(sector_data[sector_data$sector %in% pages[[x]],], aes(date,price)) + geom_line() +
geom_hline(yintercept=0,size=0.3,color="gray50") +
facet_grid( ~ sector) +
scale_x_date( breaks='1 year', minor_breaks = '1 month') +
scale_y_continuous( labels = ???) +
theme(panel.grid.major.x = element_line(size=1.5),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_text(size=8),
axis.ticks=element_blank()
)
发布于 2016-08-02 21:58:17
在?scale_y_continuous
的帮助中,参数'labels‘可以是一个函数:
标记了以下标签之一:
我们将使用最后一个选项,该函数将breaks
作为参数,并返回一个有两个小数位的数字。
#Our transformation function
scaleFUN <- function(x) sprintf("%.2f", x)
#Plot
library(ggplot2)
p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p <- p + facet_grid(. ~ cyl)
p + scale_y_continuous(labels=scaleFUN)
发布于 2018-11-18 22:35:48
“scale”包有一些用于格式化坐标轴的很好的函数。其中一个函数是number_format()。所以你不必先定义你的函数。
library(ggplot2)
# building on Pierre's answer
p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p <- p + facet_grid(. ~ cyl)
# here comes the difference
p + scale_y_continuous(
labels = scales::number_format(accuracy = 0.01))
# the function offers some other nice possibilities, such as controlling your decimal
# mark, here ',' instead of '.'
p + scale_y_continuous(
labels = scales::number_format(accuracy = 0.01,
decimal.mark = ','))
发布于 2021-10-08 15:56:30
scales包已更新,number_format()
已停用。使用label_number()
。这也可以应用于百分比和其他连续比例(例如:label_percent()
;https://scales.r-lib.org/reference/label_percent.html)。
#updating Rtists answer with latest syntax from scales
library(tidyverse); library(scales)
p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p <- p + facet_grid(. ~ cyl)
# number_format() is retired; use label_number() instead
p + scale_y_continuous(
labels = label_number(accuracy = 0.01)
)
# for whole numbers use accuracy = 1
p + scale_y_continuous(
labels = label_number(accuracy = 1)
)
https://stackoverflow.com/questions/38722202
复制相似问题