怎么自定义修改facet里面图形的背景颜色

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

今天解决问题时候所查阅的一些链接

ggplot2--legend图例的修改

ggplot2--legend图例的修改

ggplot2–legend图例的修改

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
这个网站能够找到许多我们目前遇到的问题答案
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,871评论 0 10
  • 简介 文章较长,点击直达我的博客,浏览效果更好。本文内容基本是来源于STHDA,这是一份十分详细的ggplot2使...
    taoyan阅读 51,722评论 7 159
  • 微信这是要起飞的节奏吗? 5月17日晚,微信发布最新6.5.8版本(iOS版),并通过微信「实验室」发布了二个重新...
    BAT俱乐部阅读 620评论 0 0
  • 春雨黄昏后 直到大学,这个三月才变得如此美丽。忙碌是今年三月生活的最好形容。刚刚从学生家离开,回学校的路上,我终于...
    绛紫儿阅读 260评论 0 0
  • 1冬风如期的来了。呼呼呼……像是那夜你向我的哭诉。梅花还是那种苍白,很忧伤,很忧伤的样子,让人心凉!!!那种凉,像...
    泪花香阅读 261评论 0 1

友情链接更多精彩内容