多面ggplot的不同图例和填充颜色? [英] Different legends and fill colours for facetted ggplot?
问题描述
抱歉,我的问题没有包含任何示例数据.我找不到轻松生成示例形状文件的方法.希望ggplot
有经验的用户可以从下面的描述中看到我想要做什么.
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.
我有:
A
data.frame
X 包含有关示例图的信息(plotid
、var1
、var2
,var3
,var4
, ...)
A
data.frame
X with information about sample plots (plotid
,var1
,var2
,var3
,var4
, …)
多边形 shapefile Y
包含样本图的空间信息
A polygon shapefile Y
with spatial information for the sample plots
导入shapefile Y
(使用maptools
)和fortify
作为data.frame
Z
(ggplot2
) 工作正常.melt
将 X
转换为 X_melted
工作得同样好.merge
-ing Z
和 X_melted
到 mapf
也能工作.
Importation of the shapefile Y
(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.
这意味着现在我们有一个带有空间信息和var1
、var2
、var3<的长格式的
data.frame
/code>, ...
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:
变量具有不同的最小值和最大值.例如 var1
从 0
到 5
,var2
从 0
到 >400
、var3
从 5
到 10
等等.在那个例子中,填充颜色的图例来自 0
到 400
.var2
画得很好,但 var1
和 var3
的颜色基本相同.
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.
有没有办法可以为刻面的每个面板使用不同的图例?或者,ggplot
中的 facet_wrap
或 facet_grid
根本(还)不可能实现?
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. :)
在 ggplot2
包描述的帮助下,我构建了一个示例来说明我的问题:
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
右侧的面板说明了地图上 var2
的不同值.然而,在左侧面板上,所有多边形都具有相同的颜色.这是合乎逻辑的,因为所有面板只使用一种颜色渐变.我可以为每个面板使用不同的颜色渐变吗?
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?
推荐答案
目前每个地块只能有一个比例尺(除了 x 和 y 之外的所有东西).
Currently there can be only one scale per plot (for everything except x and y).
这篇关于多面ggplot的不同图例和填充颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!