简体   繁体   中英

Specifying hexadecimal colours in R ggplot2 when grouping data

Data saved here https://www.dropbox.com/s/p13uc6js3ag3sm9/senescence_N.csv?dl=0

I'm trying to colour the bars according to allele, with the specific hexadecimal colours. Currently, the default colours override my specification using scale_fill_manual .

How do I get the lines to be coloured by the three colours I have specified?

    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))

In ggplot, bars, rectangles, areas, etc have "fill", but lines do not; they have "colour". In the ggplot command, try replacing the line:

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

with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM