データセット内のデータ値の範囲に関係なく、2つの値の間のカラーグラデーションを拡大し、凡例にラベルを付ける方法を探しています。ylim()
基本的に、カラーグラデーションと同等の機能はありますか?
通常-1から1の間のaz値をプロットするコードが与えられた場合、ブレークがデータ範囲内にある場合、勾配をプロットしてラベルを付けることができます。
library(ggplot2)
#generator from http://docs.ggplot2.org/current/geom_tile.html
pp <- function (n, r = 4) {
x <- seq(-r * pi, r * pi, len = n)
df <- expand.grid(x = x, y = x)
df$r <- sqrt(df$x^2 + df$y^2)
df$z <- cos(df$r^2) * exp(-df$r / 6)
return(df)
}
t <- pp(30)
summary(t)
b <- c(-.5, 0, .5)
colors <- c('navyblue', 'darkmagenta', 'darkorange1')
p <- ggplot(data = t, aes(x = x, y = y))+
geom_tile(aes(fill = z))+
scale_fill_gradientn(colors = colors, breaks = b, labels = format(b))
ggsave(plot = p, filename = <somefile.png>, height = 3, width = 4)
しかし、ブレークを観測範囲外の値に変更すると、グラデーションの色が調整されていないように見え、グラデーションのラベルが表示されません。
b <- c(-3, 0, 3)