【问题标题】:Legend and multiple geom_line图例和多个 geom_line
【发布时间】:2019-10-08 09:49:51
【问题描述】:

我有多条线要绘制,我希望将数据分为 3 类。请在此处找到可复制的示例:

x <- c(1:20, 1:20,1:20,1:20,1:20,1:20,1:20)
variable <- c(rep("y1", 20), rep("y2", 20),rep("y3", 20),rep("y4", 20),rep("y5", 20),rep("y6", 20),rep("y7", 20))
value <- c(rnorm(20), rnorm(20,1),rnorm(20,2),rnorm(20,3),rnorm(20,5),rnorm(20,6),rnorm(20,7))
type=c(rep("A",100),rep("B",40))

df <- data.frame(x, variable, value,type)
library(ggplot2)
d <- ggplot(df, aes(x=x, y=value, group=variable, colour=type)) + geom_line(size=0.5,alpha=0.6)+
geom_line(data=subset(df,variable=="y6"),size=2,alpha=1,col="blue")+            
geom_line(data=subset(df,variable=="y7"),size=2,alpha=1,col="black")

我预计图例中有 3 个类别:细红线中的“A”组,蓝色的“y6”组,黑色和粗线中的“y7”。如何正确设置图例?

非常感谢您的帮助, 奥切斯

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    欢迎来到 SO!处理您的数据而不是处理ggplot 怎么样(记得放一个set.seed(123) 以使您的数据也可以复制给其他人):

    # define the colors
    df$color <- ifelse(df$variable == 'y6', 'A',ifelse(df$variable == 'y7', 'B','C'))
    # define the size of the lines
    df$size <- ifelse(df$variable %in% c('y6','y7'),2,0.5)
    
    # here a custom palette of colors
    myColors <-c("black","blue","red")
    names(myColors) <- levels(df$color)
    colScale <- scale_colour_manual(name = "grp",values = myColors)
    
    # here the plot, adding your color palette, and the columns created
    library(ggplot2)
    ggplot(df, aes(x=x, y=value, group=variable,color=color)) +
     geom_line(size= df$size,alpha=1) +
     colScale
    

    【讨论】:

      【解决方案2】:

      感谢 s_t 的回答。但是,我无法将此方法应用于我的数据集。

      请找到我的数据集

      set.seed(123)
      ZS=c(rep(0,4),rep(300,4),rep(600,4),rep(900,4),rep(1200,4),rep(1500,4),rep(1800,4),rep(2100,4),rep(2400,4),rep(2700,4),rep(3000,4))
      MEAN=c(21+rnorm(4),19+rnorm(4),17+rnorm(4),15+rnorm(4),13+rnorm(4),12+rnorm(4),13+rnorm(4),9+rnorm(4),6+rnorm(4),5+rnorm(4),1+rnorm(4))
      Model_short=c(rep(c("mod3","mod4","obs1","obs2"),11))
      color=c(rep(c("r","r","ref1","ref2"),11))
      size=c(rep(c(0.5,0.5,1,1),11))
      alpha=c(rep(c(0.5,0.5,1,1),11))
      
      data=data.frame(
      ZS=ZS,
      MEAN=MEAN,
      Model_short=Model_short,
      color=color,
      size=size,
      alpha=alpha)
      

      这里有脚本:

      library(ggplot2)
      myColors=c("#4CA54C","#000000","#717171")
      names(myColors) <- levels(data$color)
      colScale <- scale_colour_manual(name = "grp",values = myColors)
      
      p<-ggplot(data,aes(x = ZS,y=MEAN,group=Model_short,color=color)) + 
      geom_line(size=data$size,alpha=data$alpha)+
      theme_bw()+theme(legend.position = "right")+
      ylab("ylab")+
      xlab("xlab")+ 
      coord_flip()+ggtitle("title")+colScale
      print(p)
      

      非常感谢

      plot_x_y

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-10
        • 2021-07-29
        • 1970-01-01
        • 1970-01-01
        • 2018-11-22
        • 2021-08-10
        • 1970-01-01
        相关资源
        最近更新 更多