Specifically, this is in a facet_grid. Have googled extensively for similar questions but not clear on the syntax or where it goes. What I want is for every number on the y-axes to have two digits after the decimal, even if the trailing one is 0. Is this a parameter in scale_y_continuous or element_text or...?
具体来说,这是在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()
)
1 个解决方案
#1
24
From the help for ?scale_y_continuous
, the argument 'labels' can be a function:
从?scale_y_continuous的帮助中,参数“标签”可以是一个函数:
labels One of:
标签之一:
NULL for no labels
空没有标签
waiver() for the default labels computed by the transformation object
对于转换对象计算的默认标签,弃权()
A character vector giving labels (must be same length as breaks)
一个字符向量给予标签(必须与中断相同长度)
A function that takes the breaks as input and returns labels as output
将中断作为输入并返回标签作为输出的函数。
We will use the last option, a function that takes breaks
as an argument and returns a number with 2 decimal places.
我们将使用最后一个选项,一个以break作为参数并返回一个小数点后两位的数字的函数。
#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)
#1
24
From the help for ?scale_y_continuous
, the argument 'labels' can be a function:
从?scale_y_continuous的帮助中,参数“标签”可以是一个函数:
labels One of:
标签之一:
NULL for no labels
空没有标签
waiver() for the default labels computed by the transformation object
对于转换对象计算的默认标签,弃权()
A character vector giving labels (must be same length as breaks)
一个字符向量给予标签(必须与中断相同长度)
A function that takes the breaks as input and returns labels as output
将中断作为输入并返回标签作为输出的函数。
We will use the last option, a function that takes breaks
as an argument and returns a number with 2 decimal places.
我们将使用最后一个选项,一个以break作为参数并返回一个小数点后两位的数字的函数。
#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)