R 将ggplot2色条记号更改为黑色

R 将ggplot2色条记号更改为黑色,r,graphics,plot,ggplot2,R,Graphics,Plot,Ggplot2,在我的一些绘图中,我发现很难看到颜色栏中的记号。我还没有找到一个有文件证明的方法来改变蜱的颜色。所有的例子似乎都集中在更改标签或根本不绘制记号上。可能吗 # Data require(ggplot2) require(grid) n <- 100 x <- y <- seq(-4*pi, 4*pi, len=n) r <- cos( sqrt( outer(x^2, y^2, "+") ) ^ 2 ) df <- data.frame( x = rep( x ,

在我的一些绘图中,我发现很难看到颜色栏中的记号。我还没有找到一个有文件证明的方法来改变蜱的颜色。所有的例子似乎都集中在更改标签或根本不绘制记号上。可能吗

#  Data
require(ggplot2)
require(grid)
n <- 100
x <- y <- seq(-4*pi, 4*pi, len=n)
r <- cos( sqrt( outer(x^2, y^2, "+") ) ^ 2 )
df <- data.frame( x = rep( x , each = n) , y = rep( y , times = n ) , val = c(r) )

#  Plot
ggplot( df , aes( x , y , fill = val ) )+
  geom_raster()+
  scale_fill_gradient( low = "#FFFFFF" , high = "#de2d26" )+
  guides( fill = guide_colourbar( barheight = unit( 3 , "in" ) ) )+
  theme_bw()+
  theme( line = element_line( colour = "#0000FF" ) )
#数据
需要(ggplot2)
需要(网格)

n我通常通过大量使用
str
来找到需要更改的内容。我相信其他人可以做得更优雅

g <- ggplotGrob(p)
g$grobs[[8]][[1]][[1]]$grobs[[5]]$gp$col <- "black"

library(grid)
grid.draw(g)

g编辑:请参考下面克劳斯·威尔克的答案


我已将原始答案包括在下面,但请注意它现在已经过时,我不建议使用它。

我在我的ggplot2分叉中包括了自定义记号和图例边框的功能,但我想我会让这里的人们知道,以防他们在这个问题上绊倒

使用以下代码安装my fork

if (!require(devtools))
  install.packages('devtools')
install_github('paleo13/ggplot2')
ggplot(df, aes( x, y, fill = val)) +
  geom_raster() +
  scale_fill_gradient(low = "#FFFFFF", high = "#de2d26") +
  theme_bw() +
  theme(line = element_line(colour = "#0000FF")) +
  guides(fill = guide_colourbar(barheight = unit(3, "in"),
   ticks=element_line(color='black'), border=element_line(color='black')))
我们可以使用以下代码指定黑色标记

if (!require(devtools))
  install.packages('devtools')
install_github('paleo13/ggplot2')
ggplot(df, aes( x, y, fill = val)) +
  geom_raster() +
  scale_fill_gradient(low = "#FFFFFF", high = "#de2d26") +
  theme_bw() +
  theme(line = element_line(colour = "#0000FF")) +
  guides(fill = guide_colourbar(barheight = unit(3, "in"),
   ticks=element_line(color='black'), border=element_line(color='black')))


(我本来会将此作为一条评论,但我缺乏这样做的声誉,如果任何有足够特权的人想要删除此答案并将内容移动到评论中,请放心)

另一个答案中提到的拉取请求从未进入ggplot2代码库,但在开发版本(计划作为ggplot2.3发布)中,现在可以以稍微不同的方式实现这一点:

您还可以添加一个框架,当颜色栏中的某些颜色非常浅时,这可能很有用

ggplot(df, aes(x, y, fill = val)) +
  geom_raster() +
  scale_fill_gradient(low = "#FFFFFF", high = "#de2d26") +
  guides(fill = guide_colourbar(barheight = unit( 3 , "in" ),
                                ticks.colour = "black",
                                ticks.linewidth = 1,
                                frame.colour = "black",
                                frame.linewidth = 1)) +
  theme_bw() +
  theme(line = element_line(colour = "#0000FF"))

不幸的是,这是一个错误。您可以发送功能请求,将
guide\u colorbar
@baptiste添加到ggplot2邮件组,这听起来是一个合理的选择,有这样的帖子!谢谢。不确定那里的帖子是否会引起开发人员的注意,更好的方式是github上的一个新问题。@baptiste谢谢,我将尝试这种方式,谢谢@巴普蒂斯特:好的,已经长大了。希望这是开发者们会考虑的。谢谢这是一个非常合适的答案。我期待着看到这一点(希望)纳入ggplot。外部滴答声可能吗?