有没有办法在 ggplot2 中沿着密度线放置文本,或者就此而言,沿着任何路径放置文本?我的意思是一次作为标签,采用这种 xkcd 风格:1835、1950(中间面板)、1392或2234(中间面板)。或者,有没有办法让该行重复文本,例如xkcd #930?我对所有 xkcd 表示歉意,我不确定这些样式叫什么,这是我能想到的唯一一个我以前见过的以这种方式区分区域的地方。
注意:我不是在谈论手绘的 xkcd 风格,也不是在顶部放置平面标签
我知道我可以放置一段笔直/平坦的文本,例如 viaannotate
或geom_text
,但我很好奇弯曲此类文本,使其看起来沿着数据的曲线。
我也很好奇这种文本风格有名字吗?
ggplot2 图表示例使用annotate(...)
:
上面的示例图在 Inkscape 中用弯曲文本修改:
编辑:根据要求,这是 3 月和 4 月前两次试运行的数据:
df <- data.frame(
monthly_run = c('March', 'March', 'March', 'March', 'March', 'March', 'March',
'March', 'March', 'March', 'March', 'March', 'March', 'March',
'April', 'April', 'April', 'April', 'April', 'April', 'April',
'April', 'April', 'April', 'April', 'April', 'April', 'April'),
duration = …
Run Code Online (Sandbox Code Playgroud) 使用 mtcars 实现可重复性
(这是一个行操作)。我想根据它们的大小连续保留 3 个值(所以基本上前 3 个值将具有价值,其余的一切都更改为 NA)
我尝试使用 pivot_longer 转换为 long 然后过滤,但问题是我想再次转换为宽,因为我想保留数据的结构。
mtcars %>%
pivot_longer(cols = everything()) %>%
group_by(name) %>% top_n(3)
Run Code Online (Sandbox Code Playgroud)
注意:在 mtcars 中,所有 3 行都具有与非 NA 相同的列名值,但在原始数据集中会有所不同。(最好是tidyverse解决方案)
我在协调情节中不同元素的大小方面遇到了一些麻烦。具体来说,我希望注释的大小与 y 轴标题的大小相同。
然而,以下代码会产生不同的大小:
library(ggplot2)
test_data <- data.frame(x = c(1), y = c(1))
ggplot(test_data) +
geom_point(aes(x = x, y = y)) +
theme_bw(base_size = 14) +
annotate("text", label = "A", x = 0.975, y = 1.025, size = unit(14, "pt")) +
ylab("Why not the same size?") +
theme(axis.title.y = element_text(size = unit(14, "pt")))
Run Code Online (Sandbox Code Playgroud)
是什么赋予了?
我正在尝试使用 R 和 package 定制竞争风险图cmprsk
。具体来说,我想覆盖默认情况,即对于竞争事件使用颜色,对于不同组使用线型。
这是我的可重现的示例:
library(ggplot2)
library(cmprsk)
library(survminer)
# some simulated data to get started
comp.risk.data <- data.frame("tfs.days" = rweibull(n = 100, shape = 1, scale = 1)*100,
"status.tfs" = c(sample(c(0,1,1,1,1,2), size=50, replace=T)),
"Typing" = sample(c("A","B","C","D"), size=50, replace=T))
# fitting a competing risks model
CR <- cuminc(ftime = comp.risk.data$tfs.days,
fstatus = comp.risk.data$status.tfs,
cencode = 0,
group = comp.risk.data$Typing)
# the default plot makes it impossible to identify the groups
ggcompetingrisks(fit = CR, multiple_panels = F, xlab = …
Run Code Online (Sandbox Code Playgroud) 如何在函数内使用highcharter :: hchart?
这是一个使用该hchart
函数的简单折线图。
library(tidyverse)
library(lubridate)
library(highcharter)
library(nycflights13)
flights_2 <- flights %>%
mutate(dep_mo = ymd(str_c(year, month, "01", sep = "-"))) %>%
group_by(dep_mo) %>%
summarize(arr_delay = mean(arr_delay, na.rm = TRUE))
hchart(flights_2,
type = "line",
hcaes(x = dep_mo, y = arr_delay),
name = "Average Arrival Delay")
Run Code Online (Sandbox Code Playgroud)
当我尝试编写一个函数来创建相同的图形时,出现错误。
h_fun <- function(df, x, y) {
hchart(df,
type = "line",
hcaes(x = x, y = y),
name = "Average Arrival Delay"
)
}
h_fun(df = flights_2, x = dep_mo, y = …
Run Code Online (Sandbox Code Playgroud) 我有一个包含两个不同变量的条形图。\n对于其中一个因素 (gr),我在图中选择了不同的 \xc2\xb4lintype\xc2\xb4。\n“gr”的图例显示 \xc2\xb4lintype\xc2 \xb4 但用深灰色填充,我认为这很令人困惑。
\n有谁知道如何去除填充或将其更改为白色或透明?
\n(我发现的所有提示仅更改图例的背景,但不影响灰色填充)
yval <- c(3, 7, 4, 4, 8, 9, 4, 7, 9, 6, 6, 3)\ntrt <- rep(c("A", "B", "C"), times=4)\ngr <- rep(c(rep(("case"), times = 3), rep(("control"), times = 3)), times = 2)\nvar <- c(rep(("var1"), times = 6), rep(("var2"), times = 6)) \ndf <- data.frame(yval, device, ccgroup, var)\n\nggplot(data=df, aes(x=var)) +\n geom_bar( color = "black", size = 1, aes(weights = yval, fill = trt, linetype = gr) , position = "dodge")\n
Run Code Online (Sandbox Code Playgroud)\n问题:
我有一个名为“New_Acoustic_Parameters”的数据框,其中包含七个变量(请参阅下面的数据结构),我想生成描述性统计的汇总表(平均值、标准差、标准误差、最小值、最大值、q25、q75、和变异系数 - CV),使用 dplyr 包以及select和summarize函数。我的目标是生成一个类似于下图的表格。
我尝试了许多不同的方法来使用不同的变体编写此代码,并且我尝试遵循 StackOverflow 上针对问题给出的其他解决方案,但这对我不起作用。我感觉很困惑!当我运行下面的代码时,没有任何反应。
有人可以帮忙吗?
如果有人可以提供帮助,非常感谢。
数据结构:
$ ID : int 1 2 3 4 5 6 7 8 9 10 ...
$ Low.Freq : num 7278 3965 4888 3639 12948 ...
$ High.Freq : num 11351 6897 6626 5248 15549 ...
$ Peak.Freq : num 10767 4221 5943 4048 13867 ...
$ Delta.Freq : num 4073 2933 1738 1609 2600 ...
$ …
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的数据框:
ID Var1 Var2 Month BeforeAfter
1 1 23 4 1 FALSE
2 1 41 5 2 FALSE
3 1 32 2 3 FALSE
4 1 58 1 4 TRUE
5 1 60 7 5 TRUE
6 2 12 3 1 FALSE
7 2 34 4 2 FALSE
8 2 55 5 3 TRUE
9 2 49 5 4 TRUE
10 2 60 6 5 TRUE
11 2 64 9 6 TRUE
Run Code Online (Sandbox Code Playgroud)
我想创建一个图(例如ggplot
)来显示 的某种处理之前和之后Var1
。因此,我希望治疗时间/x 轴为 0,并且与治疗相关的月份应在其附近。这些 …
这是一个示例,输出是一个 png 文件hsa04110.gse16873.png
。我的问题是如何直接显示绘图而不是将其另存为文件。
library(pathview)
data(gse16873.d)
data(demo.paths)
data(paths.hsa)
pathview(gene.data = gse16873.d[, 1], pathway.id = demo.paths$sel.paths[1],
species = "hsa", out.suffix = "gse16873", kegg.native = T)
#> 'select()' returned 1:1 mapping between keys and columns
#> Info: Working in directory D:/github/RNASeq-KEGG
#> Info: Writing image file hsa04110.gse16873.png
Run Code Online (Sandbox Code Playgroud) 有我的数据框
Days,Observed,Simulated
0,0,424.8933328
1,1070,1116.781453
2,2360,2278.166227
3,3882,3854.781359
4,5712,5682.090936
5,7508,7565.230044
6,9126,9343.991798
7,10600,10919.17995
8,11893,12249.07067
9,13047,13332.93044
10,14022,14193.53941
11,14852,14863.84784
12,15480,15378.56415
13,16042,15769.6773
14,16362,16064.57556
15,16582,16285.66038
16,16766,16450.70955
17,16854,16573.54275
18,16854,16664.74816
Run Code Online (Sandbox Code Playgroud)
这是我的代码,希望我没有错过一些信息
dt <- read.csv('data.csv')
days <- dt$Days
Observed <- dt$Observed
Simulated <- dt$Simulated
require(ggplot2)
R <- ggplot(dt, aes(x = days))+geom_line(y=Simulated, color="red", size=0.5)+
geom_point(y=Observed, color="midnightblue", size=1.75)
a <- geom_line(aes(y = Simulated, col='Simulated'))
n <- geom_point(aes(y = Observed, fill = "Observed"), col='blue')
c <- ggtitle("2.5kg of Placenta & 0.5kg of seed")
h <- labs(x = 'Time(Days)', y = "Cumulative Biogas Yield(ml)", …
Run Code Online (Sandbox Code Playgroud) 我有一个数据框,它有十进制格式的日期和时间列日期时间是当前格式,预期格式是这样的
Date Time Expected format
1 43824.838 2019-12-27 20:06:43
2 43824.842 2019-12-27 20:12:28
3 43824.846 2019-12-27 20:18:14
4 43824.850 2019-12-27 20:24:00
5 43824.854 2019-12-27 20:29:45
6 43824.858 2019-12-27 20:35:31
7 43824.863 2019-12-27 20:42:43
8 43824.867 2019-12-27 20:48:28
Run Code Online (Sandbox Code Playgroud)
使用以下十进制日期时间:
c(43824.838, 43824.842, 43824.846, 43824.85, 43824.854, 43824.858, 43824.863, 43824.867)
Run Code Online (Sandbox Code Playgroud) 我想我已经被派到这里接受培训了。
library(caret)
library(mlbench)
library(plotROC)
library(pROC)
data(Sonar)
ctrl <- trainControl(method="cv",
summaryFunction=twoClassSummary,
classProbs=T,
savePredictions = T)
rfFit <- train(Class ~ ., data=Sonar,
method="rf", preProc=c("center", "scale"),
trControl=ctrl)
# Select a parameter setting
selectedIndices <- rfFit$pred$mtry == 2
Run Code Online (Sandbox Code Playgroud)
我想绘制 ROC。
plot.roc(rfFit$pred$obs[selectedIndices],
rfFit$pred$M[selectedIndices])
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试 ggplot2 方法时,它给了我完全不同的东西。
g <- ggplot(rfFit$pred[selectedIndices, ], aes(m=M, d=factor(obs, levels = c("R", "M")))) +
geom_roc(n.cuts=0) +
coord_equal() +
style_roc()
g + annotate("text", x=0.75, y=0.25, label=paste("AUC =", round((calc_auc(g))$AUC, 4)))
Run Code Online (Sandbox Code Playgroud)
我在这里做了一些非常错误的事情,但我不知道它是什么。谢谢。