之前在旧线程中已经问过这个问题,但是接受的答案目前在 ggplot2 的当前版本中不再有效。这是一个最小的例子:
library(ggplot2)
library(rnaturalearth)
world = ne_countries(scale = "medium", returnclass = "sf")
ggplot(world) +
geom_sf(aes(fill = pop_est)) +
scale_fill_viridis_c(option = "plasma", trans = "sqrt")
Run Code Online (Sandbox Code Playgroud)
我的问题是:我怎样才能摆脱每个国家的边界?
R 中的包fst提供了极快的读写速度,并且需要更少的硬盘空间。请在此处查看 R 中的基准测试结果。
我想知道是否可以在 Python 中读取 .fst 文件。我还没有找到在 Python 中做到这一点的简单方法。(编辑:我的意思是导入一个 fst 文件,而不是 fst 包,到 python 中。这就像将 R 写出的 csv 文件导入到 python 中一样)。
谢谢你!
我正在尝试用rmarkdown和knitr包写一篇论文。当我在 rmarkdown 文件的 yaml 标头中设置参考书目样式时,我有点想知道我们手头有哪些 rmarkdown 选择。
bookdown: Authoring Books and Technical Documents with R Markdown中的例子给出的默认参数是“apalike”,这显然是APA风格。
我在问我们对这个书目风格的参数有什么选择?例如,如果我想使用芝加哥风格,我应该设置什么参数为 biblio-style?
我用谷歌搜索了这个,但似乎没有关于如何设置这个参数的官方文档被发布。谢谢。
我试图使用 生成一个“令人满意的”图例ggplot2,但令我沮丧的是刻度线的位置(它们位于图例键内)。这是一些代码来说明我的意思。
library(ggplot2)
ggplot(mtcars, aes(x = cyl, y = gear, fill = mpg)) +
geom_tile() +
coord_equal() +
theme(legend.position = 'top')
Run Code Online (Sandbox Code Playgroud)
有没有办法将刻度线放在图例钥匙框之外?就像这个传说一样(看起来他们是用 这样做的ggplot2):
任何建议将不胜感激!
我已经搜寻了这个答案几个小时。很多人都问过类似的问题,但是我没有找到一个足够简单的问题或一个简单的答案。这是我的方法:
假设我想在中进行一个简单的分组data.table:
library(data.table)
mtcars = data.table(mtcars)
mtcars[,sum(mpg), gear]
# Here are the results
# gear V1
#1: 4 294.4
#2: 3 241.6
#3: 5 106.9
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用自定义函数执行此操作:
zz = function(data, var, group){
return(data[,sum(var), group])
}
zz(mtcars, mpg, gear)
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
eval(bysub,parent.frame(),parent.frame())中的错误:找不到对象“齿轮”
我试过substitute,eval,quote,和其他解决方案,但没有人的作品。我想知道是否有人可以对此提供更直接的解决方案和解释。
谢谢,万圣节快乐!
我想用来...指示要从对象的自定义函数返回的变量data.table。这是一个最小的可复制示例:
library(data.table)
d = data.table(mtcars)
getvar = function(...){
return(d[,.(xyz = mean(hp), ...), cyl])
}
getvar(mpg, cyl, disp)
Run Code Online (Sandbox Code Playgroud)
在错误
[.data.table(d,(N = .N,...),CYL):对象'CYL'未找到
我希望得到的是:
d[,.(xyz = mean(hp), mpg, cyl, disp), cyl]
# cyl xyz mpg cyl disp
# 1: 6 122.28571 21.0 6 160.0
# 2: 6 122.28571 21.0 6 160.0
# 3: 6 122.28571 21.4 6 258.0
# 4: 6 122.28571 18.1 6 225.0
# 5: 6 122.28571 19.2 6 167.6
Run Code Online (Sandbox Code Playgroud)
任何人都可以分享他们的解决方案吗?
我注意到您可以class: center, middle在 xaringan 幻灯片中使用幻灯片的布局居中和居中,但这会改变整个幻灯片的标题和布局。您还可以通过以下方式将图像居中:
.center[

]
Run Code Online (Sandbox Code Playgroud)
但我想知道我是否可以在不改变 xaringan 中整个页面的布局的情况下将绘图居中并使其位于幻灯片的中间?
谢谢!
我正在尝试为 R 中的多个点创建平滑地图,但我在这里没有找到完美的解决方案。
library(mapchina)
library(sf)
library(dplyr)
library(ggplot2)
# Create some sample data
sf_beijing = china %>%
filter(Code_Province == '11') %>%
st_transform(4326)
sf_points = data.frame(
lat = c(39.523, 39.623, 40.032, 40.002, 39.933, 39.943, 40.126, 40.548),
lon = c(116.322, 116, 116.422, 116.402, 116.412, 116.408, 116.592, 116.565)
) %>%
st_as_sf(coords = c("lon", "lat"), crs = 4326)
# Plot the boundary for Beijing and the points
ggplot() +
geom_sf(data = sf_beijing, fill = NA) +
geom_sf(data = sf_points, color = 'red') +
theme_test()
Run Code Online (Sandbox Code Playgroud)
此外,我发现 …
当我尝试将点图层添加到geom_sf()图层和投影时,这些点似乎最终出现在德克萨斯州南部的一个位置。下面是重现此问题的最小示例。
library(sf)
library(ggplot2)
# devtools::install_github("hrbrmstr/albersusa")
library(albersusa)
crs_use = "+proj=laea +lat_0=30 +lon_0=-95"
d_points = data.frame(long = c(-110, -103, -84),
lat = c(45, 40, 41))
A = ggplot(data = usa_sf()) +
geom_sf() +
geom_point(data = d_points,
aes(x = long, y = lat),
color = "red", size = 5) +
theme_minimal() +
ggtitle("(A) right point position, wrong projection")
B = ggplot(data = usa_sf()) +
geom_sf() +
geom_point(data = d_points,
aes(x = long, y = lat),
color = "red", size = 5) …Run Code Online (Sandbox Code Playgroud) r ×7
ggplot2 ×4
r-sf ×3
data.table ×2
function ×2
bibliography ×1
css ×1
ellipsis ×1
fst ×1
geospatial ×1
knitr ×1
legend ×1
python ×1
r-markdown ×1
smoothing ×1
xaringan ×1