不同的图例和填充颜色为facegted ggplot? [英] Different legends and fill colours for facetted ggplot?
问题描述
对不起,我的问题没有包含任何示例数据。我无法找到一种轻松生成示例形状文件的方法。希望有经验的 ggplot
用户可以从下面的描述中看到我想要做的事情。
ve得到了:
$ ul
$ a code> data.frame X关于示例的信息( plotid
, var1
, var2
, var3
, var4
,...)
多边形shapefile Y
与样本地块的空间信息
Y
(带有 maptools
)和 fortify
ing as data.frame
Z
( ggplot2
)可以正常工作。 melt
ing X
至 X_melted
merge
-ing Z
和 X_melted
至 mapdf
也适用。 这意味着现在我们有一个 现在我想绘制这样的数据框: 结果是每个变量都有一个面板的好图。面板的地图是相同的,但填充颜色随着变量的值而变化。到目前为止,一切都像一个魅力......有一个问题: 变量具有不同的最小值和最大值。例如 data.frame $ c $
, var2
, var3 $ c
和
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
很好地绘制,但 var3
基本上都是以相同的颜色。
有没有一种方法可以为每个面板使用不同的图例?或者在 或者是否有另一个包或方法可以用来完成我想要做的事情? 和帮助将非常感激。 :) 编辑: 右侧的面板在地图上显示了 目前每个绘图只能有一个刻度和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 I’ve got: A A polygon shapefile Importation of the shapefile That means that now we have a Now I want to plot this data frame like this: 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 Is there a way I could use a different legend for each panel of the facet? Or is this simply not (yet) possible with 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 The panel on the right illustrates different values for Currently there can be only one scale per plot (for everything except x and y). 这篇关于不同的图例和填充颜色为facegted ggplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 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
的不同值。然而,在左侧的面板上,所有多边形都具有相同的颜色。这是合乎逻辑的,因为所有面板只使用一种颜色渐变。我可以为每个面板使用不同的颜色渐变吗?ggplot
can see what I’d like to do from the description below.
data.frame
X with information about sample plots (plotid
, var1
, var2
, var3
, var4
, …)Y
with spatial information for the sample plotsY
(with maptools
) and fortify
ing as data.frame
Z
(ggplot2
) works fine. melt
ing X
to X_melted
works equally fine. merge
-ing Z
and X_melted
to mapdf
works as well.data.frame
in long form with spatial information and var1
, var2
, var3
, …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
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.facet_wrap
or facet_grid
in ggplot
?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
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?