scale_fill_discrete 不更改标签名称
scale_fill_discrete does not change label names
我在尝试使用 ggplot 更改图例中的标签名称时遇到问题。
我来阅读 Cookbook for R and questions on the same topic in this forum (How do I manually change the key labels in a legend in ggplot2) 中有关该主题的一些教程,以及更多内容。我写这篇文章的原因是因为我无法理解我的 ggplot 函数发生了什么,这使得 scale_fill_discrete () 不工作,而在其他示例中它工作。
我的数据框是这样的:
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")
。为什么它不起作用?
由于您使用了 colour
、linetype
和 shape
而不是 fill
,因此您必须将新标签与 scale_xxx_manual()
调用集成,例如:
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"),breaks=c("A", "B", "C"), labels=c("O","R","N"))+
scale_colour_manual("LEGEND TITLE",values=c("black","black","darkgray","darkgray","dimgray","dimgray"),breaks=c("A", "B", "C"), labels=c("O","R","N"))+
scale_shape_manual("LEGEND TITLE",values=c(19,15,19,15,19,15),breaks=c("A", "B", "C"), labels=c("O","R","N"))+
scale_y_continuous("Y LABEL",limits=c(0.0,1.0))+
theme(axis.title.y = element_text(angle=0))+
ggtitle("MAIN TITLE")+
theme_bw()
您没有在 ggplot(..) 函数中使用 fill 参数,因此您不能使用 scale_fill_discrete()。
根据您要更改的图例,使用 scale_colour_discrete()、scale_linetype_discrete() 或 scale_shape_discrete()。我见过的关于如何更改标签的大多数提及都利用了 scale_fill_discrete,但忽略了仅当您使用 fill 参数时才会提及。
我在尝试使用 ggplot 更改图例中的标签名称时遇到问题。
我来阅读 Cookbook for R and questions on the same topic in this forum (How do I manually change the key labels in a legend in ggplot2) 中有关该主题的一些教程,以及更多内容。我写这篇文章的原因是因为我无法理解我的 ggplot 函数发生了什么,这使得 scale_fill_discrete () 不工作,而在其他示例中它工作。
我的数据框是这样的:
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")
。为什么它不起作用?
由于您使用了 colour
、linetype
和 shape
而不是 fill
,因此您必须将新标签与 scale_xxx_manual()
调用集成,例如:
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"),breaks=c("A", "B", "C"), labels=c("O","R","N"))+
scale_colour_manual("LEGEND TITLE",values=c("black","black","darkgray","darkgray","dimgray","dimgray"),breaks=c("A", "B", "C"), labels=c("O","R","N"))+
scale_shape_manual("LEGEND TITLE",values=c(19,15,19,15,19,15),breaks=c("A", "B", "C"), labels=c("O","R","N"))+
scale_y_continuous("Y LABEL",limits=c(0.0,1.0))+
theme(axis.title.y = element_text(angle=0))+
ggtitle("MAIN TITLE")+
theme_bw()
您没有在 ggplot(..) 函数中使用 fill 参数,因此您不能使用 scale_fill_discrete()。
根据您要更改的图例,使用 scale_colour_discrete()、scale_linetype_discrete() 或 scale_shape_discrete()。我见过的关于如何更改标签的大多数提及都利用了 scale_fill_discrete,但忽略了仅当您使用 fill 参数时才会提及。