如何在R中使用scale_fill_discrete时更改颜色?

huangapple go评论129阅读模式
英文:

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.

如何在R中使用scale_fill_discrete时更改颜色?

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.

如何在R中使用scale_fill_discrete时更改颜色?

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.

如何在R中使用scale_fill_discrete时更改颜色?

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.

如何在R中使用scale_fill_discrete时更改颜色?

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"))

如何在R中使用scale_fill_discrete时更改颜色?

英文:

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"))

如何在R中使用scale_fill_discrete时更改颜色?<!-- -->

答案2

得分: 1

你可以使用 scale_fill_manual 并像这样使用你想要的 valueslabels

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"))

如何在R中使用scale_fill_discrete时更改颜色?

创建于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=&quot;stack&quot;, linetype=1, size=0.5 ,colour=&quot;black&quot;) +
  scale_fill_manual(breaks=c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;), labels=c(&quot;Main_town&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;),
                      values=c(&quot;darkblue&quot;,&quot;darkred&quot;,&quot;khaki4&quot;,&quot;darkgreen&quot;))+
  theme_classic(base_size=18, base_family=&quot;serif&quot;)+
  theme(legend.position=&quot;right&quot;,
        axis.line=element_line(linewidth=0.5, colour=&quot;black&quot;))

如何在R中使用scale_fill_discrete时更改颜色?<!-- -->

<sup>Created on 2023-06-01 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月1日 19:37:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定