我尝试在 x 轴上连接来自两种不同方法 ( measure ) 的测量值之间的抖动点。这些测量值通过先证者 ( a )相互关联,先证者可以分为两个主要组,患者 ( pat ) 和对照 ( ctr ),我的 df 是这样的:
set.seed(1)
df <- data.frame(a = rep(paste0("id", "_", 1:20), each = 2),
value = sample(1:10, 40, rep = TRUE),
measure = rep(c("a", "b"), 20), group = rep(c("pat", "ctr"), each = 2,10))
Run Code Online (Sandbox Code Playgroud)
我试过
library(ggplot2)
ggplot(df,aes(measure, value, fill = group)) +
geom_point(position = position_jitterdodge(jitter.width = 0.1, jitter.height = 0.1,
dodge.width = 0.75), shape = 1) +
geom_line(aes(group = a), position = position_dodge(0.75))
Run Code Online (Sandbox Code Playgroud)

请考虑以下图表:
require(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() +
labs(title = 'Iris[small font]' ) +
theme_classic()
Run Code Online (Sandbox Code Playgroud)
左图是代码输出,右图显示了所需的结果,我使用了Adobe Illustrator
问题是,如果可以在行中更改字体大小,在本例中标题中的"[small font]"标签,但当然这也是关于其他标签的一般性问题,例如轴和图例等等
显然,字体大小被设定为theme().但是,可能有一种方法设置"相对字体大小",例如使用rel()和以某种方式使用贴标机功能?
我试图在函数中使用dplyr创建一个用户定义的函数,我可以传递多个参数来使用dplyr汇总数据,然后用ggplot绘制它.
这是一些示例数据以及我正在尝试使用dplyr然后绘制的内容
df <-data.frame(Year = c("2006", "2006", "2006", "2007", "2007", "2007", "2008", "2009", "2010", "2010", "2009", "2009"), JudicialOrientation = c("Defense", "Plaintiff", "Plaintiff", "Neutral", "Defense", "Plaintiff", "Defense", "Plaintiff", "Neutral", "Neutral", "Plaintiff","Defense"), Loss = c(100000, 100, 2500, 100000, 25000, 0, 7500, 5200, 900, 100, 0, 50))
df1 <- df %>%
group_by(Year, JudicialOrientation) %>%
summarise(MeanLoss =mean(Loss))
ggplot(df1, aes(x = JudicialOrientation, y = MeanLoss, color = Year, group =Year)) +
geom_line() +
geom_point()
Run Code Online (Sandbox Code Playgroud)
我现在正在尝试将其复制到用户函数中,以便我可以传递不同的变量来获得类似的结果.
这是我到目前为止的尝试:
ConsistencyPlot <- function(df,var1,timevar,lossvar){
df1 <- df %>%
group_by_(df[timevar], df[var1]) …Run Code Online (Sandbox Code Playgroud) 另一个“传奇-问题”。
我有几种美学,并希望指定绘制每种美学的图例的顺序。多数线程有关更改项目的顺序中的审美,但是这不是我的问题。在我的示例中,我想指定填充图例的位置。有趣的是,当在底部绘制图例时,颜色图例绘制在填充图例的顶部,但“右”到填充图例。对于像我这样的从上到下阅读的从左到右的读者来说,这似乎有些随机。
该图显然有些随机,只是为了 reprex 目的而快速制作的。
library(ggplot2)
ggplot(mtcars) +
geom_boxplot(aes(cyl, hp, fill = as.character(gear))) +
geom_boxplot(aes(cyl, disp, color = as.character(cyl))) +
labs(fill = 'fill', color = 'color')
# here I would like the fill legend to be *above* the color legend
Run Code Online (Sandbox Code Playgroud)

ggplot(mtcars) +
geom_boxplot(aes(cyl, hp, fill = as.character(gear))) +
geom_boxplot(aes(cyl, disp, color = as.character(cyl))) +
labs(fill = 'fill', color = 'color') +
theme(legend.position = 'bottom')
# here I would like the fill legend to be *right* and the color …Run Code Online (Sandbox Code Playgroud) 当尝试在不同方面创建差异 geom_rect 时,我遇到了一些奇怪的行为。在下面的示例中,geom_point 的颜色应与 geom_rect 的填充颜色相同,填充颜色应与 geom_rect 的轮廓相同。然而,这些因素似乎在某个地方以一种奇怪的方式串联起来。
任何帮助,将不胜感激。
我不希望点按序列着色,因为簇和序列并不总是相同(它用于可视化排列统计量)。
这些显然不是我想要绘制的实际数据!
library(data.table)
library(ggplot2)
scatter.dt = data.table(sequence = factor(paste('Sequence',c(1,2,3,4))),
cluster = factor(paste('Cluster',c(1,2,3,4))),
outcome = c(1,1,1,1),
transition.time = 1:4,
intervention.time = 1:4)
vline.dt = data.table(sequence = scatter.dt$sequence,
cluster = scatter.dt$cluster,
transition.time = 1:4,
intervention.time = 2:5)
plot1 = ggplot2::ggplot() +
ggplot2::geom_rect(data = vline.dt,
aes(fill = sequence,
colour = sequence,
xmin = transition.time,
xmax = intervention.time,
ymin = -Inf,
ymax = Inf),
alpha = .6,
size = 2) +
ggplot2::geom_point(data=scatter.dt,
aes(x=transition.time ,
y=intervention.time, …Run Code Online (Sandbox Code Playgroud) 我想guide_colorbar用字符描述(例如“低”和“高”)而不是实际数字来修改连续美学。当对多个图(热图,例如 )使用一个图例或颜色条时,这尤其有用geom_bin2d。
这里有一个例子。
说出给定的虚拟数据:
dd <- data.frame(xx=rnorm(100),yy=rnorm(100),zz=rep(1:10,10))
Run Code Online (Sandbox Code Playgroud)
我可以像往常一样
ggplot(dd,aes(xx,yy,color=zz))+
geom_point()+
viridis::scale_color_viridis(option='A',direction=-1)
Run Code Online (Sandbox Code Playgroud)
并使用隐藏颜色条注释
guides(color=guide_colorbar(ticks=F,label=F,title=element_blank()))
Run Code Online (Sandbox Code Playgroud)
我尝试的另一种方法是修改色彩美学
factor(zz,labels=c('low',2:9,'high'))
...
guides(color=guide_legend(override.aes=list(size=5,shape=15)))
Run Code Online (Sandbox Code Playgroud)
并绘制为离散的。也不是真正想要的。
如何添加自定义文本guide_colorbar?或者:有没有办法将离散图例转换为颜色条并保留字符标签?
我很难理解is.null和之间的范围差异missing,特别是为什么以下内容不适用于is.null,但适用于missing:
foo_iris <- function(a = NULL){
if(!is.null(a)) return('not.null') else return('is.null')
}
foo_iris(a = Species)
#> Error in foo_iris(a = Species): object 'Species' not found
foo_iris <- function(a = NULL){
if(missing(a)) return('is.null') else return('not.null')
}
foo_iris(a = Species)
#> [1] "not.null"
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.3.0)于 2020 年 1 月 16 日创建
我需要结合连续和分类数据,并需要分解我的连续变量。我很难理解如何在正确的限制上切断轴刻度。
我知道 x 限制基于新的因子水平,但为什么刻度超出下限?
背景:我想将连续变量(与geom_smooth)上的线性回归线与对相同变量(例如,geom_boxplot)的分箱数据的统计数据进行汇总。我需要创建一个具有所有级别的因子,否则这些图不会重叠。但这会在两侧产生大量空白,因此我尝试使用 设置限制coord_cartesian,我知道它始终是连续的。
library(ggplot2)
foo <- data.frame(x = 20:50, y = rnorm(31))
ggplot(foo) +
geom_col(aes(factor(x), y)) +
coord_cartesian(xlim = c(10,50))
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020 年 3 月 2 日创建

我不明白为什么底角背景是白色的,而绘图的其余部分是灰色的。无论如何,我可以将底角颜色更改为与绘图其余部分相同的背景吗?
\n我对每个图使用的代码是:
\nHP_specifikationer_model1 <- ggplot(Svar_spec_data)+geom_hline(yintercept=0)\ngeom_line(aes(y=HP1, x=kvartaler, color = "HP-BNP-KRE-REN")) +\ngeom_line(aes(y=HP2, x=kvartaler, color = "REN-KRE-HP-BNP")) +\ngeom_line(aes(y=HP3, x=kvartaler, color = "BNP-KRE-REN-HP")) +\ngeom_line(aes(y=HP4, x=kvartaler, color = "KRE-HP-REN-BNP")) +\ngeom_line(aes(y=HP5, x=kvartaler, color = "BNP-KRE-HP-REN")) +\ngeom_line(aes(y=HP6, x=kvartaler, color = "BNP-REN-HP-KRE")) +\ngeom_line(aes(y=HP7, x=kvartaler, color = "REN-HP-BNP-KRE")) +\nscale_color_manual(name=\'Specifikation\',\n breaks=c(\'HP-BNP-KRE-REN\', \'REN-KRE-HP-BNP\', \'BNP-KRE-REN-HP\',\n \'KRE-HP-REN-BNP\', \'BNP-KRE-HP-REN\', \'BNP-REN-HP-KRE\',\n \'REN-HP-BNP-KRE\'),\n values=c(\'HP-BNP-KRE-REN\'=\'red\', \'REN-KRE-HP-BNP\'=\'blue\', \'BNP-KRE-REN-HP\'=\'green\',\n \'KRE-HP-REN-BNP\'=\'cyan\', \'BNP-KRE-HP-REN\'=\'lightpink\', \'BNP-REN-HP-KRE\'=\'orange\',\n \'REN-HP-BNP-KRE\'=\'black\'))+\nlabs(title = "Varrierende SVAR matricer ved st\xc3\xb8d til Boligpriserne", x="Lag (M\xc3\xa5neder)", y="Respons fra boligprisen", caption = "Egne beregninger", color= T)+\ntheme(legend.position="right") + theme(panel.grid.minor.x = element_blank()) …Run Code Online (Sandbox Code Playgroud) 我想创建一个如下图所示的图表。它是一种使用 geom_area 和 geom_point 的组合。
\n\n假设我的数据如下所示:
\nlibrary(gcookbook, janitor)\n\nggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) +\n geom_area()\nRun Code Online (Sandbox Code Playgroud)\n\n然后,我想添加确切的点数作为每个类别的总数,即:
\nlibrary(dplyr)\nuspopage |> \n group_by(AgeGroup) |> \n summarize(total = sum(Thousands))\n\n# A tibble: 8 \xc3\x97 2\n AgeGroup total\n <fct> <int>\n1 <5 1534529\n2 5-14 2993842\n3 15-24 2836739\n4 25-34 2635986\n5 35-44 2331680\n6 45-54 1883088\n7 55-64 1417496\n8 >64 1588163\nRun Code Online (Sandbox Code Playgroud)\n