1

我正在使用scale_x_discrete()自定义 x 轴的刻度和标签。

但是,如图所示,这些线切割了右侧的 y 轴,这对我来说看起来不太好。你能帮我解决这个问题吗?数据(温度)也如下所示。

> a = ggplot(data = temp, aes(b, c, group=a,shape=a,colour=a), ordered=TRUE) + geom_line() + geom_point() 
> a
> b = a + scale_x_discrete(breaks = c("2","4","8","16","32","64","128"), labels=c("2","4","8","16","32","64","128"))

> temp
       a   b    c
1    One   2  5.1
2    One   4  6.6
3    One   8  7.7
4    One  16  8.4
5    One  32 16.1
6    One  64 38.0
7    One 128 49.2
8    Two   2  5.9
9    Two   4  7.7
10   Two   8  9.2
11   Two  16 10.3
12   Two  32 16.8
13   Two  64 32.4
14   Two 128 45.7
15 Three   2  4.7
16 Three   4  7.0
17 Three   8  8.5
18 Three  16  9.6
19 Three  32 14.8
20 Three  64 31.0
21 Three 128 34.5
22  Four   2  4.3
23  Four   4  6.9
24  Four   8  8.3
25  Four  16  9.1
26  Four  32 14.0
27  Four  64 23.8

在此处输入图像描述

4

2 回答 2

7

你为什么要使用离散比例来表示似乎是连续的。

如果您替换scale_x_discretescale_x_continuousthen 这应该可以按您的意愿工作。

b <- a + scale_x_continuous(breaks = 2^(1:7))
b

在此处输入图像描述 您可能对以 2 为底的转换感兴趣,因为您的数据b似乎只是 2 的整数幂。

a + scale_x_continuous(breaks = 2^(1:7), trans = 'log2')

在此处输入图像描述

于 2012-12-12T23:15:25.713 回答
2

ggplot 网站也有“扩展”参数。将数字调整为您想要达到的任何外观

a + scale_x_discrete(breaks = c("2","4","8","16","32","64","128"),
                     labels=c("2","4","8","16","32","64","128"),
                     expand = c(.1,.1))
于 2012-12-12T23:20:40.417 回答