我正试图将地图绘制限制在特定区域. 因为后者弄乱了多边形,所以coord_map 优先选择scale_x_continuous和y等价.但在这里我发现它因某种原因失败了.这是我的代码(下载一个57kb的shapefile):
require(maptools)
require(ggplot2)
download.file('https://dl.dropboxusercontent.com/u/46043231/UK.zip', "uk.zip", method="internal", mode="wb")
unzip('uk.zip')
uk = readShapePoly('uk_outline_1000m.shp')
print(bbox(uk))
min max
x 259.9625 655566.4
y 7211.7025 1218558.9
uk2 = fortify(uk)
(p = ggplot(uk2, aes(x=long, y=lat, group=group)) + geom_polygon() + coord_equal())
Run Code Online (Sandbox Code Playgroud)

但是当coord_map使用时,情节消失了:
p + coord_map(xlim=c(0, 630000), ylim=c(0, 1000000))
Run Code Online (Sandbox Code Playgroud)

知道发生了什么事吗?
我想通过ggplot2中的数据变量facet_wrap一个地图,例如下面例子中的'pets'.这是否需要完全复制每个变量类别的强化地图数据?那会让我觉得有点傻.有替代方法吗?
require(ggplot2)
(nz_dat = data.frame(island = rep(c('North.Island ','South.Island '), 3),
pets = c('cats','cats','dogs','dogs','birds','birds'),
n = c(13, 26, 48, 74, 24, 17)))
island pets n
1 North.Island cats 13
2 South.Island cats 26
3 North.Island dogs 48
4 South.Island dogs 74
5 North.Island birds 24
6 South.Island birds 17
nz = map_data("nz")
nz = subset(nz, nz$region %in% c('North.Island ','South.Island ')) # 2 main islands
# simple plot
ggplot(nz, aes(long, lat, group=group, fill=factor(region))) +
geom_polygon() + coord_quickmap()
Run Code Online (Sandbox Code Playgroud)
我想为每个方面添加自定义图像.使用annotation_custom复制所有方面的图像,例如:
require(ggplot2); require(grid); require(png); require(RCurl)
p = ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + facet_wrap(~Species)
img1 = readPNG(getURLContent('https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png'))
img2 = readPNG(getURLContent('https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png'))
img3 = readPNG(getURLContent('https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png'))
a1 = annotation_custom(rasterGrob(img1, interpolate=TRUE), xmin=7, xmax=8, ymin=3.75, ymax=4.5)
a2 = annotation_custom(rasterGrob(img2, interpolate=TRUE), xmin=7, xmax=8, ymin=3.75, ymax=4.5)
a3 = annotation_custom(rasterGrob(img3, interpolate=TRUE), xmin=7, xmax=8, ymin=3.75, ymax=4.5)
p + a1
Run Code Online (Sandbox Code Playgroud)
是否有另一种方法可以实现这一点,从而img1-3正确填充各个方面?
我试图绘制一个半透明的矩形,其颜色与密度曲线上方的背景颜色相同,因此它可以创建一个较浅色的垂直区域(一个黑客可以看到一系列的兴趣,如高峰时间).如你所见,alpha失败了.我想知道是否有人可以使用以下代码?
我知道在ggplot的alpha通道问题上有其他帖子(例如)但似乎没有解决这个问题,并且目前的情况不明确是版本明智的.
提前致谢 :)

d <- data.frame(rnorm(100, mean = 0, sd = 100)); names(d) <- 'data'
ggplot(d) + geom_density(aes(x=data),col=NA, fill='grey30') + opts(panel.background=NULL) +
geom_rect(aes(xmin=-30, xmax=30, ymin=0, ymax=0.005), fill='white',alpha=0.2)
ggplot(d) + geom_density(aes(x=data),col=NA, fill='grey30') + opts(panel.background=NULL) +
geom_rect(aes(xmin=-30, xmax=30, ymin=0, ymax=0.005), fill='#FFFFFF40')
Run Code Online (Sandbox Code Playgroud) 有谁知道如何在R中创建模拟栅格高程数据集 - 即实际高程值的2d矩阵?R jitter似乎不合适.在Java/Processing中,该noise()函数使用Perlin噪声算法实现了这一点,例如:
size(200, 200);
float ns = 0.03; // for scaling
for (float i=0; i<200; i++) {
for (float j=0; j<200; j++) {
stroke(noise(i*ns, j*ns) * 255);
point(i, j);
}
}
Run Code Online (Sandbox Code Playgroud)

但我发现R文献中没有提到Perlin噪音.提前致谢.
有谁知道它是否可以用ggplot映射到六边形大小?大小在geom_hex文档中列为参数,但stat_hexbin中没有大小映射的示例,因此这似乎与bin大小有关.
举个例子:
ggplot(economics, aes(x=uempmed, y=unemploy)) + geom_hex()
Run Code Online (Sandbox Code Playgroud)

