如何修改图像图例?

时间:2019-06-01 12:58:50

标签: r ggplot2 plot

我想在白天创建图像并绘制不同的图案(例如吃饭等)

我正在使用以下示例创建图像图

# ----- Define a function for plotting a matrix ----- #

PlotMatrix <- function(x, ...)
{
  min <- min(x)
  max <- max(x)
  yLabels <- rownames(x)
  xLabels <- colnames(x)
  title <-c()

  # check for additional function arguments
  if( length(list(...)) ){
    Lst <- list(...)
    if( !is.null(Lst$zlim) ){
      min <- Lst$zlim[1]
      max <- Lst$zlim[2]
    }
    if( !is.null(Lst$yLabels) ){
      yLabels <- c(Lst$yLabels)
    }
    if( !is.null(Lst$xLabels) ){
      xLabels <- c(Lst$xLabels)
    }
    if( !is.null(Lst$title) ){
      title <- Lst$title
    }
  }
  # check for null values
  if( is.null(xLabels) ){
    xLabels <- c(1:ncol(x))
  }
  if( is.null(yLabels) ){
    yLabels <- c(1:nrow(x))
  }

  layout(matrix(data=c(1,2), nrow=1, ncol=2), widths=c(4,1), heights=c(8,1))

  # Red and green range from 0 to 1 while Blue ranges from 1 to 0
  ColorRamp <- rgb( seq(0,1,length=256),  # Red
                    seq(0,1,length=256),  # Green
                    seq(1,0,length=256))  # Blue
  ColorLevels <- seq(min, max, length=length(ColorRamp))

  # Reverse Y axis
  reverse <- nrow(x) : 1
  yLabels <- yLabels[reverse]
  x <- x[reverse,]

  par(mar = c(3,5,2.5,2))
  image(1:length(xLabels), 1:length(yLabels), t(x), col=ColorRamp, xlab="",
        ylab="", axes=FALSE, zlim=c(min,max))
  if( !is.null(title) ){
    title(main=title)
  }
  axis(BELOW<-1, at=1:length(xLabels), labels=xLabels, cex.axis=0.7)
  axis(LEFT <-2, at=1:length(yLabels), labels=yLabels, las= HORIZONTAL<-1,
       cex.axis=0.7)

  # Color Scale
  par(mar = c(3,2.5,2.5,2))
  image(1, ColorLevels,
        matrix(data=ColorLevels, ncol=length(ColorLevels),nrow=1),
        col=ColorRamp,
        xlab="",ylab="",
        xaxt="n")

  layout(1)
}

enter image description here

如何更改此代码图例,使其仅具有1个执行活动和0个未执行活动的比例?还有如何更改y和x轴?

我的数据结构:

