r - ggplot2 离散比例的连续颜色并删除图例

标签 r ggplot2

我有以下数据框:

df
zone        X        Y   Value
   1   604000  2673000     522
   1   612000  2643000     524
   .        .        .       .
   .        .        .       .
 615   604000  2673000     698

实际上我有 615 个区域,X 和 Y 是兰伯特坐标,值是雨。
我的区域代表法国,我正在尝试绘制它们:
mi <- 300
ma <- 1900

df$val <- cut(df$Sim, breaks=seq(mi,ma,100),
          labels = paste( "(", format(seq(mi,ma,100)[1:(length(seq(mi,ma,100))-1)]), 
                          ", ", format(seq(mi,ma,100)[-1]), "]", sep = ""))

breaks <- unique(bincol(df$Sim,"green","yellow","red"))
breaks <- breaks[order(unique(df$val))]

p <- ggplot(data =df, aes(x=X, y=Y))
p <- (p    
      + theme_bw()
      + coord_equal() 
      + geom_tile(aes(fill=val))
      + scale_colour_manual("mm", values = breaks)
      + geom_path(data = polfrance, colour = 'black', 
               aes(x = long, y = lat, group = group))
      + geom_path(data = sympzones, colour = 'grey40', 
                  aes(x = long, y = lat, group = group))
      )

p <- (p + scale_y_continuous(limits=c(1580000,2730000),
                         breaks=seq(1600000,2700000, 200000), 
                         labels= seq(1600,2700,200), expand=c(0,0))
        + scale_x_continuous(limits=c(0,1250000), 
                         breaks= seq(0,1250000, 400000), 
                         labels= seq(0,1250, 400), expand=c(0,0))
        + xlab("X Lambert  [km]")
        + ylab("Y Lambert  [km]")
        )

我使用 sympzones 和 polfrance 来绘制我的区域的轮廓。


bincol <- function(x,low,medium,high) {
  breaks <- function(x) pretty(range(x), n = nclass.Sturges(x), min.n = 1)

  colfunc <- colorRampPalette(c(low, medium, high))

  binned <- cut(x,breaks(x))

  res <- colfunc(length(unique(binned)))[as.integer(binned)]
  names(res) <- as.character(binned)
  res
}

值从 300 到 1900,这是我得到的:

plot

我有个问题:

我无法更改图例,这是来自 geom_file 的图例,它没有考虑 scale_colour_manual。所以这不是升序,不是好的标题(我想要“mm”),也不是好的颜色。

不知道是不是真的清楚... 有人可以帮帮我吗?

编辑:我从中获得灵感:easiest way to discretize continuous scales for ggplot2 color scales?

最佳答案

您有两个量表,因为您同时使用了 fill和一个 color美学,并且您为它们使用了不同的变量(一个是离散的,一个是连续的)。您想要做的是仅使用单个变量进行填充。

此外,您的标签都搞砸了,因为您将它们作为字符传递,然后 ggplot 将按词法排序(因此“10”在“2”之前)。

这是绕过这两个问题的解决方案。我们保留原版factor cut 的输出格式,它将以正确的顺序标记,我们只使用 fill审美的。还要注意通过创建颜色并用级别标记它们并使用 scale_fill_manual 来设置比例要简单得多。 :

library(ggplot2)
df <- expand.grid(1:10, 1:10)                   # make up data
df <- transform(df, z=Var1 * Var2)              # make up data
df <- transform(df, z.cut=cut(z, 10))           # bin data

colors <- colorRampPalette(c("blue", "yellow", "red"))(length(levels(df$z.cut)))
ggplot(df, aes(x=Var1, y=Var2, fill=z.cut)) + 
  geom_tile() +
  scale_fill_manual(values=setNames(colors, levels(df$z.cut)))

enter image description here

关于r - ggplot2 离散比例的连续颜色并删除图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23572660/

相关文章:

R 应用并从前一行获取值

r - 如何在r中绘制多列?

r - 使用自定义渐变填充直方图箱

r - ggplot2中的图例和图表颜色不匹配

r - 试图在 ggplot 中加粗 y 轴

r - 如何创建定期发送文本到 "log file",同时将正常输出打印到控制台?

r - 针织/rmarkdown/ latex : How to cross-reference figures and tables?

r - 如何在 R 中循环

c++ - 修改R包gbm

r - 如何根据分组变量对ggplot中的结果进行排序