多彩多姿的背景,从网格线偏移的值

时间:2013-06-13 19:15:32

标签: r ggplot2 title background-color gridlines

我想用R来生成如下图:

https://dl.dropboxusercontent.com/u/9297902/R_desired_output.png

比例从左边的0到右边的100,每个主要类别(差,公平等)占25%。我想我需要: 1.制作多个颜色背景框(#D55E00,#E69F00,#56B4E9,#009E73) 2.更改网格线颜色,位置(从我的y类别值到它们之间)和厚度 3.选择一个合适的符号(这允许我有大纲和填充条?) 4.以更加可控,可预测的方式将标签置于图表之上(差,一般,良好,优秀)

这是我到目前为止的地方:

# assemble data
metric<-c("Bottom Substrate","Deposition",
   "Substrate Stability","In-stream Cover",
   "Pool Substrate","Pool Quality",
   "Pool Variability","Channel Alteration",
   "Channel Sinuosity","Width/Depth",
   "Hydrolic Diversity","Canopy Cover",
   "Bank Vegetation","Immediate Land Use",
   "Flow Related Refugia")
score<-c(10.53,18.18,13.33,9.09,
   26.32,6.67,6.67,57.14,
   18.18,40.00,27.27,
   9.09,73.33,71.43,27.27)

hab<-data.frame(metric,score) #create data frame

library(ggplot2)

# colorblind friendly colors
# #000000 # Black
# #E69F00 # Orange
# #56B4E9 # Sky Blue
# #009E73 # bluish Green
# #F0E442 # Yellow
# #0072B2 # Blue
# #D55E00 # Vermillion
# #CC79A7 # reddish Purple

# set up to remove x axis values, and axis titles
theme_mod <- theme(axis.text.x = element_blank(),
   axis.title.x = element_blank(), 
   axis.title.y = element_blank()) 

qplot(score,metric) +
   geom_point(aes(score,metric), size=6, pch="|") + # pch gets the symbol I want, how to lose the dot?
   scale_x_continuous(limits=c(0,100)) + # locks scale to be 0-100, whcih I want
   opts(title="Poor                    Fair                       Good                        Excellent") +
   theme_mod # removes axis stuff

所以,我还有很长的路要走。它似乎也出于某种原因对我的数据集进行了重新排序。

我在这里寻找可能性,但我不确定要走哪条路:

Using ggplot2 in R, how do I make the background of a graph different colours in different regions?

对不起,我有点新手 - 提前感谢任何指示。

1 个答案:

答案 0 :(得分:1)

您可以将geom_rectgeom_line一起使用来模拟此效果。如果您确实需要行而非点数,则可以在shape="|"中添加geom_point

   p<-ggplot(hab,aes(x=score, y=metric))+theme_classic()+geom_rect(aes(xmin = 0 , xmax = 25) , ymin = -Inf , ymax = Inf ,fill = "#F15922") + 
geom_rect(aes(xmin = 25 , xmax = 50) , ymin = -Inf , ymax = Inf ,fill = "#F7941E")+
geom_rect(aes(xmin = 50 , xmax = 75) , ymin = -Inf , ymax = Inf ,fill = "#00BAF2")+
geom_rect(aes(xmin = 75 , xmax = 100) , ymin = -Inf , ymax = Inf ,fill = "#00A975")+
geom_vline(xintercept=seq(0,100,by=25),colour="white",size=1.5)+
geom_hline(yintercept=c(seq(0,0.5,by=0.1),seq(1.5,15.5,by=1),seq(15.5,16,by=0.1)),colour="white",size=1.5)+
geom_point(colour="white", size=4) + geom_point(colour = "black",size=3)+
geom_text(aes(label = "Poor", x = 12.5, y = 16), vjust = 1.2,size=4)+
geom_text(aes(label = "Fair", x = 37.5, y = 16), vjust = 1.2,size=4)+
geom_text(aes(label = "Good", x = 62.5, y = 16), vjust = 1.2,size=4)+
geom_text(aes(label = "Excellent", x = 87.5, y = 16), vjust = 1.2,size=4)

p

enter image description here