[R] How to manually color specific bars

Jeff Reichman reichm@nj @ending from @bcglob@l@net
Wed Aug 22 03:44:00 CEST 2018


R-Help Forum

 

While the following code works fine I need to change (highlight) specific
"bars" within plot 2 (p2). For example I want the bars to be  (lets say)
red, on  1 Aug 2016 and 1 Aug 2017 . What do I need to do?

 

library(ggplot2)

library(reshape2)

library(scales)

library(egg)

#data <- dataset

data <- read.csv("~/R/Data_Files/AreaPlotData.csv", stringsAsFactors=FALSE)

data$Serial <- seq.int(nrow(data))

data$min <- pmin(data$Melbourne,data$Sydney)

data <- melt(data, id.vars=c("Serial","min","Timeline"), value.name="Price")

data$Timeline <- as.Date(data$Timeline, format="%m/%d/%Y")

p1 <- ggplot(data, aes(x = Timeline, y = Price)) +

  geom_line(aes(col = variable)) + geom_ribbon(aes(ymin = min, ymax = Price,
fill = variable), alpha = 0.3) + 

  scale_color_manual(values = c("#144A90","#D81F26")) + 

  scale_fill_manual(values = c("#F7A396","#88CADD")) + 

  theme_get() + theme(legend.position="top", legend.title=element_blank()) +

  scale_x_date(labels=date_format("%b%y"))

 

data2 <- read.csv("~/R/Data_Files/AreaPlotData2.csv",
stringsAsFactors=FALSE)

data2$Timeline <- as.Date(data2$Timeline, format="%m/%d/%Y")

p2 <- ggplot(data2, aes(x = Timeline, y=Port)) + 

  geom_bar(stat = "identity", width = 0.1, color = "blue") +

  scale_y_continuous(name="Port Holdings", limits=c(0, 40))

 

ggarrange(p1, p2, heights = c(2, 0.6),ncol = 1, nrow = 2)

 

Jeff


	[[alternative HTML version deleted]]



More information about the R-help mailing list