在ggplot2中重复X轴标签和图例标签

时间:2013-04-23 21:18:58

标签: r ggplot2

我不确定为什么我的x刻度标签重复出现这样的问题,而不是只标记有测量点的地方。此外,我的传奇标签无效。

 FamIncome Ethnicity    mean.bmi
1   1   1   28.54250
2   1   2   26.66300
3   1   3   26.62105
4   1   4   29.51396
5   1   5   25.66722
6   2   1   29.62404
7   2   2   28.08393
8   2   3   28.62215
9   2   4   28.97561
10  2   5   25.57714
11  3   1   29.52630
12  3   2   28.27235
13  3   3   29.67060
14  3   4   31.36768
15  3   5   26.13361
16  4   1   30.83368
17  4   2   30.80814
18  4   3   29.29594
19  4   4   29.18521
20  4   5   24.80550
21  5   1   29.76500
22  5   2   29.24404
23  5   3   28.89435
24  5   4   31.48172
25  5   5   28.02522
26  6   1   30.05087
27  6   2   29.88574
28  6   3   29.53793
29  6   4   30.97993
30  6   5   25.57857
31  7   1   30.31787
32  7   2   29.28055
33  7   3   28.50421
34  7   4   30.65427
35  7   5   26.66094
36  8   1   29.15000
37  8   2   29.02789
38  8   3   28.36507
39  8   4   33.51915
40  8   5   28.38263
41  9   1   28.17679
42  9   2   28.74731
43  9   3   28.06196
44  9   4   31.38483
45  9   5   26.96000
46  10  1   28.71633
47  10  2   33.44409
48  10  3   30.63048
49  10  4   30.22587
50  10  5   27.36375
51  14  1   30.78161
52  14  2   27.43575
53  14  3   28.96817
54  14  4   32.22378
55  14  5   25.62778
56  15  1   29.15982
57  15  2   27.42672
58  15  3   27.60567
59  15  4   30.05013
60  15  5   26.80271

以下代码:

a <- ggplot(nh1, aes(x=FamIncome, y=mean.bmi)) + geom_line(aes(group=Ethnicity, colour = Ethnicity)) + geom_point()
a = a + labs(list(title="Average BMI versus Family Income", x = "Family Income", y = "Average BMI"))
a = a + scale_x_discrete(breaks=c("1","2","3","4","5","6","7","8","9","10","14","15"),
      labels = c("0-4,999", "5K-9,999", "10K-14,999", "15K-19,999", "20K-24,999", "25K-34,999", "35K-44,999", "45K-54,999", "55K-64,999", "65K-74,999", "75K-100K", "Over 100K"))
a = a + theme(axis.text.x=element_text(angle=-90))
a = a + scale_colour_continuous(name = "Ethnicity", 
                        breaks=c("5","4","3","2","1"),
                        labels=c("Other Race/Multi", "Black","White","Other Hispanic", "Mexican-American"))

a

在我获得2个“声望”​​点之前,我无法发布我正在拍摄的图像照片

1 个答案:

答案 0 :(得分:1)

尝试将x变量转换为因子:

a <- ggplot(nh1, aes(x=factor(FamIncome), y=mean.bmi)) + geom_line(aes(group=Ethnicity, colour = factor(Ethnicity)))
a = a + labs(list(title="Average BMI versus Family Income", x = "Family Income", y = "Average BMI"))
a = a + scale_x_discrete("Family Income", labels = c("0-4,999", "5K-9,999", "10K-14,999", "15K-19,999", "20K-24,999", "25K-34,999", "35K-44,999", "45K-54,999", "55K-64,999", "65K-74,999", "75K-100K", "Over 100K"))
a = a + opts(axis.text.x=theme_text(angle=-90))
a = a + scale_colour_discrete(name = "Ethnicity", 
                        breaks=c("5","4","3","2","1"),
                        labels=c("Other Race/Multi", "Black","White","Other Hispanic", "Mexican-American"))

enter image description here

使用数字x变量时,ggplot会将其视为数字刻度,当您真正希望它是分类时。另请注意fillcolour之间的混淆。 fill适用于二维填充区域。

相关问题