英文:
How to change colors when using scale_fill_discrete in R?
问题
I have the data below:
time=c(200,218,237,237,237,237,237,246,246,246,257,257,257,272,272,272,294,294,294)
location=c("A","A","D","C","A","B","B","D","C","B","D","C","B","D","C","B","D","C","B")
value=c(0,774,0,0,2178,0,2178,0,1494,2644,1326,1504,4188,3558,1385,5013,12860,829,3483)
dataA=data.frame(time,location,value)
and I made a graph.
ggplot(data=dataA, aes(x=time, y=value))+
geom_area(aes(group=location, fill=location), position="stack", linetype=1, size=0.5 ,colour="black") +
scale_fill_discrete(breaks=c("A","B","C","D"), labels=c("Main_town","B","C","D"))+
theme_classic(base_size=18, base_family="serif")+
theme(legend.position="right",
axis.line=element_line(linewidth=0.5, colour="black"))+
windows(width=5.5, height=5)
I changed one of the legend label from A to main_town using scale_fill_discrete()
. Then color is automatically generated.
I want to change this color according to my preference. When I add a code, scale_fill_manual(values=c("darkblue","darkred","khaki4","darkgreen"))+
the below message shows up and the graph is before I changed legend label.
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.
How can I change colors when using scale_fill_discrete()
? I want to change colors to "darkblue","darkred","khaki4","darkgreen"
Could you please let me know how to do that? Or do you let me know simply how to change legend labels maintaining colors I want?
Always many thanks!!!
英文:
I have the data below:
time=c(200,218,237,237,237,237,237,246,246,246,257,257,257,272,272,272,294,294,294)
location=c("A","A","D","C","A","B","B","D","C","B","D","C","B","D","C","B","D","C","B")
value=c(0,774,0,0,2178,0,2178,0,1494,2644,1326,1504,4188,3558,1385,5013,12860,829,3483)
dataA=data.frame(time,location,value)
and I made a graph.
ggplot(data=dataA, aes(x=time, y=value))+
geom_area(aes(group=location, fill=location), position="stack", linetype=1, size=0.5 ,colour="black") +
scale_fill_discrete(breaks=c("A","B","C","D"), labels=c("Main_town","B","C","D"))+
theme_classic(base_size=18, base_family="serif")+
theme(legend.position="right",
axis.line=element_line(linewidth=0.5, colour="black"))+
windows(width=5.5, height=5)
I changed one of the legend label from A to main_town using scale_fill_discrete()
. Then color is automatically generated.
I want to change this color according to my preference. When I add a code, scale_fill_manual(values=c("darkblue","darkred","khaki4","darkgreen"))+
the below message shows up and the graph is before I changed legend label.
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.
How can I change colors when using scale_fill_discrete()
? I want to change colors to "darkblue","darkred","khaki4","darkgreen"
Could you please let me know how to do that? Or do you let me know simply how to change legend labels maintaining colors I want?
Always many thanks!!!
答案1
得分: 2
我认为您需要使用 scale_fill_discrete(type = c(...))
。
library(ggplot2)
ggplot(data=dataA, aes(x=time, y=value)) +
geom_area(aes(group=location, fill=location), position="stack", linetype=1, size=0.5, colour="black") +
scale_fill_discrete(breaks=c("A","B","C","D"), labels=c("Main_town","B","C","D"),
type=c("darkblue","darkred","khaki4","darkgreen")) +
theme_classic(base_size=18, base_family="serif") +
theme(legend.position="right",
axis.line=element_line(linewidth=0.5, colour="black"))
英文:
I think you need scale_fill_discrete(type = c(...))
.
library(ggplot2)
ggplot(data=dataA, aes(x=time, y=value))+
geom_area(aes(group=location, fill=location), position="stack", linetype=1, size=0.5 ,colour="black") +
scale_fill_discrete(breaks=c("A","B","C","D"), labels=c("Main_town","B","C","D"),
type=c("darkblue","darkred","khaki4","darkgreen"))+
theme_classic(base_size=18, base_family="serif")+
theme(legend.position="right",
axis.line=element_line(linewidth=0.5, colour="black"))
<!-- -->
答案2
得分: 1
你可以使用 scale_fill_manual
并像这样使用你想要的 values
和 labels
:
library(ggplot2)
ggplot(data=dataA, aes(x=time, y=value))+
geom_area(aes(group=location, fill=location), position="stack", linetype=1, size=0.5 ,colour="black") +
scale_fill_manual(breaks=c("A","B","C","D"), labels=c("主城","B","C","D"),
values=c("darkblue","darkred","khaki4","darkgreen"))+
theme_classic(base_size=18, base_family="serif")+
theme(legend.position="right",
axis.line=element_line(linewidth=0.5, colour="black"))
创建于2023-06-01,使用 reprex v2.0.2
英文:
You could use scale_fill_manual
and use the values
and labels
you want like this:
library(ggplot2)
ggplot(data=dataA, aes(x=time, y=value))+
geom_area(aes(group=location, fill=location), position="stack", linetype=1, size=0.5 ,colour="black") +
scale_fill_manual(breaks=c("A","B","C","D"), labels=c("Main_town","B","C","D"),
values=c("darkblue","darkred","khaki4","darkgreen"))+
theme_classic(base_size=18, base_family="serif")+
theme(legend.position="right",
axis.line=element_line(linewidth=0.5, colour="black"))
<!-- -->
<sup>Created on 2023-06-01 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论