我试图用 ggraph 绘制一个网络,我想在图形周围添加一个圆圈,边缘和节点位于圆圈内。
使用以下代码绘制圆圈效果很好(改编自Draw a circle with ggplot2)
gg_circle <- function(r, xc, yc, color = "black", fill = NA, lty = NA, size = NA, ...) {
x <- xc + r*cos(seq(0, pi, length.out = 100))
ymax <- yc + r*sin(seq(0, pi, length.out = 100))
ymin <- yc + r*sin(seq(0, -pi, length.out = 100))
annotate("ribbon", x = x, ymin = ymin, ymax = ymax,
color = color, fill = fill, lty = lty, size = size, ...)
} …Run Code Online (Sandbox Code Playgroud) 我希望 56 显示在 x 轴上,但我无法弄清楚。
我有以下脚本。我尝试将以下内容添加到脚本中xlim = c(seq(0,100, by=10),56),但这似乎不起作用。
我尝试用谷歌搜索它并阅读了 R 文档。我希望你能帮忙。
library(survival)
library(survminer)
library(ggplot2)
fit <- survfit(Surv(p$time.recur.months, p$recurrence) ~ p$simpson.grade,
conf.type="log", data=p)
j <- ggsurvplot(
fit,
data = p,
fun="cumhaz",
risk.table = TRUE,
pval = TRUE,
pval.coord = c(0, 0.25),
conf.int = F,
legend.labs=c("Simpson Grade 1" ,"Simpson Grade 2", "Simpson Grade 3",
"Simpson Grade 4"),
size=c(0.7,0.7,0.7,0.7),
xlim = c(0,100),
alpha=c(0.7),
break.time.by = 10,
xlab="Time in months",
#ylab="Survival probability",
ggtheme = theme_gray(),
risk.table.y.text.col = T,
risk.table.y.text = TRUE, …Run Code Online (Sandbox Code Playgroud) 我有以下图表:
并且想进行我认为非常简单的更改:我想删除左侧刻面标签边框线的顶部、右侧和底部。
我如何删除这些线,或绘制右手线的等价物?如果可能的话,我宁愿不要乱搞,但不会拒绝任何有效的解决方案。
图代码:
library(ggplot2)
library(dplyr)
library(forcats)
posthoc1 %>%
mutate(ordering = -as.numeric(Dataset) + Test.stat,
Species2 = fct_reorder(Species2, ordering, .desc = F)) %>%
ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) +
geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) +
ylab("") +
theme_classic(base_size = 20) +
facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y", switch = "y") +
geom_vline(xintercept = 0) +
theme(axis.text.x=element_text(colour = "black"),
strip.placement = "outside",
strip.background.x=element_rect(color = NA, fill=NA),
strip.background.y=element_rect(color = "black", fill=NA)) +
coord_cartesian(clip = "off") +
scale_x_continuous(limits=NULL)
Run Code Online (Sandbox Code Playgroud)
数据:
structure(list(Dataset = structure(c(1L, 1L, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将一些图表相互匹配。有些是在 Sigmaplot 中制作的,我无权访问数据。所以这意味着我的新图表必须看起来尽可能相似,我正在使用它ggplot来实现这一点。我添加了 100 万个小细节,使它们更相似,但我仍然无法理解其中一个细节。
线端应该是四舍五入的,我已经通过lineend = "round"在geom_line(). 然而,图例中的线端仍然有一个对接端。我是一个坚持一致性的人,每一个细节都是如此,这意味着我真的不能接受......
这是一个例子。
var1 <- seq(1,100,10)
var2 <- c(1:10)
trt <- c("t1", "t1", "t1", "t1", "t1", "t2", "t2", "t2", "t2", "t2")
my.df <- data.frame(var1, var2, trt)
ggplot(data = my.df, aes(x = var1, y = var2, col = trt))+
geom_line(size = 3, lineend = "round")
Run Code Online (Sandbox Code Playgroud) I want to plot a world map with multiple points aka combinations of latitude and longitude coordinates.
I don't want to use Mercator, therefore I re-project both, the data for the world map and my coordinates.
While the projection for the world changes, all points are suddenly placed in the middle of the map (a common behavior, when the projections don't align, see https://www.earthdatascience.org/courses/earth-analytics/spatial-data-r/intro-to-coordinate-reference-systems/).
What am I doing wrong in when assigning the projection to the points?
My code: …
这是一个与自定义 geom 相关的问题,从这个答案修改。给定的 geom 分组失败,所以我包含coord_munch在 中draw_panel,受到GeomLine和 的启发GeomPath。它实际上在许多情况下都有效,但我觉得它同样经常失败。
特别是,它似乎以两人为一组失败(参见下面的示例),并且在使用拼凑而成的某些情节时奇怪地失败。我打开了一个问题,但还没有得到答复,我对此并不感到惊讶,我同意并觉得这实际上是一个写得不好的geom问题,而不是一个拼凑的问题。
我相信## Work out grouping variables for grobs用于 GeomPath的分组(在代码中,用
我的主要问题是,我如何检查这个对象?
如果有人看到并理解我的 geom 的问题,我会更加感激。干杯
例子:
library(tidyverse)
## this is not an arrange problem, as shown by the correct plot using geom_path
testdf <- testdf %>% arrange(id, group, x)
Run Code Online (Sandbox Code Playgroud)
与 geom_path 一起使用
ggplot(testdf, aes(x, y)) +
geom_path(aes(group = id))
Run Code Online (Sandbox Code Playgroud)

geom_trail 失败
ggplot(testdf, aes(x, y)) +
geom_path(aes(group = id))
Run Code Online (Sandbox Code Playgroud)

使用颜色时更糟糕
ggplot(testdf, aes(x, …Run Code Online (Sandbox Code Playgroud) 当需要对模型变量之一进行分层时,我试图找到一种方法来从 Cox-PH 模型制作风险比的森林图。对于非分层模型,该ggforest()功能非常出色。运行一些示例代码
library(survival)
library(survminer)
model <- coxph(Surv(time, status) ~ sex + rx + adhere,
data = colon )
ggforest(model)
colon <- within(colon, {
sex <- factor(sex, labels = c("female", "male"))
differ <- factor(differ, labels = c("well", "moderate", "poor"))
extent <- factor(extent, labels = c("submuc.", "muscle", "serosa", "contig."))
})
bigmodel <-
coxph(Surv(time, status) ~ sex + rx + adhere + differ + extent + node4,
data = colon )
ggforest(bigmodel)
Run Code Online (Sandbox Code Playgroud)
产生这个图

但是,如果我必须纠正分层的非比例性
stratamodel <- coxph(Surv(time, status) ~ sex …Run Code Online (Sandbox Code Playgroud) 下面的代码创建一个散点图,并使用 theme_bw 和背景中的网格线 -
data = mtcars
data %>%
select(mpg, disp) %>%
ggplot(aes(disp, mpg))+
geom_point(size = 3)+
theme_bw()
Run Code Online (Sandbox Code Playgroud)
我还想在图表上添加一些垂直和水平线。然而,从目前的网格线来看,它看起来有点繁忙。有没有办法进一步降低网格线的可见性。我不想完全删除它们。
几个小时以来,我一直在挠头。我现在所拥有的:
library(ggplot2)
library(grid)
all_data = data.frame(country=rep(c("A","B","C","D"),times=1,each=20),
value=rep(c(10,20,30,40),times=1,each=20),
year = rep(seq(1991,2010),4))
# PLOT GRAPH
p1 <- ggplot() + theme_bw() + geom_line(aes(y = value, x = year,
colour=country), size=2,
data = all_data, stat="identity") +
theme(plot.title = element_text(size=18,hjust = -0.037), legend.position="bottom",
legend.direction="horizontal", legend.background = element_rect(size=0.5, linetype="solid", colour ="black"),
legend.text = element_text(size=16,face = "plain"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.border = element_blank(),axis.line = element_line(colour = "black"),legend.title = element_blank(),
axis.text=element_text(size=18,face = "plain"),axis.title.x=element_text(size=18,face = "plain", hjust = 1,
margin = margin(t = 10, r = …Run Code Online (Sandbox Code Playgroud) 我正在使用 tidyr 中的 pivot_longer 将数据帧从宽转换为长。我希望使用所有列并在列中维护行名。较早的melt函数在此调用中完美运行
w1 <- reshape2::melt(w)
head(w1)
'data.frame': 900 obs. of 3 variables:
$ Var1 : Factor w/ 30 levels "muscle system process",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Var2 : Factor w/ 30 levels "muscle system process",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value: num NA NA NA NA NA NA NA NA NA NA ...
Run Code Online (Sandbox Code Playgroud)
但pivot_longer 没有
w %>% pivot_longer()
Error in UseMethod("pivot_longer") : …Run Code Online (Sandbox Code Playgroud)