我正在尝试缓冲半径为100km的数据集中的点.我正在使用gBuffer
包中的功能rgeos
.这是我到目前为止所拥有的:
head( sampledf )
# postalcode lat lon city province
#1 A0A0A0 47.05564 -53.20198 Gander NL
#4 A0A1C0 47.31741 -52.81218 St. John's NL
coordinates( sampledf ) <- c( "lon", "lat" )
proj4string( sampledf ) <- CRS( "+proj=longlat +datum=WGS84" )
distInMeters <- 1000
pc100km <- gBuffer( sampledf, width=100*distInMeters, byid=TRUE )
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:
在gBuffer中(sampledf,width = 100*distInMeters,byid = TRUE):不投影空间对象; GEOS期望平面坐标
根据我的理解/阅读,我需要将数据集的坐标参照系(CRS),特别是投影从"地理"更改为"预测".我不确定如何改变它.这些都是加拿大的地址,我可以补充一下.所以NAD83在我看来是一个自然的选择,但我可能错了.
任何/所有帮助将不胜感激.
我想在使用facet_wrap()
和刻面使用两个变量并且两个都可以自由缩放时删除带状标签的冗余。
例如,facet_wrap
以下图形的此版本
library(ggplot2)
dt <- txhousing[txhousing$year %in% 2000:2002 & txhousing$month %in% 1:3,]
ggplot(dt, aes(median, sales)) +
geom_point() +
facet_wrap(c("year", "month"),
labeller = "label_both",
scales = "free")
Run Code Online (Sandbox Code Playgroud)
应该具有此facet_grid
版本的外观,其中带状标签位于图形的顶部和右侧(也可以是底部和左侧)。
ggplot(dt, aes(median, sales)) +
geom_point() +
facet_grid(c("year", "month"),
labeller = "label_both",
scales = "free")
Run Code Online (Sandbox Code Playgroud)
不幸的是,使用facet_grid
不是一种选择,因为据我所知,它不允许秤“完全免费” -参见此处或此处
我考虑过的一种尝试是制作单独的地块,然后将它们组合起来:
library(cowplot)
theme_set(theme_gray())
p1 <- ggplot(dt[dt$year == 2000,], aes(median, sales)) +
geom_point() +
facet_wrap("month", scales = "free") +
labs(y = "2000") +
theme(axis.title.x = element_blank())
p2 …
Run Code Online (Sandbox Code Playgroud) 在这个拼凑而成的小插图中解释了如何组合多个 ggplots。我遇到的一个困难是收集图例并在它们的标题在字符数上非常不同时正确对齐/对齐它们。
下面是一个示例 - 我希望“mpg”图例也左对齐/对齐,而不是在“大小”图例下方居中。有什么建议?请注意,添加theme(legend.justification = "left")
并不能解决问题。
library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp, colour = mpg, size = wt)) +
guides(size = guide_legend(title = "Size - long title for the purpose of this example")) +
ggtitle('Plot 1')
p2 <- ggplot(mtcars) +
geom_boxplot(aes(gear, disp, group = gear)) +
ggtitle('Plot 2')
p3 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle('Plot 3')
(p1 | (p2 / p3)) + plot_layout(guides = 'collect')
Run Code Online (Sandbox Code Playgroud)
我不确定如何或是否可以以休闲方式调整关键图例。考虑这个例子:
library(tmap)
data(Europe)
my_map <-
tm_shape(Europe) +
tm_polygons("well_being", textNA="Non-European countries", title="Well-Being Index") +
tm_text("iso_a3", size="AREA", root=5) +
tm_layout(legend.position = c("RIGHT","TOP"),
legend.frame = TRUE)
Run Code Online (Sandbox Code Playgroud)
我尝试legend.format = list(scientific = TRUE)
过类似的东西:
my_map2 <- my_map +
tm_layout(legend.format = list(scientific = TRUE))
Run Code Online (Sandbox Code Playgroud)
这给了一个传说:
但是,我希望的是:
在我的数据中,4 是一个零,我被要求让它单独作为零。
我正在尝试自定义一个包含刻面的 ggplot2 图,并且想要更改刻面条的颜色以及字体的颜色。我找到了一些更改颜色的代码strip.background
,但无法修改它以更改字体颜色......知道吗?
到目前为止我得到了什么:
library(ggplot2)
library(grid)
p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl) +
ggtitle("How to change coloour of font in facet strip?")
g <- ggplot_gtable(ggplot_build(p))
strip_both <- which(grepl('strip-', g$layout$name))
fills <- c("red","green","blue","yellow","red","green","blue","yellow")
k <- 1
for (i in strip_both) {
j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
k <- k+1
}
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.2.1)于 2018 年 11 月 23 日创建
我有这个数据框:
> str(DF)
'data.frame': 14084 obs. of 6 variables:
.
.
.
$ Variables: chr "Height" "Height" "Height" "Height" ...
$ Values : num 245 129 301 162 123 125 115 47 46 135 ...
$ Year : Factor w/ 2 levels "2015","2016": 1 1 1 1 1 1 1 1 1 1 ...
Run Code Online (Sandbox Code Playgroud)
我使用facet_wrap()
自由轴刻度将图分成两个小平面(分为两列)。
ggplot(data = DF, aes(x = Year, y = Values)) +
geom_boxplot() +
facet_wrap("Variables", scales = "free")
Run Code Online (Sandbox Code Playgroud)
我的问题是:
两个构面共享一个共同的y轴标题。但是,我想为两个方面提供两个单独的y轴标题。常见的x轴标题对我来说很好。
我碰到了这个问题, 使用带有facet_wrap的ggplot2显示不同的轴标签, 但是它并不能解决我想要的问题,因为我不想失去顶部的切面标签。而且,我的构面是水平排列的。 …