本文介绍了我如何应用渐变填充到ggplot2中的geom_rect对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用ggplot2构建一个时间序列折线图,该图利用geom_rect对象来突出显示特定的时间序列事件。 出于纯粹的美学原因,我我对将渐变应用到geom_rect对象感兴趣,因此随着y的增加它渐变为白/透明。 我已经阅读了其他的答案,有人建议 geom_tile 或geom_raster可能会提供一个解决方案。我对此没有任何好运...对我来说geom_rect似乎是一个明显的选择,因为我可以指定时间序列的开始和结束作为边界。但是,我希望我可以被证明是错误的!如果有人有任何指导,将非常感激。到目前为止,我的尝试是: ##读数据 file =Data.csv timeSeries< - read.csv(file,header = TRUE) ## CONVERT DATA TO DATE CLASS timeSeries $ Date< - as.Date( timeSeries $ Date,%d /%m /%y) timeSeries $ Date< - as.Date(format(timeSeries $ Date,19%y-%m-%d)) $ b $ ## SET GEOM_RECT数据帧 事件< -c(Event1,Event2,Event3) startDate< - c(15 / 06/15,12/07/17,6/09/18) finishDate< - c(9/01/16,18/11/17,5 / 11/18) 日期< - cbind(event,startDate,finishDate)日期< - as.data.frame(日期,rownames = NULL,stringsAsFactors = FALSE) 日期$ startDate< - as.Date(日期$ startDate,%d /%m /%y)日期$ startDate< - as.Date(格式$ startDate,19%y-%m-%d)) 日期$ finishDate< - as.Date(日期$ finishDate,%d /%m /%y)日期$ finishDate< - as.Date(格式(日期$ finishDate,19%y-%m-%d)) ##使用GGPLOT plot geom_rect(data = dates,aes(xmin = startDate,xmax = finishDate,ymin = 0,ymax = 25),fill =blue, alpha = 0.4)+ geom_line(aes(x = Date,y = Event))+ scale_x_date(labels = date_format(19%y))+ ggtitle() + xlab(Time Series)+ ylab(Number)+ theme_minimal() plot 上面的代码应该产生这个图。您可以从此处下载数据。 解决方案这里是我对@ baptiste想法的实现。看起来很花哨! ggplot_grad_rects y_steps alpha_steps rect_grad ymax = y_steps [-1], alpha = alpha_steps) rect_total< - merge(dates,rect_grad) ggplot(timeSeries)+ geom_rect(data = rect_total, aes(xmin = startDate,xmax = finishDate, ymin = ymin,ymax = ymax, alpha = alpha ),fill =blue)+ guides(alpha = FALSE)} ggplot_grad_rects(100,0,25)+ geom_line(aes(x = Date,y = Event))+ scale_x_date(labels = date_format(19%y))+ ggtitle()+ xlab(Time Series)+ ylab(Number)+ theme_minimal() I'm in the midst of building a time series line chart using ggplot2 that utilises geom_rect objects to highlight a particular time series event.For purely aesthetic reasons I'm interested in applying a gradient to the geom_rect object so that it fades to white/transparent as y increases.I have read other answers where it has been suggested that geom_tile or geom_raster may offer a solution. I've had no luck with this... to me geom_rect seems to be the obvious choice since I can specify the beginning and end of the time series as boundaries. However, I'm hoping that I can be proven wrong! If anyone has any guidance, it would be very much appreciated. My attempt thus far is:## READ DATAfile = "Data.csv"timeSeries <- read.csv(file, header=TRUE)## CONVERT DATA TO DATE CLASStimeSeries$Date <- as.Date(timeSeries$Date, "%d/%m/%y")timeSeries$Date <- as.Date(format(timeSeries$Date, "19%y-%m-%d"))## SET GEOM_RECT DATA FRAMEevent <- c("Event1", "Event2", "Event3")startDate <- c("15/06/15", "12/07/17", "6/09/18")finishDate <- c("9/01/16", "18/11/17", "5/11/18")dates <- cbind(event, startDate, finishDate)dates <- as.data.frame(dates, rownames=NULL, stringsAsFactors=FALSE)dates$startDate <- as.Date(dates$startDate, "%d/%m/%y")dates$startDate <- as.Date(format(dates$startDate, "19%y-%m-%d"))dates$finishDate <- as.Date(dates$finishDate, "%d/%m/%y")dates$finishDate <- as.Date(format(dates$finishDate, "19%y-%m-%d"))## PLOT USING GGPLOTplot <- ggplot(timeSeries) + geom_rect(data=dates, aes(xmin=startDate, xmax=finishDate, ymin=0, ymax=25), fill="blue", alpha=0.4) + geom_line(aes(x=Date, y=Event)) + scale_x_date(labels=date_format("19%y")) + ggtitle("") + xlab("Time Series") + ylab("Number") +theme_minimal()plotThe code above should produce this plot. Data can be downloaded from here. 解决方案 Here's my implementation of @baptiste's idea. Looks fancy!ggplot_grad_rects <- function(n, ymin, ymax) { y_steps <- seq(from = ymin, to = ymax, length.out = n + 1) alpha_steps <- seq(from = 0.5, to = 0, length.out = n) rect_grad <- data.frame(ymin = y_steps[-(n + 1)], ymax = y_steps[-1], alpha = alpha_steps) rect_total <- merge(dates, rect_grad) ggplot(timeSeries) + geom_rect(data=rect_total, aes(xmin=startDate, xmax=finishDate, ymin=ymin, ymax=ymax, alpha=alpha), fill="blue") + guides(alpha = FALSE)}ggplot_grad_rects(100, 0, 25) + geom_line(aes(x=Date, y=Event)) + scale_x_date(labels=date_format("19%y")) + ggtitle("") + xlab("Time Series") + ylab("Number") + theme_minimal() 这篇关于我如何应用渐变填充到ggplot2中的geom_rect对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 20:20