关于 r:有没有办法操纵 ggplot 比例中断和标签?

Is there a way of manipulating ggplot scale breaks and labels?

ggplot 通常可以很好地按比例创建合理的中断和标签。

但是,我发现在具有许多方面和可能是 formatter= 语句的情节中,标签往往会变得过于"密集"和叠印,例如在这张图片中:

1
2
3
4
5
6
7
8
9
df <- data.frame(
        fac=rep(LETTERS[1:10], 100),
        x=rnorm(1000)
)

ggplot(df, aes(x=x)) +
  geom_bar(binwidth=0.5) +
  facet_grid(~fac) +
  scale_x_continuous(formatter="percent")

enter

1
2
3
4
5
6
ggplot(df, aes(x=x)) +
  geom_bar(binwidth=0.5) +
  facet_grid(~fac) +
  scale_x_continuous(breaks = c(min(df$x), 0, max(df$x))
    , labels = c(paste( 100 * round(min(df$x),2),"%", sep =""), paste(0,"%", sep =""), paste( 100 * round(max(df$x),2),"%", sep =""))
    )

或使用 opts(axis.text.x = theme_text(angle = 90, hjust = 0)) 旋转 x 轴文本以产生类似:

enter

1
2
3
4
5
6
7
8
9
10
11
myBreaks <- function(x){
    breaks <- c(min(x),median(x),max(x))
    names(breaks) <- attr(breaks,"labels")
    breaks
}

ggplot(df, aes(x=x)) +
  geom_bar(binwidth=0.5) +
  facet_grid(~fac) +
  scale_x_continuous(breaks = myBreaks,labels = percent_format()) +
  opts(axis.text.x = theme_text(angle = 90, hjust = 1,size = 5))


scales 包包含几个 breaks_*label_* 函数,它们返回 ggplot 使用的函数(闭包)。因此,您可以为这些修改输出的package器编写一个package器。

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
library(ggplot2)

# Compute the list of breaks using original_func,
# then remove any of these that occur in remove_list
remove_breaks <- function(original_func, remove_list = list()) {
  function(x) {
    original_result <- original_func(x)
    original_result[!(original_result %in% remove_list)]
  }
}

# Compute the list of labels using original_func,
# then remove any of these that occur in remove_list
remove_labels <- function(original_func, remove_list = list()) {
  function(x) {
    original_result <- original_func(x)
    replace(original_result, original_result %in% remove_list, '')
  }
}

# Original plot
ggplot(data.frame(x=c(1,2,3,4,5,6,7,8), y = c(1,4,9,16,25,36,49,64))) + geom_line(aes(x, y)) +
  scale_x_continuous(breaks       = scales::breaks_pretty(9),
                     minor_breaks = scales::breaks_pretty(18),
                     labels       = scales::label_number_auto()) +
  scale_y_continuous(breaks       = scales::breaks_pretty(9),
                     minor_breaks = scales::breaks_pretty(18),
                     labels       = scales::label_number_auto())

# Remove some breaks from the x-axis, and remove some labels from the y-axis
ggplot(data.frame(x=c(1,2,3,4,5,6,7,8), y = c(1,4,9,16,25,36,49,64))) + geom_line(aes(x, y)) +
  scale_x_continuous(breaks       = remove_breaks(scales::breaks_pretty(9), seq(3,6)),
                     minor_breaks = remove_breaks(scales::breaks_pretty(18), seq(3,6,0.5)),
                     labels       = scales::label_number_auto()) +
  scale_y_continuous(breaks       = scales::breaks_pretty(9),
                     minor_breaks = scales::breaks_pretty(18),
                     labels       = remove_labels(scales::label_number_auto(), seq(20, 30)))

当然,使用我简单的 remove_breaksremove_labels 函数,您仍然需要指定要删除的值,但是您可以轻松地将它们修改为删除最大值和最小值,删除指定范围内的任何值等