简体   繁体   English

ggplot2用于离散比例的连续颜色并删除图例

[英]ggplot2 continuous colors for discrete scale and delete a legend

I have the following data frame: 我有以下数据框:

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

In fact I have 615 zones, X and Y are the Lambert coordinates and Value is the rain. 实际上我有615个区域,X和Y是Lambert坐标,Value是下雨。 My zones represents France and I am trying to plot them: 我的区域代表法国,我试图绘制它们:

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]")
        )

I use sympzones and polfrance to draw the contour of my zones. 我使用sympzones和polfrance绘制我的区域的轮廓。

AND

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
}

Values are from 300 to 1900 and this is what I obtain: 值从300到1900,这是我获得的:

情节

I have a problem: 我有个问题:

I can't change the legend, this is the legend from geom_file and it doesn't take into account the scale_colour_manual. 我无法更改图例,这是geom_file的图例,并没有考虑scale_colour_manual。 So this is not an ascending order, not the good title (I want "mm") and not the good colors. 所以这不是一个升序,而不是好的标题(我想要“mm”)而不是好的颜色。

I don't know if it is really clear... Can someone help me? 我不知道是否真的很清楚......有人能帮助我吗?

Edit: I took inspiration from that: easiest way to discretize continuous scales for ggplot2 color scales? 编辑:我从中获取灵感: 最简单的方法来对ggplot2色标的连续比例进行离散化?

You have two scales because you are using both a fill and a color aesthetic, and you are using different variables for them (one discrete, one continuous). 你有两个比例,因为你同时使用fillcolor美学,并且你使用不同的变量(一个离散,一个连续)。 What you want to do is use a single variable just for the fill. 你想要做的是只使用一个变量填充。

Additionally, your labels are all messed up because you are passing them as character, which ggplot will then sort lexically (so "10" comes before "2"). 此外,你的标签都搞砸了,因为你将它们作为字符传递,然后ggplot将在词汇上排序(所以“10”出现在“2”之前)。

Here is a solution that bypasses both these problems. 这是一个绕过这两个问题的解决方案。 We keep the original factor format for the output of cut , which will be labeled in the correct order, and we just use the fill aesthetic. 我们保留cut输出的原始factor格式,将以正确的顺序标记,我们只使用fill美学。 Note also how much simpler it is to set the scale by creating the colors and labeling them with the levels, and using scale_fill_manual : 另请注意,通过创建颜色并使用级别标记它们并使用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)))

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM