本文介绍了我怎样才能在ggplot中使用不同的背景色x值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想在R中使用ggplot库创建boxplot。我想要的是为各个x值设置各种背景色,例如后面的​​图像。我无法找到任何改变每个x轴的bgcolor的选项。只有我发现的是改变整个背景颜色,例如'theme_bw()'选项 在第一,第四和第五x轴上,bgcolor是蓝色的。但是,第二,第三,第六x轴没有任何价值,所以我想将红色设置为bgcolor。 感谢您的时间!解决方案也许你可以在这个玩具的例子上建立一些东西: nFac df y = runif(nFac * nDat)) rec xmax = tail(seq,-1), alpha = c(.5,0 ,0,5 .5,0)) library(ggplot2) ggplot()+ scale_x_discrete(seq_len(nFac))+ geom_rect(data = rec, aes(xmin = xmin, xmax = xmax, alpha = alpha), ymin = -Inf, ymax = Inf, fill = lightblue, color =blue, size = 2)+ geom_boxplot(data = df, aes(x = x,y = y))+ 主题(panel.background = element_rect(fill =pink), panel.grid.major = element_blank(), panel.grid.minor = element_blank())+ 指南(alpha = FALSE) I want to create a boxplot using ggplot library in R.What I want is that set various background color to each x value such as a following image. I cannot find any option for changing bgcolor of each x axis. Only thing I found is for changing entire background color such as 'theme_bw()' optionOn first, fourth, fifth x axis, bgcolor is blue. However, there is no value on second, third, sixth x axis, so I want to set red color as bgcolor.Thanks for your time! 解决方案 Maybe you can build up something on this toy example: nFac <- 6; nDat <- 10df <- data.frame(x = gl(nFac, nDat), y = runif(nFac * nDat))rec <- data.frame(xmin = head(seq <- seq(0.5, nFac + .5, 1), -1), xmax = tail(seq, -1), alpha = c(.5, 0, 0, .5, .5, 0))library(ggplot2)ggplot() + scale_x_discrete(seq_len(nFac)) + geom_rect(data = rec, aes(xmin = xmin, xmax = xmax, alpha = alpha), ymin = -Inf, ymax = Inf, fill = "lightblue", colour = "blue", size = 2) + geom_boxplot(data = df, aes(x = x, y = y)) + theme(panel.background = element_rect(fill = "pink"), panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + guides(alpha = FALSE) 这篇关于我怎样才能在ggplot中使用不同的背景色x值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 19:58