我撰寫了以下代碼來顯示四個圖
Scores <- as.factor(sampleXYPCA$PC1)
p1 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC1))
geom_point( ) scale_color_gradient(low="blue", high="red")
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) labs( x ="x (m) ", y = "y (m)")
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank(),
legend.position="right", legend.direction="vertical")
Scores <- as.factor(sampleXYPCA$PC2)
p2 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC2))
geom_point( ) scale_color_gradient(low="blue", high="red")
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) labs( x ="x (m) ", y = "y (m)")
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
Scores <- as.factor(sampleXYPCA$PC3)
p3 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC3))
geom_point( ) scale_color_gradient(low="blue", high="red")
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) labs( x ="x (m) ", y = "y (m)")
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
Scores <- as.factor(sampleXYPCA$PC4)
p4 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC4))
geom_point( ) scale_color_gradient(low="blue", high="red")
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) labs( x ="x (m) ", y = "y (m)")
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
figure <- ggarrange(p1, p2,p3,p4 font("x.text", size = 10),
ncol = 2, nrow = 2)
show(figure)

我有兩個問題要解決:
- 我想洗掉最后一個圖 (PC4) 中 x 軸的值,就像之前的圖一樣。
- 我想在所有圖的顏色條上設定相同的比例(從 -3,3)
為方便起見,我復制了我正在使用的資料框 (sampleXYPCA) 的第一行:
X_UTM_ Y_UTM_ PC1 PC2 PC3 PC4
1 6501395 1885718 -1.37289727 2.320717816 0.93434761 1.24571643
2 6500888 1885073 -1.22111900 4.021127182 1.89434320 1.26801802
3 6500939 1885241 -0.58212873 3.301443355 -1.79458946 0.63329006
4 6500965 1884644 -1.13872381 4.521231473 2.43925215 0.53962882
5 6501608 1884654 -0.24075643 5.871225725 0.69257238 0.89294843
6 6501407 1883939 -0.15938861 3.965081981 1.40970861 -0.77825417
7 6501581 1883630 -0.59187192 2.904278269 0.40655574 -1.66513966
uj5u.com熱心網友回復:
使用facet_wrap和添加用于可視化的航空底圖(繪制空間資料時的個人偏好):
#sample data as dput
dt <- structure(list(x = c(6501395, 6500888, 6500939, 6500965, 6501608,
6501407, 6501581), y = c(1885718, 1885073, 1885241, 1884644,
1884654, 1883939, 1883630), pca1 = c(-1.37289727, -1.221119,
-0.58212873, -1.13872381, -0.24075643, -0.15938861, -0.59187192
), pca2 = c(2.320717816, 4.021127182, 3.301443355, 4.521231473,
5.871225725, 3.965081981, 2.904278269), pca3 = c(0.93434761,
1.8943432, -1.79458946, 2.43925215, 0.69257238, 1.40970861, 0.40655574
), pca4 = c(1.24571643, 1.26801802, 0.63329006, 0.53962882, 0.89294843,
-0.77825417, -1.66513966)), class = "data.frame", row.names = c(NA,
-7L))
#load libraries
library(sf)
library(tidyr)
library(ggplot2)
library(ggspatial)
library(tmaptools)
#pivot_longer on PCA
dt <- pivot_longer(dt, cols = c("pca1", "pca2", "pca3", "pca4"), names_to = "PCA", values_to = "Score")
#convert to sf object (assumed that you use espg:32629, change to whatever you use as the coordinate system)
dt <- st_as_sf(dt, coords = c("x", "y"), crs = st_crs(32629))
#load a basemap
basemap <- read_osm(dt, type = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}", zoom = 15, ext = 1.2)
#plot
ggplot() layer_spatial(basemap) geom_sf(data = dt, aes(col = Score), size = 3) facet_wrap(~PCA) labs( x ="x (m) ", y = "y (m)")
theme_bw() theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
scale_x_continuous(expand = c(0.01,0.01))
scale_y_continuous(expand = c(0.01,0.01))
scale_color_gradient(low="blue", high="red")

uj5u.com熱心網友回復:
使用一些虛擬資料來說明可能的解決方案,這可能會有所幫助。
OP 問題中的問題似乎與呼叫ggarrange. 查看檔案?ggarrange
library(ggpubr)
library(ggplot2)
p1 <-
ggplot(mtcars, aes(mpg, wt))
geom_point()
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
figure <-
ggarrange(p1, p1, p1, p1,
font.label = list(size = 10),
ncol = 2,
nrow = 2)
show(figure)

由reprex 包(v2.0.1)于 2021 年 12 月 10 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381244.html
下一篇:多組的ggplot條形圖 折線圖
