r - **ggplot2**:scale_x_continuous(breaks = ) 和 xlim() 之间的差异(或等效 scale_y_continouous(breaks=) 和 ylim)
问题描述
在ggplot2中,我想在图中指定值标签,并使 y 轴和 x 轴在同一范围内,即使没有数据存在。
基本图p
如下所示。我想每隔 5 向 x 轴和 y 轴添加标签,并将 x 轴边框扩展到 105,尽管不存在边框。
谁能帮我理解如何scale_x_continuous(breaks=...)
互动xlim()
?或者有什么scale_x_continuous(breaks=..., lim = c(...))
不同?
library(ggplot2)
library(viridis)
#> Loading required package: viridisLite
ggplot_common_scatter <- function(p, size = 3, step = 1){
q <- p +
theme_bw() +
geom_point(size = size) +
scale_y_continuous(breaks = h(step)) +
scale_fill_viridis(begin = 0.4, end = 1, direction = 1, discrete = TRUE) +
theme(legend.position = "bottom",
axis.text = element_text(size = 13), #Text size of axes
axis.title = element_text(size = 14,face = "plain"), #Text size of axis titles
# axis.text.x = element_text(angle = 90) #not if text
# only for MSD
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), #remove gridlines
legend.box = "horizontal", legend.direction = "vertical" #add if needed if legend is too wide.
)
return(q)
}
h <- function(k) {
step <- k
# Round the minimum down to the nearest 5
# y.min <- 5*floor(min(y)/5
# Round the maximum up to the nearest 5
# y.max <- 5*ceiling(max(y)/5)
function(y){
# cat("min: ", min(y, na.rm = TRUE), "max: ", max(y, na.rm = TRUE), "|| Labels:")
start <- 5*floor(min(y, na.rm = TRUE)/5)
end <- 5*ceiling(max(y, na.rm = TRUE)/5)
A <- seq(start, end, by = step)
# print(A)
return(A)
}
}
data(anorexia, package = "MASS")
p <- ggplot_common_scatter(
ggplot(anorexia,
aes(x = Prewt ,
y = Postwt,
col = Treat,
pch = Treat
)
),
step = 10
)+
ggtitle("Comparisons of Aneorxia girls"
) +
geom_rect(color = "black", fill = NA,
aes(xmin = 70, xmax = 100, ymin = 70, ymax = 100) #lty = "dashed", alpha = 0.5
)
p
p + xlim(70, 105)
p + scale_x_continuous(breaks = seq(70, 105, by = 5))
#Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale
p + scale_x_continuous(breaks = seq(70, 105, by = 5)) + xlim(70, 105)
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.
p + scale_x_continuous(breaks = seq(70, 105, by = 5), lim = c(70, 105))
xfun::session_info(c("ggplot2", "viridis"))
#> R version 4.0.4 (2021-02-15)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 18363)
#>
#> Locale:
#> LC_COLLATE=English_United States.1252
#> LC_CTYPE=English_United States.1252
#> LC_MONETARY=English_United States.1252
#> LC_NUMERIC=C
#> LC_TIME=English_United States.1252
#>
#> Package version:
#> assertthat_0.2.1 cli_2.3.1 colorspace_2.0.0 crayon_1.4.1
#> digest_0.6.27 ellipsis_0.3.1 fansi_0.4.2 farver_2.1.0
#> ggplot2_3.3.3 glue_1.4.2 graphics_4.0.4 grDevices_4.0.4
#> grid_4.0.4 gridExtra_2.3 gtable_0.3.0 isoband_0.2.4
#> labeling_0.4.2 lattice_0.20.41 lifecycle_1.0.0 magrittr_2.0.1
#> MASS_7.3.53.1 Matrix_1.3.2 methods_4.0.4 mgcv_1.8.34
#> munsell_0.5.0 nlme_3.1.152 pillar_1.5.1 pkgconfig_2.0.3
#> R6_2.5.0 RColorBrewer_1.1.2 rlang_0.4.10 scales_1.1.1
#> splines_4.0.4 stats_4.0.4 tibble_3.1.0 tools_4.0.4
#> utf8_1.2.1 utils_4.0.4 vctrs_0.3.6 viridis_0.5.1
#> viridisLite_0.3.0 withr_2.4.1
Created on 2021-05-11 by the reprex package (v1.0.0)
解决方案
警告消息给出了提示:
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.
xlim()
并ylim()
替换图中的现有比例,而不是修改其限制。
下面的代码使用expand_limits()
修改了图中已经存在的比例限制。(如果需要容纳所有观察结果,规模将增长到超过要求的限制的额外优势。)
p + scale_x_continuous(breaks = seq(70, 105, by = 5)) +
expand_limits(x = c(70, 105))
推荐阅读
- python - 在 Python 3 中对两个压缩的 NumPy 数组进行字符串格式化
- instagram - Instagram oEmbed - thumbnail_url 的 URL 签名不匹配
- javascript - HTML 5 - 带有画布的绘图应用程序不绘图
- nrpe - 有人可以告诉我如何在 icinga 2 中使用 check_nrpe 定义 check_disk 服务吗?
- php - ZoomIn ZoomOut 花式框
- python - 执行脚本时如何防止打开 HTML 本地文件?
- javascript - 从带有方括号的对象中删除引号
- python-3.x - 如何在 python 3 进程中定义超时?
- c# - 在给定索引处将行添加到 TableLayoutPanel
- php - 在 php 数组中格式化来自服务器的响应