用不同颜色的水平线

时间:2017-12-06 21:05:55

标签: r ggplot2

如何修改此代码以使行具有不同的颜色(例如黑色,红色,绿色)

library(ggplot2)

    lt <- data.frame(yint = c(200, 250, 210, 215, 279, 280),
                     grp  = factor(c(1, 1, 2, 2, 3, 3),
                                   levels = 1:3,
                                   labels = c("Group 1", "Group 2", "Group 3")))


    ggplot(mtcars, aes(mpg, disp)) + 
          geom_point(aes(colour=factor(vs), 
          fill = factor(vs)), shape=21, size = 4) + 
          scale_fill_manual(values=c("blue", "pink")) + 
          scale_colour_manual(values=c("black", "black"))+
          geom_hline(data = lt,
                     mapping = aes(yintercept = yint, linetype = grp))

我尝试使用此功能但是我收到了错误

ggplot(DataSet, aes(AGE, RESULT)) + 
      geom_point(aes(colour=PATIENT.SEX, 
      fill = PATIENT.SEX), shape=21, size = 1.4) + 
      scale_fill_manual(values=c("hotpink", "skyblue2")) + 
      scale_colour_manual(values=c("hotpink", "skyblue2"))+  
      ylab("Potassium (mmol/L) ")+xlab("Age (month) ")+ggtitle("Alberta observations")+theme_bw()+
      theme(plot.title = element_text(lineheight=.8, face="bold",size=13))+
      theme(axis.text=element_text(size=12),axis.title=element_text(size=12,face="bold"))+
      theme(panel.border = element_blank(), panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
      geom_hline(data = lt,
                 mapping = aes(yintercept = yint,linetype = RI),colour
=c("blue","blue", "red", "red", "black","black" ))

2 个答案:

答案 0 :(得分:0)

解决方案是修改scale_color_manual电话。为values(已在帖子中完成)和三个组级别的因子级别设置limitsvs。在下面的示例中,我将两个级别的factor(vs)颜色设置为黑色,如原始帖子中所做的那样,然后将组1,组2和组3的颜色设置为紫色,绿色和黄色。

library(ggplot2)

lt <- data.frame(yint = c(200, 250, 210, 215, 279, 280),
                 grp  = factor(c(1, 1, 2, 2, 3, 3),
                               levels = 1:3,
                               labels = c("Group 1", "Group 2", "Group 3")))


ggplot(mtcars, aes(mpg, disp)) + 
      geom_point(aes(colour=factor(vs), fill = factor(vs)), shape=21, size = 4) + 
      scale_fill_manual(values=c("blue", "pink")) + 
      geom_hline(data = lt,
                 mapping = aes(yintercept = yint, linetype = grp, color = grp)) +
      scale_color_manual(name   = "",
                         values = c("black", "black", "Purple", "Green", "Yellow"),
                         limits = c(0, 1, "Group 1", "Group 2", "Group 3"))

enter image description here

答案 1 :(得分:0)

蛮力&#34;从 ggplot2 的当前development version开始的选项2.2.1.9000,用于设置aes以外的颜色。您给出的颜色必须是长度1(所有颜色相同)或与该层中使用的数据集相同的长度。

在您的示例中,数据集中有6行可以生成6条水平线,因此我给出了6种颜色。

如果使用此方法,则必须小心将颜色与数据集中的相应行匹配。在您的实际用例中,您可以考虑将颜色添加到数据集中以保持井井有条。

ggplot(mtcars, aes(mpg, disp)) + 
     geom_point(aes(colour=factor(vs), 
                    fill = factor(vs)), shape=21, size = 4) + 
     scale_fill_manual(values=c("blue", "pink")) + 
     scale_colour_manual(values=c("black", "black"))+
     geom_hline(data = lt,
                mapping = aes(yintercept = yint, linetype = grp), 
                color = c("black", "black", "red", "red", "green", "green") )

enter image description here

如果您想在linetype图例上添加颜色,可以使用guide_legendoverride.aes设置图例颜色。

ggplot(mtcars, aes(mpg, disp)) + 
     geom_point(aes(colour=factor(vs), 
                    fill = factor(vs)), shape=21, size = 4) + 
     scale_fill_manual(values=c("blue", "pink")) + 
     scale_colour_manual(values=c("black", "black"))+
     geom_hline(data = lt,
                mapping = aes(yintercept = yint, linetype = grp), 
                color = c("black", "black", "red", "red", "green", "green") ) +
     guides(linetype = guide_legend(override.aes = list(color = c("black", "red", "green") ) ) )

enter image description here