很抱歉没有包含我的问题的任何示例数据。我找不到轻松生成示例形状文件的方法。希望有经验的ggplot
用户可以从下面的说明中看到我想要做的事情。
我有:
data.frame
X,其中包含有关样本图的信息(plotid
,var1
,var2
,var3
,var4
,... )
包含样本图空间信息的多边形shapefile Y
将shapefile Y
(maptools
}和fortify
输入data.frame
Z
(ggplot2
)的方式正常运行。 melt
X
X_melted
merge
的工作同样正常。 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),colour="black")
pl1 <- pl1 + facet_grid(variable ~ .)
pl1 <- pl1 + coord_equal(ratio = 1)
pl1
,...
现在我想绘制这样的数据框:
var1
结果是一个很好的情节,每个变量有一个面板。面板的地图是相同的,但填充颜色随变量的值而变化。到目前为止,一切都像魅力......有一个问题:
变量具有不同的最小值和最大值。例如,0
从5
转移到var2
,0
从400
转移到var3
,5
从10
转移到0
至400
等。在该示例中,填充颜色的图例从var2
变为var1
。 var3
很精美,但facet_wrap
和facet_grid
基本上颜色相同。
有没有办法可以为刻面的每个面板使用不同的图例?或者ggplot
中的ggplot2
或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
- 包描述的帮助下,我构建了一个示例来说明我的问题:
{{1}}
右侧的面板说明了地图上{{1}}的不同值。但是,在左侧面板上,所有多边形都具有相同的颜色。这是合乎逻辑的,因为所有面板只使用一个颜色渐变。我可以为每个面板使用不同的颜色渐变吗?
答案 0 :(得分:14)
目前每个地块只能有一个比例尺(除了x和y之外的所有内容)。
答案 1 :(得分:4)
有网格善良
align.plots <- function(..., vertical=TRUE){
#http://ggextra.googlecode.com/svn/trunk/R/align.r
dots <- list(...)
dots <- lapply(dots, ggplotGrob)
ytitles <- lapply(dots, function(.g) editGrob(getGrob(.g,"axis.title.y.text",grep=TRUE), vp=NULL))
ylabels <- lapply(dots, function(.g) editGrob(getGrob(.g,"axis.text.y.text",grep=TRUE), vp=NULL))
legends <- lapply(dots, function(.g) if(!is.null(.g$children$legends))
editGrob(.g$children$legends, vp=NULL) else ggplot2:::.zeroGrob)
gl <- grid.layout(nrow=length(dots))
vp <- viewport(layout=gl)
pushViewport(vp)
widths.left <- mapply(`+`, e1=lapply(ytitles, grobWidth),
e2= lapply(ylabels, grobWidth), SIMPLIFY=F)
widths.right <- lapply(legends, function(g) grobWidth(g) + if(is.zero(g)) unit(0, "lines") else unit(0.5, "lines")) # safe margin recently added to ggplot2
widths.left.max <- max(do.call(unit.c, widths.left))
widths.right.max <- max(do.call(unit.c, widths.right))
for(ii in seq_along(dots)){
pushViewport(viewport(layout.pos.row=ii))
pushViewport(viewport(x=unit(0, "npc") + widths.left.max - widths.left[[ii]],
width=unit(1, "npc") - widths.left.max + widths.left[[ii]] -
widths.right.max + widths.right[[ii]],
just="left"))
grid.draw(dots[[ii]])
upViewport(2)
}
}
p <- ggplot(datapoly[datapoly$variable=="val1",], aes(x=x, y=y)) + geom_polygon(aes(fill=value, group=id),colour="black")
p1 <- ggplot(datapoly[datapoly$variable=="val2",], aes(x=x, y=y)) + geom_polygon(aes(fill=value, group=id),colour="black")
align.plots( p,p1)
答案 2 :(得分:2)
冒着明显的风险,似乎你应该按百分比而不是原始值着色。然后你的变换值和你的传奇从0变为1.
答案 3 :(得分:2)
10 多年后重新审视这个问题,出色的 ggnewscale
包解决了具有多个色阶的问题。需要注意的是,您的构面数据需要两个单独的层,因此您必须将其分解一下。将新比例添加到绘图中的顺序很重要,因此我建议使用“图层 - 比例 - new_scale - 图层 - 比例”的顺序。后续的新比例应该重复“new_scale - layer - scale”模式。
library(ggplot2)
library(ggnewscale)
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 <- reshape2::melt(values)
#> Using id as id variables
datapoly <- merge(values, positions, by=c("id"))
ggplot(datapoly, aes(x=x, y=y)) +
geom_polygon(aes(fill=value, group=id),
data = ~ subset(., variable == "val1"),
colour="black") +
scale_fill_distiller(palette = "Reds") +
new_scale_fill() +
geom_polygon(aes(fill=value, group=id),
data = ~ subset(., variable == "val2"),
colour="black") +
scale_fill_distiller(palette = "Greens") +
facet_wrap(~ variable)
由 reprex package (v1.0.0) 于 2021 年 2 月 12 日创建
答案 4 :(得分:1)
也许有点非正统,但你可以尝试分析你的“价值”。例如:
p <- ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=factor(value), group=id),colour="black")
p <- p + facet_wrap(~ variable)
p
ggplot2使用因子来创建图例。因此,如果您可以添加一个带有“value”的列并将其分解为因子范围,则可以将“value”替换为范围。
创建一个列,例如“f”:
id variable value x y f
1 1.1 val1 0.09838607 2.0 -0.5 0.09-0.13
2 1.1 val1 0.09838607 1.0 0.0 0.09-0.13
3 1.1 val1 0.09838607 1.1 1.0 0.09-0.13
4 1.1 val1 0.09838607 2.2 0.5 0.09-0.13
25 2.1 val1 0.13121347 1.0 0.0 0.13-0.20
...
然后使用:
p <- ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=f, group=id),colour="black")
p <- p + facet_wrap(~ variable)
p
您必须指定所需的类别,这可能非常耗时。但至少图表会出现你想要的方式。基本上,您将数据重新编码到另一列。以下是一些例子: