2014-03-31 102 views
1

的followong代码彩色化一个geombar

library(ggplot2) 
library(data.table) 

dt.source <- data.table(
         category = c("A", "B", "C", "D"), 
         value = c(100, 80, 85, 11) 
         ) 

bar.width <- 0.9 
p <- ggplot() 
p <- p + geom_bar(data = dt.source, aes(x = category, y = value, width = bar.width), 
        stat = "identity", position = "dodge", fill = "darkgrey") 
p 

producess此条形图(竖线)内的附加空间,

enter image description here

我知道如何避免由ggplot添加额外的空间为了更好的可读性 使用

p <- p + scale_x_discrete(expand = c(0, 0)) 

现在我想知道是否有可能为这个额外的空间增加额外的颜色,例如

enter image description here

能否请你给我一个提示在哪里可以找到详细说明如何控制panel.border。我有点犹豫,开始重读Hadley Wickhams书籍的原因。

我刚刚意识到我错过了着色图表右侧的额外空间,我不得不承认,我并不知道图表顶部的一些额外空间。

与往常一样,提前千恩万谢

+0

我可能是错的,但“我有点犹豫,开始重读哈德利Wickhams出于这个原因的书“听起来很像你在阅读手册和帮助文本时犹豫不决,但期望别人花时间做这件事。 – Henrik

+0

@Henrik,我不会犹豫地阅读书籍,我也毫不犹豫地阅读手册,这就是为什么我要求提示的原因,在这里我可以找到详细的描述如何控制面板情节的边界。 –

回答

1

可以只让一对夫妇的矩形

library(ggplot2) 
p <- ggplot(data=diamonds, aes(x=price, y=carat)) + geom_line(aes(color=color)) 
rect <- data.frame (xmin1 = -Inf, xmax1 = Inf, 
        ymin1 = -Inf, ymax1 = 0, 
        xmin2 = -Inf, xmax2 = 0, 
        ymin2 = -Inf, ymax2 = Inf) 
p + geom_rect(data=rect, aes(xmin=xmin1, xmax=xmax1, ymin=ymin1, ymax=ymax1), 
       color="red", fill="red", inherit.aes=FALSE) + 
    geom_rect(data=rect, aes(xmin=xmin2, xmax=xmax2, ymin=ymin2, ymax=ymax2), 
       color="red", fill="red", inherit.aes=FALSE) 

enter image description here