ggplot2传奇标题位置

时间:2016-09-12 23:03:28

标签: r ggplot2

我使用以下代码创建热图:

library(ggplot2)
gamma_df <- read.csv("https://www.dropbox.com/s/1kpclcq7o907t61/blogs_test.csv?dl=1")
p <- ggplot(data = gamma_df, aes(x=gamma2, y=gamma3, fill=predacc)) + geom_tile()
p <- p + scale_fill_gradient2(low = "white", high = "red",
                              limit = c(0.1,0.4), space = "Lab",
                              name="Discounts")
p <- p + theme(legend.position="right")
p

我的结果如下:

enter image description here

我一直未能成功地将图例标题移动一点,以至于它不会与值重叠。我尝试添加

p + guides(color=guide_colourbar(title.vjust=3))

建议here,其中包含 title.vjust 参数的各种值,但没有任何运气。有人知道这个的魔力吗?

1 个答案:

答案 0 :(得分:3)

快速解决方案只是在图例标题的末尾添加换行符:

library(ggplot2)
gamma_df <- read.csv("https://www.dropbox.com/s/1kpclcq7o907t61/blogs_test.csv?dl=1")
p <- ggplot(data = gamma_df, aes(x=gamma2, y=gamma3, fill=predacc)) + geom_tile()
p <- p + scale_fill_gradient2(low = "white", high = "red",
                              limit = c(0.1,0.4), space = "Lab",
                              name="Discounts\n")
p <- p + theme(legend.position="right")
p

虽然你可能最好摆脱limit =

gamma_df <- read.csv("https://www.dropbox.com/s/1kpclcq7o907t61/blogs_test.csv?dl=1")
    p <- ggplot(data = gamma_df, aes(x=gamma2, y=gamma3, fill=predacc)) + geom_tile()
    p <- p + scale_fill_gradient2(low = "white", high = "red",
                                  space = "Lab",
                                  name="Discounts")
    p <- p + theme(legend.position="right")
    p