Home > Enterprise >  Remove x-labels from the last plot in ggplot
Remove x-labels from the last plot in ggplot

Time:12-10

I have written the following code to show four plots

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)

enter image description here

I have two issues that I am trying to fix:

  1. I want to remove the values at x-axis at the last plot (PC4), as in the previous plots.
  2. I want to set the same scale at the colour bar for all plots (from -3,3)

For convenience, I copy the first lines of the dataframe (sampleXYPCA) that I am using:

     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

CodePudding user response:

Using some dummy data to illustrate a possible solution, this may help.

The issue in the OP's question seems to be with the call to ggarrange. Check out the documentation with ?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)

Created on 2021-12-10 by the plot

  • Related