FIS*_*HnR 1 r ggplot2 facet-wrap ggpmisc
我想知道为什么文本在图中的趋势更高......它不会与 facet_wrap 或 facet_grid 保持一致。在更复杂的数据集图中,由于重叠,文本难以辨认。
以下是重现情节和问题的数据和代码。将 geom="text" 添加到 stat_fit_glance,结果是Error: Discrete value supplied to continuous scale.
library(ggpmisc)
library(ggplot2)
DF <- data.frame(Site = rep(LETTERS[20:24], each = 4),
Region = rep(LETTERS[14:18], each = 4),
time = rep(LETTERS[1:10], each = 10),
group = rep(LETTERS[1:4], each = 10),
value1 = runif(n = 1000, min = 10, max = 15),
value2 = runif(n = 1000, min = 100, max = 150))
DF$time <- as.numeric(DF$time)
formula1 <- y~x
plot1 <- ggplot(data=DF,
aes(x=time, y= value2,group=Site)) +
geom_point(col="gray", alpha=0.5) +
geom_line(aes(group=Site),col="gray", alpha=0.5) +
geom_smooth(se=F, col="darkorange", alpha=0.8, fill="orange",
method="lm",formula=formula1) +
theme_bw() +
theme(strip.text.x = element_text(size=10),
strip.text.y = element_text(size=10, face="bold", angle=0),
strip.background = element_rect(colour="black", fill="gray90"),
axis.text.x = element_text(size=10), # remove x-axis text
axis.text.y = element_text(size=10), # remove y-axis text
axis.ticks = element_blank(), # remove axis ticks
axis.title.x = element_text(size=18), # remove x-axis labels
axis.title.y = element_text(size=25), # remove y-axis labels
panel.background = element_blank(),
panel.grid.major = element_blank(), #remove major-grid labels
panel.grid.minor = element_blank(), #remove minor-grid labels
plot.background = element_blank()) +
labs(y="", x="Year", title = "")+ facet_wrap(~group)
plot1 + stat_fit_glance(method = "lm", label.x="right", label.y="bottom",
method.args = list(formula = formula1),
aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',
stat(..r.squared..),stat(..p.value..))),
parse = TRUE)
Run Code Online (Sandbox Code Playgroud)
当标签的npcy位置自动设置时,分组变量中每个级别的位置都会增加。您映射Site到group美学,因为Site5 个级别不均匀地出现在不同方面,'ggpmisc' 中相当粗糙的算法不均匀地定位标签:五行对应五个站点中的每一个。我已将映射更改为使用颜色,以便这变得更加明显。我还删除了与此问题无关的所有代码。
plot1 <- ggplot(data=DF,
aes(x=time, y= value2, color=Site)) +
geom_smooth(se=F, alpha=0.8,
method="lm",formula=formula1) +
facet_wrap(~group)
plot1 +
stat_fit_glance(method = "lm", label.x="right", label.y="bottom",
method.args = list(formula = formula1),
aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',
stat(..r.squared..),stat(..p.value..))),
parse = TRUE) +
expand_limits(y = 110)
Run Code Online (Sandbox Code Playgroud)
要使用固定位置,如果使用默认的“geom_text_npcy()”或传递数据坐标并使用“geom_text()”,则可以传递 npcy 坐标。一个位置对应分组因子的每个级别Site。如果向量较短,则将其回收。当然,为了适应更多标签,您可以通过扩大绘图区域来减小文本大小并增加空间。在任何情况下,在实践中,您都需要以某种方式指出哪些估计对应于哪条线。
plot1 +
stat_fit_glance(method = "lm", label.x="right", label.y= c(0.01, 0.06, 0.11, 0.01, 0.06),
method.args = list(formula = formula1),
aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',
stat(..r.squared..),stat(..p.value..))),
parse = TRUE, size = 2.5) +
expand_limits(y = 110)
Run Code Online (Sandbox Code Playgroud)
注意:Error: Discrete value supplied to continuous scale尝试使用时
geom_text()是我几天前修复的“ggpmisc”中的一个错误,但还没有进入 CRAN(未来版本 0.3.3)。
| 归档时间: |
|
| 查看次数: |
577 次 |
| 最近记录: |