但是查看人口分布(如下),将分箱平均人口映射到六边形大小可能是有用的,但我没有找到这个的公式(如果存在).
ggplot(economics, aes(x=uempmed, y=unemploy, col=pop)) + geom_point()
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?
我似乎无法复制添加线性abline到log-log ggplot.下面的代码说明.感谢我出错的想法.
d = data.frame(x = 100*sort(rlnorm(100)), y = 100*sort(rlnorm(100)))
(fit = lm(d$y ~ d$x))
# linear plot to check fit
ggplot(d, aes(x, y)) + geom_point() + geom_abline(intercept = coef(fit)[1], slope = coef(fit)[2], col='red')
# log-log base plot to replicate in ggplot (don't worry if fit line looks a bit off)
plot(d$x, d$y, log='xy')
abline(fit, col='red', untf=TRUE)
# log-log ggplot
ggplot(d, aes(x, y)) + geom_point() +
geom_abline(intercept = coef(fit)[1], slope = coef(fit)[2], col='red') +
scale_y_log10() + scale_x_log10()
Run Code Online (Sandbox Code Playgroud) 基于 ggplot2 的密封示例,我尝试更改箭头的粗细,以便它们的整体大小更好地反映数据变量。我可以指定长度和厚度,但不知道如何更改箭头的大小。非常感谢任何建议。
require(ggplot2)
require(grid)
d = seals[sample(1:nrow(seals), 100),]
d$size = sqrt(sqrt(d$delta_long^2 + d$delta_lat^2))
ggplot(d, aes(x = long, y = lat, size = size)) +
geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat), arrow = arrow(length = unit(0.1,"cm")))
Run Code Online (Sandbox Code Playgroud)

编辑
解决方案代码:
ggplot(d, aes(x = long, y = lat, size = size)) +
geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat),
arrow = arrow(length = unit(d$size/3, "cm"), type='closed')) +
scale_size(range = c(0, 2))
Run Code Online (Sandbox Code Playgroud) 我发现了一个令人惊讶的行为apply,我想知道是否有人可以解释.让我们采取一个简单的矩阵:
> (m = matrix(1:8,ncol=4))
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
Run Code Online (Sandbox Code Playgroud)
我们可以垂直翻转它:
> apply(m, MARGIN=2, rev)
[,1] [,2] [,3] [,4]
[1,] 2 4 6 8
[2,] 1 3 5 7
Run Code Online (Sandbox Code Playgroud)
这将rev()迭代地将矢量反转函数应用于每列.但是当我们尝试逐行应用时,我们得到:
> apply(m, MARGIN=1, rev)
[,1] [,2]
[1,] 7 8
[2,] 5 6
[3,] 3 4
[4,] 1 2
Run Code Online (Sandbox Code Playgroud)
..逆时针旋转90度!Apply使用相同的结果,FUN=function(v) {v[length(v):1]}因此绝对不是转速的错误.
对此有何解释?
我希望ggplot以特定的顺序绘制,以控制对象重叠时可见的内容.每个数据行映射到2个几何图层的合成 - 图的细节需要这样.我一直在使用循环来做到这一点,但它很慢.我想知道是否有更好的方法?例如
d = data.frame(x=c(1,5,2,2,4,2), y=c(1,1,4,3,3,5), grp=c(1,1,1,2,2,2))
ggplot(d, aes(x, y, group=grp)) +
geom_polygon(aes(fill = factor(grp))) +
geom_line(size=1)
Run Code Online (Sandbox Code Playgroud)
每个多边形线应与其多边形一起绘制 - 例如,红色多边形的线应该被蓝色多边形遮挡.有没有办法在没有循环的情况下实现这一点geom_polygon并geom_line使用相同的数据集?
编辑:循环方法..
这是我用过的循环方法.添加了更好的数据集来比较性能.两者都需要大约5.6秒才能在我的机器上运行.相比之下,典型的方法(ggplot(d, aes(x, y, fill=factor(grp))) + geom_polygon() + geom_line(size=1))需要0.45秒.
d = data.frame(x = sample(-30:30,99,rep=T) + rep(sample(1:100,33),each=3),
y = sample(-30:30,99,rep=T) + rep(sample(1:100,33),each=3),
grp = rep(1:33,each=3))
# Method 1 - for loop
p = ggplot()
for(g in unique(d$grp)){
dat = subset(d, grp == g)
p = p + geom_polygon(data=dat, aes(x, y, fill = factor(grp))) …Run Code Online (Sandbox Code Playgroud)