Home > Software engineering >  How to remove space above y axis and to the right of x axis?
How to remove space above y axis and to the right of x axis?

Time:02-15

I have the following df

    df <- data.frame(x = c(rep(seq(1:3),3)), y = c(rep(1,3),rep(2,3),rep(3,3)),
l = c(rep("1 km",3),rep("10 km",3),rep("30 km",3)),
m = c(rep(c("urban","semi urban","rural"),3)),
p = c(2.3,2.2,3.3,3.3,3.5,3.9,3.6,3.5,6.6),
q = c("Cargo Bike","Diesel Van", "Electric Van", "Single Delivery AV", "Multiple Delivery AV","Cargo Bike","Diesel Van","Diesel Van", "Electric Van"))

I am trying to make a graph with 3x3 regions (i.e. a total of 9 boxes). In each region, I need the value of 'p' and the text 'q'. I want the box to be closed.

 amaze <- ggplot(df, aes(x-0.5,y-0.5))  
           geom_text(aes(label = q, color = factor(q)), nudge_y = 0.1, check_overlap = TRUE, show.legend = FALSE) 
           geom_text(aes(label = p, colour = factor(q)), show.legend = FALSE) 
           geom_label(aes(label = l), nudge_x = -0.4,nudge_y = 0.4) 
           geom_label(aes(label = m), nudge_x = 0.35,nudge_y = -0.4)  
           xlab("Density of Deliveries")   ylab("Distance from warehouse to first delivery")
         
         
         amaze   geom_vline(xintercept = c(1,2,3))   geom_hline(yintercept = c(1,2,3))  
           #theme(panel.border = element_rect(color = "black", size = 2))  
           scale_x_continuous(limits = c(0,3)) 
           scale_y_continuous(limits = c(0,3))   theme_bw() 
           theme(axis.ticks = element_blank(), axis.text.y = element_blank(), axis.text.x = element_blank())
             

This is what I get:

enter image description here

I would like to remove all space above yaxis and to the right of and below x axis (so there is a box of 3 by 3 precisely). How can I do that?

thanks in advance for your help!

CodePudding user response:

Since you want three grids per plane, you should only use two yintercept in geom_vline (you can keep it for aesthetic reason, but it's not necessary).

Also, set expand = c(0, 0) in both scale_x_continuous and scale_y_continuous helps remove the padding.

library(ggplot2)

ggplot(df, aes(x-0.5,y-0.5))  
  geom_text(aes(label = q, color = factor(q)), nudge_y = 0.1, check_overlap = TRUE, show.legend = FALSE) 
  geom_text(aes(label = p, colour = factor(q)), show.legend = FALSE) 
  geom_label(aes(label = l), nudge_x = -0.4,nudge_y = 0.4) 
  geom_label(aes(label = m), nudge_x = 0.35,nudge_y = -0.4)  
  xlab("Density of Deliveries")   
  ylab("Distance from warehouse to first delivery")   
  geom_vline(xintercept = c(1,2))   geom_hline(yintercept = c(1,2))  
  scale_x_continuous(limits = c(0,3), expand = c(0, 0)) 
  scale_y_continuous(limits = c(0,3), expand = c(0, 0))   
  theme_bw() 
  theme(axis.ticks = element_blank(), axis.text.y = element_blank(), 
        axis.text.x = element_blank())

expand_0

CodePudding user response:

You could add the expand argument to the call to scale_continuous

amaze   geom_vline(xintercept = c(1,2,3))   geom_hline(yintercept = c(1,2,3))  
  #theme(panel.border = element_rect(color = "black", size = 2))  
  scale_x_continuous(limits = c(0,3), expand= c(0,0))              ### here
  scale_y_continuous(limits = c(0,3), expand= c(0,0))              ### here
  theme_bw() 
  theme(axis.ticks = element_blank(), axis.text.y = element_blank(), axis.text.x = element_blank())
  • Related