【发布时间】:2016-02-29 23:46:57
【问题描述】:
我在尝试使用 ggplot 更改图例中的标签名称时遇到问题。
我来阅读Cookbook for R 中有关该主题的一些教程和本论坛(How do I manually change the key labels in a legend in ggplot2)中有关同一主题的问题,等等。我写这篇文章的原因是因为我无法理解我的 ggplot 函数发生了什么,这使得 scale_fill_discrete () 在其他示例中不起作用。 p>
我的数据框是这样的:
x w t
1 0.8972208 A 0
2 0.8312684 A 1
3 0.7504638 A 2
4 0.6563472 A 3
5 0.5764883 A 4
6 0.5224609 A 5
7 0.8972208 B 0
8 0.8456315 B 1
9 0.7674353 B 2
10 0.6689723 B 3
11 0.6114414 B 4
12 0.5381668 B 5
13 0.8983253 C 0
14 0.7878405 C 1
15 0.6955420 C 2
16 0.6179749 C 3
17 0.5524141 C 4
18 0.4966967 C 5
据我所知,使用 ggplot 对图例中的因子进行排序是一项非常困难的任务,这就是它们在数据框中按字母顺序排列的原因 (factor w)。但是真名应该是不同的,比如:“O”、“R”和“N”。
我正在使用的 ggplot 函数是:
ggplot (data = eso3, aes (x=t, y = x, colour = w, linetype = w, shape= w))
+geom_line()
+geom_point()
+scale_linetype_manual("LEGEND TITLE",values =c("solid","solid","dashed","dashed","dotted","dotted"))
+scale_colour_manual("LEGEND TITLE",values=c("black","black","darkgray","darkgray","dimgray","dimgray"))
+scale_shape_manual("LEGEND TITLE",values=c(19,15,19,15,19,15))
+scale_y_continuous("Y LABEL",limits=c(0.0,1.0))
+theme(axis.title.y = element_text(angle=0))
+scale_fill_discrete("LEGEND TITLE",breaks=c("A","B","C"),labels=c("O","R","N"))
+ggtitle("MAIN TITLE")
+theme_bw()
我知道函数 scale_fill_discrete 应该将 ("A","B","C") 更改为 ("O","R","N")。为什么它不起作用?
【问题讨论】: