问题描述
Indian style thousand separators 是这样使用的。第一个分隔符为 3 位(千位),之后每两位分隔符。
1
10
100
1,000
10,000
1,00,000
我知道我可以使用 scale_y_continuous(labels = scales::comma)
但是如何根据上述印度格式更改 r ggplot2 图表轴中的千位分隔符占位符。
示例示例
library(tidyverse)
iris %>%
mutate(Petal.Length= Petal.Length*100000) %>%
ggplot(aes(x= Species,y = Petal.Length)) +
geom_col() +
scale_y_continuous(labels = scales::comma)
由 reprex package (v2.0.0) 于 2021 年 6 月 28 日创建
解决方法
您可以定义自己的格式化函数并将其作为 public class CosmosDataFixture : IDisposable
{
public static readonly string CosmosEndpoint = "https://localhost:8081";
public static readonly string EmulatorKey = "testkey";
public static readonly string DatabaseId = "testdb";
public static readonly string RecordingCollection = "testcollection";
public static string Root = Directory.GetParent( Directory.GetCurrentDirectory() ).Parent.Parent.FullName;
public static DocumentClient client { get; private set; }
public void ReadConfigAsync()
{
client = new DocumentClient( new Uri( CosmosEndpoint ),EmulatorKey,new ConnectionPolicy
{
ConnectionMode = ConnectionMode.Direct,ConnectionProtocol = Protocol.Tcp
} );
}
public void ReadAllData(Document client){}
public void CreateDatabaseFromPowerShell()
{
string path = Path.Combine( Root,@"TestData\CosmosScript.ps1" );
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript( path );
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
ReadAllData( client );
}
public CosmosDataFixture()
{
ReadConfigAsync();
CreateDatabaseFromPowerShell();
ReadAllData( client );
}
public void Dispose()
{
DeleteDatabaseFromPowerShell();
}
}
public class CosmosDataTests : IClassFixture<CosmosDataFixture>
{
[fact]
....test cases...
参数提供给 labels
。以下是使用基本 scale_y_continuous()
函数的示例:
prettyNum()
,
您可以为y轴设置中断,然后根据印度系统进行标记:
iris %>%
mutate(Petal.Length= Petal.Length*100000) %>%
ggplot(aes(x= Species,y = Petal.Length)) +
geom_col() +
scale_y_continuous(breaks = c(0,10000000,20000000),labels = c("0","1,00,00000","2,000"))
,
这篇文章:https://stackoverflow.com/a/62037466/2554330 定义了一个函数 format2()
。它不像现在那样有效,但经过一些小修正,这有效:
format2 <- function(x,...,big.mark = "",big.interval = c(3L,2L),decimal.mark = ".") {
intervene <- !is.na(x) && x > 0 && (log(abs(x),10) >= sum(big.interval)) && nzchar(big.mark)
cl <- match.call()
cl[[1]] <- substitute(format)
if (intervene) {
cl$x <- x %/% 10^big.interval[1]
cl$big.interval <- big.interval[2]
bigx <- eval.parent(cl)
cl$x <- x
cl$big.interval <- big.interval[1]
mostx <- eval.parent(cl)
mostx <-
substr(mostx,1L + nchar(x %/% 10^big.interval[1]) +
trunc(trunc(log(abs(x %/% 10^big.interval[1]),10L)) / big.interval[1]),nchar(mostx))
return( paste0(bigx,mostx) )
} else eval.parent(cl)
}
f <- function(x) {
sapply(x,format2,scientific = FALSE,big.mark = ",")
}
library(tidyverse)
iris %>%
mutate(Petal.Length= Petal.Length*100000) %>%
ggplot(aes(x= Species,y = Petal.Length)) +
geom_col() +
scale_y_continuous(labels = f)
由 reprex package (v2.0.0) 于 2021 年 6 月 28 日创建