我需要将绘图对象存储在变量中。我知道我可以做到:
plot(rnorm(10))
obj = recordPlot()
replayPlot(obj)
Run Code Online (Sandbox Code Playgroud)
但是我不想显示图形窗口。因此,我正在尝试执行此操作,但直到现在都没有成功。
win.metafile()
plot(rnorm(10))
obj = recordPlot()
dev.off()
replayPlot(obj) # it shows a null plot
Run Code Online (Sandbox Code Playgroud)
好吧,可能是因为我在做obj = recordPlot()图时还没有准备好。
G Elliot Moris 使用动态分布图显示了政治两极分化.

从这个问题:如何使用'facet'在GGPLOT中创建多个密度图我设法使用facet使用虚拟数据重现类似的图:
library(ggplot2)
set.seed(101)
dtf <- data.frame(variable = c(rnorm(1000),
rnorm(1000) + rep(1:10/2,each =100)),
group = rep(c("a","b"), each = 1000),
year = rep(2001:2010, each=100))
ggplot(dtf) +
geom_density(aes(x = variable, fill = group)) +
facet_grid(year ~.)
Run Code Online (Sandbox Code Playgroud)
但我希望这些分布在原始情节中重叠.这些图是否有特定的名称,是否可以用R复制它们?
[编辑] 此全局温度分布图中提供了类似图的动态版本.
我在 Twitter 上被问到这个问题,我认为把它放在这里可能会很好。
使用 制作带标签的并排图时plot_grid(),对于单字母标签,事情按预期工作:
library(cowplot)
p1 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_density(alpha = 0.7) +
ggtitle("") + theme_minimal()
p2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_density(alpha = 0.7) +
ggtitle("") +
scale_fill_grey() + theme_minimal()
plot_grid(p1, p2, labels = c("A", "B"))
Run Code Online (Sandbox Code Playgroud)
但是,如果我们使用较长的字符串作为标签,标签会向右移动,并且它们移动得越多,字符串越长:
plot_grid(p1, p2, labels = c("Density plot in color", "In gray"))
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
免责声明:我是包的作者。在这里发布这个答案,希望它会有用。
我非常感谢R的帮助。我想绘制一个网格数据的数据框(例如沿着对角线从左上方到右下方)。我已经看过很多使用ggplot2的示例,但是,我只是缺乏R操纵数据结构所必需的经验。我已经使用LISP和Java编程多年了,但我的头无法绕过R :-(
数据如下所示:
tension cluster migraineNoAura migraineAura
tension NA 1.5 6.960453e+00 3.596953
cluster 1.943113e+08 NA NA NA
migraineNoAura 8.462798e+00 NA NA 7.499999
migraineAura 2.833333e+00 NA 7.148313e+07 NA
Run Code Online (Sandbox Code Playgroud)
这只是一个很小的子集,它是60x60的数据帧。注意NAs。
我希望有一个60x60的网格,用值着色,并使用数据框中的名称标记x和y。
我即将通过下面的跨越 textgrob 图形图例实现这个多面板图。但是我继续在人物和人物图例之间获得意想不到的空间。在下面的 reprex 中尝试。
# Library calls
library(tidyverse)
library(grid)
library(gridtext)
library(ggtext)
library(patchwork)
# make dummy figures
d1 <- runif(500)
d2 <- rep(c("Treatment", "Control"), each=250)
d3 <- rbeta(500, shape1=100, shape2=3)
d4 <- d3 + rnorm(500, mean=0, sd=0.1)
plotData <- data.frame(d1, d2, d3, d4)
str(plotData)
#> 'data.frame': 500 obs. of 4 variables:
#> $ d1: num 0.0177 0.2228 0.5643 0.4036 0.329 ...
#> $ d2: Factor w/ 2 levels "Control","Treatment": 2 2 2 2 2 2 2 2 2 2 ... …Run Code Online (Sandbox Code Playgroud) 我有一个名为的对象defaultPacks,包含我使用的所有计算机上安装的软件包的名称.很多缩写:
defaultPacks <- c(
"AER",
"plyr",
"dplyr"
)
Run Code Online (Sandbox Code Playgroud)
我想将此对象保存到共享目录中的文件,所有这些目录都可以到达.我正在使用Dropbox,当R运行时,同步始终暂停.
save(defaultPacks,
file.path("C:","Users","andrewH","Dropbox","R_PROJ","sharedSettings.rdata"))
Run Code Online (Sandbox Code Playgroud)
然后我想加载对象并安装其名称在对象中的包defaultPacks.
SyncPacks <- function(fileString){
defaultPacks <- load(file=fileString)
install.packages(defaultPacks, repos="http://cran.us.r-project.org")
}
SyncPacks(file.path("C:","Users","andrewH","Dropbox","R_PROJ","sharedSettings.rdata")
Run Code Online (Sandbox Code Playgroud)
如果我这样做,我会收到警告:
Warning in install.packages: package ‘defaultPacks’ is not available (for R version 3.2.1)
Run Code Online (Sandbox Code Playgroud)
我defaultPacks加载并分配后立即查看内容:字符串"defaultPacks".所以它似乎只是一个字符串而不是一个对象.
所以我回到我的保存,然后尝试
save(get(defaultPacks), file.path(etc.))
Run Code Online (Sandbox Code Playgroud)
这给了我一个不同的错误:
Error in save(get("defaultPacks"), file = file.path("C:", "Users", "andrewH", :
object ‘get("defaultPacks")’ not found.
Run Code Online (Sandbox Code Playgroud)
然后我试着dynGet()用同样的结果.
因此,在将符号视为字符串之前,现在它将函数视为字符串.
所以我尝试使用list选项进行保存:
save(list = defaultPacks, file = file.path(etc))
Run Code Online (Sandbox Code Playgroud)
并得到另一个错误:
Error in save(list = defaultPacks, file = …Run Code Online (Sandbox Code Playgroud) 在研究此答案时,我尝试geom_image()从ggimage包中绘制图像带,但无法使其正常工作。geom_image()修改图像的宽高比,但我不知道如何阻止它(或者是否有可能)。我也不清楚用什么单位size来衡量。从代码的行为方式来看,也许它在npc从0到1的坐标范围内,而不管ggplot2坐标系如何?
这是我使用的代码:
require(ggimage)
df_img <- data.frame(phase = c("Interphase", "Prophase", "Metaphase", "Anaphase", "Telophase"),
image = c("http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_interphase.jpg",
"http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_prophase.jpg",
"http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_metaphase2.jpg",
"http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_anaphase2.jpg",
"http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_telophase.jpg"))
df_img$phase <- factor(df_img$phase, levels=df_img$phase)
ggplot(df_img, aes(x = phase, y = 0, image = image)) + geom_image(size = 0.18)
Run Code Online (Sandbox Code Playgroud)
这是结果图像:
这是图像的外观:
注意:这是一个专门关于ggimage行为的问题。我知道如何使用其他方法来生成正确的图像,例如通过使用draw_image()Cowplot。
我正在尝试使用cowplot包的draw_image()功能。我已经设法在图表中获取图像作为示例。
我无法弄清楚 xy 位置是如何工作的,我必须不断输入随机数,直到看到图像。
require(ggplot2) #required packages
require(cowplot)
require(magick)
p1 <- qplot(Sepal.Length, Sepal.Width, data = iris)
ggdraw(p1) +
draw_image(
"https://upload.wikimedia.org/wikipedia/en/7/77/EricCartman.png",
y = 0.2,
scale = 0.5
)
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议他们的运作规模如何?它看起来确实与图表的比例不同。
I am using the Tigris package to download shape files with the code,
options(tigris_class = "SF")
Run Code Online (Sandbox Code Playgroud)
While I can easily map polygons
ggplot(data=zctas) +
geom_sf(aes())
Run Code Online (Sandbox Code Playgroud)
I am struggling to create a marker/point instead of a filled polygon (for example, a shape -- like a circle inside a zctas) as the shape file I've pulled down from tigris does not have a lat/long for the x and y AES mapping. The shape file has a "geometry" column, and I am wondering …
我需要通过 ggplot2 调整热图的中点位置。我用谷歌搜索了一下,发现scale_fill_gradient2很合身,但颜色似乎与我要找的不匹配。我知道 z 需要从 0 到 1 的范围。下面的示例数据集生成:
library(ggplot2)
library(tibble)
library(RColorBrewer)
set.seed(5)
df <- as_tibble(expand.grid(x = -5:5, y = 0:5, z = NA))
df$z <- runif(length(df$z), min = 0, max = 1)
Run Code Online (Sandbox Code Playgroud)
我尝试用 绘图,scale_fill_gradient2但蓝色并没有像我想要的那样“暗”。
ggplot(df, aes(x = x, y = y)) +
geom_tile(aes(fill = z)) +
scale_fill_gradient2(
low = 'red', mid = 'white', high = 'blue',
midpoint = 0.7, guide = 'colourbar', aesthetics = 'fill'
) +
scale_x_continuous(expand = c(0, 0), breaks = unique(df$x)) + …Run Code Online (Sandbox Code Playgroud)