structure(list(act1_1 = c(0, 0, 0, 0), act1_2 = c(0, 0, 0, 0), 
    act1_3 = c(0, 0, 0, 0), act1_4 = c(0, 0, 0, 0), act1_5 = c(0, 
    0, 0, 0), act1_6 = c(0, 0, 0, 0), act1_7 = c(0, 0, 0, 0), 
    act1_8 = c(0, 0, 0, 0), act1_9 = c(0, 0, 0, 0), act1_10 = c(0, 
    0, 0, 0), act1_11 = c(0, 0, 0, 0), act1_12 = c(0, 0, 0, 0
    ), act1_13 = c(0, 0, 0, 0), act1_14 = c(0, 0, 0, 0), act1_15 = c(0, 
    0, 0, 0), act1_16 = c(0, 0, 0, 0), act1_17 = c(0, 0, 0, 0
    ), act1_18 = c(0, 0, 0, 0), act1_19 = c(0, 0, 0, 0), act1_20 = c(0, 
    4, 0, 0), act1_21 = c(0, 4, 0, 0), act1_22 = c(0, 0, 0, 0
    ), act1_23 = c(0, 0, 0, 0), act1_24 = c(0, 0, 0, 0), act1_25 = c(0, 
    0, 0, 0), act1_26 = c(0, 0, 0, 0), act1_27 = c(0, 0, 0, 0
    ), act1_28 = c(0, 0, 0, 0), act1_29 = c(0, 0, 0, 0), act1_30 = c(0, 
    0, 0, 0), act1_31 = c(4, 0, 0, 0), act1_32 = c(0, 0, 0, 4
    ), act1_33 = c(0, 0, 0, 4), act1_34 = c(0, 0, 0, 0), act1_35 = c(0, 
    0, 0, 0), act1_36 = c(0, 0, 0, 0), act1_37 = c(0, 0, 0, 0
    ), act1_38 = c(0, 0, 0, 0), act1_39 = c(0, 0, 4, 0), act1_40 = c(0, 
    0, 4, 0), act1_41 = c(0, 0, 4, 0), act1_42 = c(0, 0, 4, 0
    ), act1_43 = c(0, 0, 0, 0), act1_44 = c(0, 0, 0, 0), act1_45 = c(0, 
    0, 0, 0), act1_46 = c(0, 0, 0, 0), act1_47 = c(0, 0, 0, 0
    ), act1_48 = c(0, 0, 0, 0), act1_49 = c(0, 4, 0, 0), act1_50 = c(0, 
    4, 0, 0), act1_51 = c(0, 0, 0, 0), act1_52 = c(0, 0, 0, 0
    ), act1_53 = c(0, 0, 0, 0), act1_54 = c(0, 0, 0, 0), act1_55 = c(0, 
    0, 0, 4), act1_56 = c(0, 0, 0, 4), act1_57 = c(4, 0, 0, 4
    ), act1_58 = c(4, 0, 0, 4), act1_59 = c(4, 0, 0, 4), act1_60 = c(4, 
    0, 0, 4), act1_61 = c(0, 0, 0, 0), act1_62 = c(0, 0, 0, 0
    ), act1_63 = c(0, 0, 0, 0), act1_64 = c(0, 0, 0, 0), act1_65 = c(0, 
    0, 0, 0), act1_66 = c(0, 0, 0, 0), act1_67 = c(0, 0, 0, 0
    ), act1_68 = c(0, 0, 0, 0), act1_69 = c(0, 0, 0, 0), act1_70 = c(0, 
    0, 0, 0), act1_71 = c(0, 0, 0, 0), act1_72 = c(0, 0, 0, 0
    ), act1_73 = c(0, 0, 4, 0), act1_74 = c(0, 0, 4, 0), act1_75 = c(0, 
    0, 4, 0), act1_76 = c(0, 0, 0, 0), act1_77 = c(0, 0, 0, 0
    ), act1_78 = c(0, 0, 0, 0), act1_79 = c(0, 0, 0, 0), act1_80 = c(0, 
    0, 0, 0), act1_81 = c(0, 0, 0, 0), act1_82 = c(0, 4, 0, 0
    ), act1_83 = c(0, 0, 0, 0), act1_84 = c(0, 0, 0, 4), act1_85 = c(4, 
    0, 0, 4), act1_86 = c(4, 0, 0, 4), act1_87 = c(4, 0, 0, 4
    ), act1_88 = c(0, 0, 0, 4), act1_89 = c(0, 0, 0, 0), act1_90 = c(0, 
    0, 0, 0), act1_91 = c(0, 0, 0, 0), act1_92 = c(0, 0, 0, 0
    ), act1_93 = c(0, 0, 0, 0), act1_94 = c(0, 0, 0, 0), act1_95 = c(0, 
    0, 0, 0), act1_96 = c(0, 0, 0, 0), act1_97 = c(0, 0, 0, 0
    ), act1_98 = c(0, 0, 0, 0), act1_99 = c(0, 0, 0, 0), act1_100 = c(0, 
    0, 0, 0), act1_101 = c(0, 0, 0, 0), act1_102 = c(0, 0, 0, 
    0), act1_103 = c(4, 0, 0, 0), act1_104 = c(0, 0, 0, 0), act1_105 = c(0, 
    0, 0, 0), act1_106 = c(0, 0, 0, 0), act1_107 = c(0, 0, 0, 
    0), act1_108 = c(0, 0, 0, 0), act1_109 = c(0, 0, 0, 0), act1_110 = c(0, 
    0, 0, 4), act1_111 = c(0, 0, 0, 4), act1_112 = c(0, 0, 0, 
    4), act1_113 = c(0, 0, 0, 4), act1_114 = c(0, 0, 0, 0), act1_115 = c(0, 
    0, 0, 0), act1_116 = c(0, 0, 0, 0), act1_117 = c(0, 0, 0, 
    0), act1_118 = c(0, 0, 0, 0), act1_119 = c(0, 0, 0, 0), act1_120 = c(0, 
    0, 0, 0), act1_121 = c(0, 0, 0, 0), act1_122 = c(0, 0, 0, 
    0), act1_123 = c(0, 0, 0, 0), act1_124 = c(0, 0, 0, 0), act1_125 = c(0, 
    0, 0, 0), act1_126 = c(0, 0, 0, 0), act1_127 = c(0, 0, 0, 
    0), act1_128 = c(0, 0, 0, 0), act1_129 = c(0, 0, 0, 0), act1_130 = c(0, 
    0, 0, 0), act1_131 = c(0, 0, 0, 0), act1_132 = c(0, 0, 0, 
    0), act1_133 = c(0, 0, 0, 0), act1_134 = c(0, 0, 0, 0), act1_135 = c(0, 
    0, 0, 0), act1_136 = c(0, 0, 0, 0), act1_137 = c(0, 0, 0, 
    0), act1_138 = c(0, 0, 0, 0), act1_139 = c(0, 0, 0, 0), act1_140 = c(0, 
    0, 0, 0), act1_141 = c(0, 0, 0, 0), act1_142 = c(0, 0, 0, 
    0), act1_143 = c(0, 0, 0, 0), act1_144 = c(0, 0, 0, 0)), label = "Written by R.", row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

0 个答案:

没有答案