不同的图例和填充颜色为facegted ggplot? [英] Different legends and fill colours for facetted ggplot?

查看:125
本文介绍了不同的图例和填充颜色为facegted ggplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的问题没有包含任何示例数据。我无法找到一种轻松生成示例形状文件的方法。希望有经验的 ggplot 用户可以从下面的描述中看到我想要做的事情。



ve得到了:

$ ul
$ a code> data.frame X关于示例的信息( plotid var1 var2 var3 var4 ,...)

  • 多边形shapefile Y 与样本地块的空间信息

  • shapefile Y (带有 maptools )和 fortify ing as data.frame Z ggplot2 )可以正常工作。 melt ing X X_melted merge -ing Z X_melted mapdf 也适用。



    这意味着现在我们有一个 data.frame var1 var2 var3

    现在我想绘制这样的数据框:

      pl1 <-ggplot(mapdf,aes(long,lat),group = group)
    pl1 <-pl1 + geom_polygon(aes(group = group,fill = value),color =黑色)
    pl1 < - pl1 + facet_grid(变量〜。)
    pl1 < - pl1 + coord_equal(比率= 1)
    pl1

    结果是每个变量都有一个面板的好图。面板的地图是相同的,但填充颜色随着变量的值而变化。到目前为止,一切都像一个魅力......有一个问题:

    变量具有不同的最小值和最大值。例如 var1 0 5 ,<$从 0 400 var3 c $ c> var2 5 10 等。在该示例中,填充颜色的图例从 0 400 var2 很好地绘制,但 var1 var3 基本上都是以相同的颜色。



    有没有一种方法可以为每个面板使用不同的图例?或者在 ggplot facet_wrap facet_grid c $ c $?b

    我可以为每个变量绘制单独的图并将它们与视口结合起来,但是存在大量变量,这将是很多工作。 / p>

    或者是否有另一个包或方法可以用来完成我想要做的事情?

    和帮助将非常感激。 :)

    编辑:
    借助于 ggplot2 -package描述,我构建了一个示例这说明了我的问题:

      id<  -  factor(c(1.1,2.1,1.2, (
    id = ids,
    val1 = cumsum(runif(6,max = 0.5)),
    val2 = cumsum(runif(6,max = 50))

    positions< - data.frame(
    id = rep(id,each = 4),
    x = c(2,1,1.1,2.2,1,0,0.3,1.1,2.2,1.1,1.2,2.5,1.1,0.3,
    0.5,1.2,2.5,1.2,1.3,2.7, (0.5,1.2,0.5,0.6,1.3),
    y = c(-0.5,0,1,0.5,0,0.5,1.5,1,0.5,1,2.1,1.7,1,1.5,
    2.2 ,2.1,1.7,2.1,3.2,2.8,2.1,2.2,3.3,3.2)


    值< - 熔化(值)
    数据点< - 合并(值,位置,by = c(id))

    p < - ggplot(数据点,aes(x = x,y = y))+ geom_polygon(aes(fill = value,group = id),color =black)
    p < - p + facet_wrap(〜变量)
    p

    右侧的面板在地图上显示了 var2 的不同值。然而,在左侧的面板上,所有多边形都具有相同的颜色。这是合乎逻辑的,因为所有面板只使用一种颜色渐变。我可以为每个面板使用不同的颜色渐变吗?

    解决方案

    目前每个绘图只能有一个刻度和y)。

    Sorry for not included any example data for my problem. I couldn’t find a way to easily produce an example shape file. Hopefully, experienced users of ggplot can see what I’d like to do from the description below.

    I’ve got:

    • A data.frame X with information about sample plots (plotid, var1, var2, var3, var4, …)

    • A polygon shapefile Y with spatial information for the sample plots

    Importation of the shapefile Y (with maptools) and fortifying as data.frame Z (ggplot2) works fine. melting X to X_melted works equally fine. merge-ing Z and X_melted to mapdf works as well.

    That means that now we have a data.frame in long form with spatial information and var1, var2, var3, …

    Now I want to plot this data frame like this:

    pl1 <- ggplot(mapdf,aes(long,lat),group=group)
    pl1 <- pl1 + geom_polygon(aes(group=group,fill=value),colour="black")
    pl1 <- pl1 + facet_grid(variable ~ .)
    pl1 <- pl1 + coord_equal(ratio = 1)
    pl1
    

    The result is a nice plot with one panel for each variable. The maps of the panels are identical, but fill colour varies with the values of the variables. Up to now, everything works like a charm… with one problem:

    The variables have different min and max values. For example var1 goes from 0 to 5, var2 from 0 to 400, var3 from 5 to 10, etc. In that example, the legend for the fill colour goes from 0 to 400. var2 is nicely drawn, but var1 and var3 are basically in the same colour.

    Is there a way I could use a different legend for each panel of the facet? Or is this simply not (yet) possible with facet_wrap or facet_grid in ggplot?

    I could make individual plots for each variable and join them with viewports, but there a plenty of variables and this would be a lot of work.

    Or is there maybe another package or method I could use to accomplish what I’d like to do?

    And help would be very much appreciated. :)

    Edit: With the help of the ggplot2-package description, I constructed an example that illustrates my problem:

    ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
    values <- data.frame(
    id = ids,
    val1 = cumsum(runif(6, max = 0.5)),
    val2 = cumsum(runif(6, max = 50))
    )
    positions <- data.frame(
    id = rep(ids, each = 4),
    x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
    0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
    y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
    2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
    )
    
    values <- melt(values)
    datapoly <- merge(values, positions, by=c("id"))
    
    p <- ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=value, group=id),colour="black")
    p <- p + facet_wrap(~ variable)
    p
    

    The panel on the right illustrates different values for var2 on the map. On the panel on the left however, all polygons have the same colour. This is logical, because only one colour gradient is used for all panels. Could I use a different colour gradient for each panel?

    解决方案

    Currently there can be only one scale per plot (for everything except x and y).

    这篇关于不同的图例和填充颜色为facegted ggplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