r - 如何为不同的facet指定不同的尺度格式?

标签 r ggplot2

在下面的代码中,我可以为两个方面设置两种不同的比例。现在,我想格式化大比例尺(例如使用 scale_y_continuous(labels = Dollar) 将其转换为美元)。如何设置各个方面的比例格式?

df <- data.frame(value = c(50000, 100000, 4, 3),
                 variable = c("big", "big", "small", "small"),    
                 x = c(2010, 2011, 2010, 2011))
ggplot(df) + geom_line(aes(x, value)) + facet_wrap(~ variable, scales = "free_y")

A similar question a long time ago about setting individual limit没有答案。我希望 ggplot2 2.0 能对此有所作为。

最佳答案

函数scale_y_continuous允许将函数用于labels参数。我们可以创建一个使用最小 big 值(或任何其他值)作为阈值的自定义标签器。

dollar <- function(x) {
  ifelse(x >= min(df$value[df$variable == "big"]),
    paste0("$", prettyNum(x, big.mark=",")), x)
}

ggplot(df) + geom_line(aes(x, value)) + facet_wrap(~ variable, scales = "free_y") +
  scale_y_continuous(labels = dollar)

enter image description here

关于r - 如何为不同的facet指定不同的尺度格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42168645/

相关文章:

r - 如何根据位置的数值向量拆分字符向量

r - 如何在R中旋转 map

r - 在来自不同组的两个点之间画一条线,同时跟踪这些点

r - 将意大利面条图(带有分组变量)与 ggplot2 中的 split fiddle (没有它)相结合

r - 分离组内不重叠的间隔并在 R 中计数

r - 如何在 shiny 中创建一个简单的下拉菜单?

r - Googleway 不考虑旅行时间的交通(路线搜索)

r - ggplot : remove lines at ribbon edges

r - ggplot2 中 `geom_label` 的 y 值基于 `..count..` 变量

r - 当我使用我的 Shiny 小部件时,我的 ggplot 会调整大小 : how to stop this?