带有中断的ggplot scale_color_manual与预期顺序不匹配

时间:2019-01-07 17:08:24

标签: r ggplot2

我正在绘制散点图,我想指定图例中文本的顺序以及颜色。使用ggplot,颜色顺序与编写的文本顺序不匹配。

我的代码:

MyX<-c(1,2,3,4,5)
MyY<-c(3,4,5,2,7)
MyCat<-c("Dark","Medium","DarkLight","LightDark","Light")
MyDF<-data.frame(MyX,MyY,MyCat)


ggplot(MyDF, aes(x=MyX, y=MyY, color=MyCat)) +
  geom_point()+
  xlab("Time") + 
  ylab("Num") +
  scale_color_manual(breaks =     c("Dark","DarkLight","Medium","LightDark","Light"),
                     values=c("red", "orange","yellow","cadetblue2","dodgerblue"))

我不希望图例中文本的默认顺序,因此我想将顺序更改为:

“暗”,“暗光”,“中”,“浅暗”,“浅”

我还希望颜色在渐变中:

“红色”,“橙色”,“黄色”,“ cadetblue2”,“ dodgerblue”

因此,深色=红色,深色=橙色,中等=黄色,深色= cadetblue2,浅色=蓝色。

我想以上代码会发生什么。但是如图所示,文本与颜色不匹配,我有:

暗=红色,暗浅=橙色,中=蓝色,浅暗= cadetblue2,浅=黄色。

注意-使用色带时我有同样的问题:

  gs.pal <- colorRampPalette(c("red","blue"),bias=.1,space="rgb")
+scale_color_manual(breaks = c("Dark","DarkLight","Medium","LightDark","Light"),
                       values=gs.pal(5))

3 个答案:

答案 0 :(得分:5)

这可以同时设置图例名称,颜色和顺序。

p <- ggplot(dd) + 
      geom_line(aes(x=num_round, y = goods, color = "Good"), size=1) +
      geom_line(aes(x=num_round, y = mediums, color = "Medium"), size=1) + 
      geom_line(aes(x=num_round, y = bads, color = "Bad"), size=1) + 
      scale_color_manual("State", breaks=c("Good", "Medium", "Bad"),
                         values=c("Good"="green","Medium"="orange","Bad"="red")) 
print(p)

enter image description here

答案 1 :(得分:2)

ggplot正在按字母顺序排列MyCat变量,以避免出现这种情况,需要按所需顺序将该变量定义为一个因子:

library(ggplot2)

MyDF<-data.frame(MyX,MyY,MyCat)

#define as factor in the desired order
MyDF$MyCat<-factor(MyDF$MyCat, levels=c("Dark","DarkLight","Medium","LightDark","Light"))

ggplot(MyDF, aes(x=MyX, y=MyY, color=MyCat)) +
  geom_point()+
  xlab("Time") + 
  ylab("Num") +
  scale_color_manual(breaks = c("Dark","DarkLight","Medium","LightDark","Light"),
                     values=c("red", "orange","yellow","cadetblue2","dodgerblue"))

这应该提供愿望顺序
https://blogs.msdn.microsoft.com/besidethepoint/2010/05/08/double-hop-authentication-why-ntlm-fails-and-kerberos-works/

答案 2 :(得分:1)

Dave2e发布了一个优雅的解决方案;比较两个数据集时,我遇到了类似的问题,但他的解决方案(我认为)不太方便。另一种方法可能是使用标签选项,同时确保类别按字母顺序排列:

MyX<-c(1,2,3,4,5)
MyY<-c(3,4,5,2,7)
MyCat<-c("a","b","c","d","e")
MyDF<-data.frame(MyX,MyY,MyCat)

ggplot(MyDF, aes(x=MyX, y=MyY, color=MyCat)) +
  geom_point()+
  xlab("Time") + 
  ylab("Num") +
  scale_color_manual(breaks = c("a","b","c","d","e"),
                     values= c("red", "orange","yellow","cadetblue2","dodgerblue"),
                    labels= c("Dark","Medium","DarkLight","LightDark","Light"))

您可以使用不同的数据集来直接工作

ggplot(MyDF, aes(x=MyX, y=MyY, color="a")) +
  geom_point()+
  geom_point(data=data.frame(time=c(1,2,3),num=c(4,5,6)),aes(x=time,y=num,color="c"))+
  geom_hline(aes(yintercept=3.5, color="b"))+
  xlab("Time") + 
  ylab("Num") +
  scale_color_manual(breaks = c("a","b","c"),
                     values= c("black", "red","blue"),
                    labels= c("c_black_line","b_red_line","a_blue_line"))