ggplot2:轴线为0,负数据点

时间:2013-09-09 16:21:08

标签: r ggplot2

我正在做正面和负面数据的boxplot,并希望轴在y = 0。 之后添加一行并不优雅,因为该行将在盒子上而不在它们后面。 (目标是使y = 0的线为黑色,而1和-1处的线应为灰色)

另外我想只有轴线。因此我用了

axis.line=element_line()
  ,panel.border = element_blank()

在主题中。但是垂直线超过1,看起来不太好(我的数据定义在-1和1之间)。

这是代码:

 require (ggplot2)
theme_jack <- function (base_size = 10, base_family = "") {
  theme_bw(base_size = base_size, base_family = base_family) %+replace% 
    theme(
      title = element_text(size = 12)
      ,axis.title.x = element_text(colour = "white", size=0)
      ,axis.title.y = element_text(size=12, angle=90)
      ,axis.text.x=element_text(size=8)
      ,panel.grid.major = element_line(colour = "grey")#,
      ,axis.line=element_line()
      ,panel.border = element_blank()
      ,panel.grid.minor = element_blank()
      ,panel.grid.major.x = element_blank() 
      ,legend.position = "none"

    )   
}
theme_set(theme_jack())

datatest2=structure(list(datatest2.genotype = structure(c(1L, 1L, 5L, 5L, 
                                                          1L, 5L, 5L, 5L, 1L, 5L, 1L, 5L, 1L, 5L, 5L, 1L, 1L, 1L, 5L, 5L, 
                                                          1L, 1L, 1L, 5L, 1L, 1L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
                                                          6L, 6L, 6L, 6L), .Label = c("CS_d42-chG80", "CS_G_c380cha", "CS_Gd42-chG80", 
                                                                                      "PKC_CS", "PKC_d42-chG80", "PKC_G_c380cha", "PKC_Gd42-chG80"), class = "factor"), 
                         datatest2.score = c(0.8882, -0.3775, -0.4053, 0.1962, 0.9982, 
                                             0.5627, -0.4865, 0.7267, 0.3276, 0.5017, 0.9731, 0.1525, 
                                             0.7857, 0.6121, 0.8508, 0.1311, -0.2457, 0.8848, -0.1254, 
                                             0.1047, -0.2715, 0.7189, 0.4115, 0.9704, -0.8328, -0.1301, 
                                             0.9756, 0.2317, 0.4297, 0.9967, 0.6423, 0.8516, 0.3386, 0.5208, 
                                             0.9148, 0.2539, 0.8581, 0.5621, 0.5969, 0.7435)), .Names = c("genotype", 
                                                                                                          "score"), row.names = c(NA, 40L), class = "data.frame")


p=ggplot(datatest2, aes(x=factor(genotype),y= score))
plot=p+ geom_boxplot()+ labs(x="genotype",y="PI during final test")+ 
  scale_fill_grey(start = 0.9, end = 0.9)+ ##allow good bw prints
  scale_y_continuous(minor_breaks=NULL,breaks = seq(-1 , 1, 1) )

plot

1 个答案:

答案 0 :(得分:2)

尝试使用{scale_y_continuous的expand参数。用这个替换最后一节,你应该得到更接近你想要的东西。您可能需要尝试以下值。

p = ggplot(datatest2, aes(x = factor(genotype), y = score))
plot = p+ geom_boxplot()+ labs(x = "genotype",
    y = "PI during final test")+
  scale_fill_grey(start = 0.9, end = 0.9)+ ##allow good bw prints
  scale_y_continuous(expand = c(0,0.005),
                     minor_breaks = NULL,
                     breaks = seq(-1 , 1, 1) ) +

    theme()

plot

screenshot