【问题标题】:Specifying hexadecimal colours in R ggplot2 when grouping data分组数据时在 R ggplot2 中指定十六进制颜色
【发布时间】:2015-05-11 08:31:42
【问题描述】:

此处保存的数据 https://www.dropbox.com/s/p13uc6js3ag3sm9/senescence_N.csv?dl=0

我正在尝试根据等位基因使用特定的十六进制颜色为条形着色。 目前,默认颜色使用 scale_fill_manual 覆盖我的规范。

如何让线条被我指定的三种颜色着色?

    sen<-read.csv("senescence_N.csv",as.is=T)
    sen$ID <- factor(sen$ID)

    library("ggplot2")

    ##DATA SUMMARY##
    ##code for data summary from 
    ## http://www.cookbook-r.com/Manipulating_data/Summarizing_data/
    ##    data: a data frame.
    ##   measurevar: the name of a column that contains the variable to be 

    summariezed
##   groupvars: a vector containing names of columns that contain grouping variables
##   na.rm: a boolean that indicates whether to ignore NA's
##   conf.interval: the percent range of the confidence interval (default is 95%)


summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
  require(plyr)

  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }

  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )

  # Rename the "mean" column    
  datac <- rename(datac, c("mean" = measurevar))

  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean

  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult

  return(datac)
}

##using function to summarise data 

summarySE(sen, measurevar="score", groupvars=c("treatment", "allele","day.degrees"))
summary.sen<-summarySE(sen, measurevar="score", groupvars=c("treatment", "allele","day.degrees"))



      pd <- position_dodge(1)
  ggplot(summary.sen, aes(x=day.degrees, y=score, group=allele, colour=allele)) + 
  geom_errorbar(aes(ymin=score-se, ymax=score+se), colour="black", width=1, position=pd) +
  geom_line(position=pd,aes(fill=allele)) +
  scale_fill_manual(values=c("#0000FF","#CE00CE","#00CC00")) +
  ylab("Senescence score") +
  xlab("Thermal time since sowing  (°C d)") +
  geom_point(aes(shape=allele),position=pd, size=2) +
facet_grid(treatment~., scales="free") +
  theme(panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank(),
        plot.title = element_text(lineheight=.4, face="bold"),
        axis.title = element_text(size=16, face="bold", colour="black"), 
        axis.text.y = element_text(size=12, colour="black"),
        axis.text.x = element_text(vjust=0.5, size=16,colour="black"),
        strip.text.y=element_text(size =16, face="bold"),
        strip.background=element_rect(colour = "black"),
        legend.text= element_text(size = 16),
        legend.position="top") +
  guides(fill=guide_legend(title=NULL))

【问题讨论】:

    标签: r colors ggplot2


    【解决方案1】:

    在 ggplot 中,条形、矩形、区域等有“填充”,但线条没有;他们有“颜色”。在 ggplot 命令中,尝试替换以下行:

    scale_fill_manual(values=c("#0000FF","#CE00CE","#00CC00"))
    

    与:

    scale_colour_manual(values=c("#0000FF","#CE00CE","#00CC00"))
    

    【讨论】:

      猜你喜欢
      • 2019-02-10
      • 2017-10-01
      • 2020-03-05
      • 2014-12-01
      • 2015-10-07
      • 2014-07-03
      • 2019-06-16
      • 2021-03-08
      • 2011-10-25
      相关资源
      最近更新 更多