我有一个关于 Cowplot 中的情节对齐的问题。
我想在由 3 列组成的面板中对齐图 (ggplot)。第一列有 2 个图(垂直对齐),第二列有 1 个图,第三列也有两个图(垂直对齐)。
例子:
# Packages
library(tidyverse)
library(cowplot)
# Create sample data
df <- data.frame(replicate(2,sample(0:10,10,rep=TRUE)))
# Create sample plots
plot.a1 <- ggplot(df, aes(x=df$X1, y=df$X2)) +
geom_point()
plot.a2 <- plot.a1
plot.a <- plot_grid(plot.a1, plot.a2, align = "v", ncol = 1, nrow = 2)
plot.b <- plot.a1
plot.c1 <- plot.a1
plot.c2 <- plot.a1
plot.c <- plot_grid(plot.c1, plot.c2, align = "v", ncol = 1, nrow = 2)
# Create panel figure
plot_grid(plot.a, plot.b, plot.c, labels …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个散点图,它使用图形上每个点的数字,而不是黑点。我可以对 9 及以下的数字执行此操作,但任何 10 及以上的数字在结果图上均由 1 表示。这些数字代表个体受试者,我有 13 个,每个个体都有一些数据点。
我正在plot为此使用该函数,因为我已经弄清楚如何用它来做我需要的所有其他事情,所以我不愿意更改为其他绘图类型/命令。
示例代码:
plot(dif ~ mean, pch=as.character(Subject.Number), data = Cov5.1)
但pch=as.character仅适用于个位数。我在这里看到了讨论使用文本或标签的解决方案,但对于我无法让其中任何一个起作用的任何解决方案。
这是之前的一个问题,但答案对我不起作用: R- plot numbers 而不是点
如何创建使用数字而不是点的散点图?
我正在尝试在 R 中的一条线上应用三种颜色(即绿色、黑色、红色)的颜色渐变。
示例代码
# Create sample dataframe
df <- data.frame(x = seq(0, 15 , 0.001),
y = seq(0, 15, 0.001))
# Plot data
ggplot(df, aes(x=x, y=y, col = y)) +
geom_line() +
scale_color_gradientn(colours = c("green", "black", "red"),
values = rescale(x = c(0, 2, 4), to = c(0,1), from = c(0, 15) ))
Run Code Online (Sandbox Code Playgroud)
问题
使用该scale_color_gradientn()函数,我可以在线上应用颜色渐变。我想使用绿色表示 0 到 1.5 之间的值,黑色表示 1.5-2.5 之间的值,红色表示 2.5 及以上,但我似乎无法掌握该rescale()功能。在示例中,颜色在 y 值 4 处停止。
如何以所需的方式应用颜色渐变?
我有一个数据帧列表,我想从中删除在特定列中具有特定值的数据帧。
代码
library(tidyverse)
combi <- function(a=c(1,2,3), b=c(2,3,4,5)){
return(expand.grid(a,b))
}
df <- pmap(list(a=c(1,2,3)), combi)
Run Code Online (Sandbox Code Playgroud)
输出
df
[[1]]
Var1 Var2
1 1 2
2 1 3
3 1 4
4 1 5
[[2]]
Var1 Var2
1 2 2
2 2 3
3 2 4
4 2 5
[[3]]
Var1 Var2
1 3 2
2 3 3
3 3 4
4 3 5
Run Code Online (Sandbox Code Playgroud)
假设我想删除一个数据框,如果Var1==2。
您在 Tidyverse 中如何解决这个问题?
我的文件夹中有几个.txt文件,它们的名称如下:
file1.txt
file2.txt
file2.txt_newfile.txt
file3.txt
file4.txt
file4.txt_newfile.txt
file5.txt_newfile.txt
...
Run Code Online (Sandbox Code Playgroud)
_newfile.txt我正在尝试从文件名中删除。如果该文件存在,它应该被新文件覆盖(例如file2.txt将被替换,但file5.txt只会被重命名)。
预期输出:
file1.txt
file2.txt # this was file2.txt_newfile.txt
file3.txt
file4.txt # file4.txt_newfile.txt
file5.txt #file5.txt_newfile.txt
...
Run Code Online (Sandbox Code Playgroud)
我尝试了以下代码:
for i in $(find . -name "*_newfile.txt" -print); do
mv -f "$file" "${file%????????????}"
done
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
mv: rename to : No such file or directory
Run Code Online (Sandbox Code Playgroud)
我做错了什么以及如何重命名这些文件?
我正在尝试在 R 中绘制由 ggplot 生成的大型热图。最终,我想使用 Illustrator 来“润色”此热图。
示例代码:
# Load packages (tidyverse)
library(tidyverse)
# Create dataframe
df <- expand.grid(x = seq(1,100000), y = seq(1,100000))
# add variable: performance
set.seed(123)
df$z <- rnorm(nrow(df))
ggplot(data = df, aes(x = x, y = y)) +
geom_raster(aes(fill = z))
Run Code Online (Sandbox Code Playgroud)
虽然我将绘图保存为矢量化图像(.pdf;那不是那么大),但打开时 pdf 的加载速度非常慢。我希望在打开文件时呈现数据框中的每个单独点。
我已经阅读了其他用于可视化矩阵的帖子(例如R 中的数据探索:显示大矩阵的热图,很快?)image(),但是我想使用 ggplot 来修改图像。
问题:如何加快此图的渲染速度?有没有办法(除了降低绘图的分辨率),同时保持图像矢量化,以加快这个过程?是否可以对矢量化 ggplot 进行下采样?
我在 R 中有一个数据框,如下所示:
data.frame(ID = seq(1, 12, 1),
value = rnorm(12))
#> ID value
#> 1 1 -0.05695300
#> 2 2 -0.95685356
#> 3 3 0.32508199
#> 4 4 -1.26247870
#> 5 5 0.65572362
#> 6 6 -1.23285703
#> 7 7 -1.57634388
#> 8 8 -0.50605901
#> 9 9 -0.52063312
#> 10 10 0.76800781
#> 11 11 1.10101402
#> 12 12 0.09528496
Run Code Online (Sandbox Code Playgroud)
我想将 重命名IDs为ID-01, ID-02... ID-12。
感觉像是一份工作dplyr mutate,但是,我不知道该怎么做。
你会怎么做这是R?