3/1/2018 10:44:17 PM
一个折腾了一下的关于怎么自定义facet里面每个图形的背景颜色
## 简单的数据导入
input=read.table(file.choose(),header=FALSE) ## temp2.txt
names(input)=c("my.x","my.y","dim1","dim2","loc")
library("ggplot2")
input$dim2 <- as.factor(input$dim2) 、
## 注意这里一定要转换成因子不然会报错
Error in `$<-.data.frame`(`*tmp*`, facet_fill_color, value = c("grey", :
replacement has 32208 rows, data has 45920
ggplot(input,aes(x=my.x,y=my.y,group=loc))+
facet_grid(dim2~dim1)+
theme(strip.background=element_blank())+###########删除标签
theme(panel.background=element_rect(fill=,color="black"))+
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
theme(axis.text=element_blank())+
theme(axis.ticks=element_blank())+
geom_rect(aes(fill = input$dim2),xmin = -Inf,xmax = Inf,
ymin = -Inf,ymax = Inf,alpha = 0.9) +
scale_fill_manual(values=c("0"="grey","1"="red","2"="black","3" = "green","4"= "yellow", "5" = "white","6"= "grey50","7"="blue")) +
geom_line(alpha=1/20) +
scale_colour_brewer(palette="Paired") +
labs(fill="dim2") ## 修改legend.title标签名
theme(legend.title=element_blank()) ## 不显示legend.title名
意外的收获
如果你需要自定义修改某一类的颜色,可以首先创建一列,给其赋值出你想要画的图形的factor
input$facet_fill_color <- c("red", "green", "blue", "yellow", "orange","black","grey","white")[input$dim2]
## 虽然最后不用这一步,但是意外学到可以通过如上操作替换dim2里面的factor
facet_background_color
http://note.youdao.com/noteshare?id=2bb07c0352372889ba80bc1a4dcd0562&sub=C758C85512744E098DBF35C1B562D10C
今天解决问题时候所查阅的一些链接
beautiful graphics ggplot2 (学习ggplot2的可以参考敲一下)
Background color rectangles in ggplot according to a factor value
ggplot2 - Change geom_rect
colour in a stacked barplot
Conditionally change panel background with facet_grid?
Multiple colors in a facet STRIP background
> 从上参考链接可以看出来
https://stackoverflow.com
这个网站能够找到许多我们目前遇到的问题答案