Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space between x-axis tick marks in scale_x_discrete too far apart

Tags:

scale

ggplot2

I am using ggplot2 to make a simple graph, but it has too much white space.

Comparison graph

I want the two points to be closer together on the x-axis so the overall image is smaller. I am using scale_x_discrete with the following code:

ggplot(box, aes(x=Garden, y=Fitness)) + 
geom_errorbar(aes(ymin=(Fitness-Error), ymax=(Fitness+Error)), 
colour="black", width=.05) +
geom_line() +
geom_point(size=6)+ 
theme(panel.background = element_rect(fill =  
'white'),axis.text=element_text(size=22), 
axis.title=element_text(size=28,face="bold"),legend.key = element_rect(fill = "white"), plot.title = element_text(size=30,face="bold", hjust=0.5),axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)), axis.ticks.length = unit(0, "lines"),legend.text=element_text(size=24),legend.title=element_text(size=26), legend.key.size = unit(1.5, 'lines'))+
ggtitle("Fitness Comparison") + labs(y="Ranked Fitness", x = "Common Garden") +
scale_x_discrete(labels=c("Warm" = "Warm Limit", "Cool" = "Cool Limit"), expand=c(0.2, 0))
like image 383
Jackie Shay Avatar asked Sep 01 '17 19:09

Jackie Shay


People also ask

How do you set the X axis ticks?

Specify x-Axis Tick Values for Specific AxesCall the nexttile function to create the axes objects ax1 and ax2 . Plot random data into each axes. Then set the x-axis tick values for the lower plot by passing ax2 as the first input argument to the xticks function.

Are labeled tick marks required on a graph?

Tick marks are used to indicate a reference value at a given point in a chart. Tick marks function similar to the lines on a ruler – not all tick marks need to be labeled, but they do need to establish a continuous interval by ensuring the number of tick marks between each labeled tick mark is always the same.


1 Answers

A way for reducing the white space between the two error bars is to use a continuous x variable with scale_x_continuous.

box <- data.frame(Garden=c("Cool","Warm"),
                  Fitness=c(2980,2050),
                  Error=c(50,50))

# Convert Garden from factor to numeric  
box$Garden <- as.numeric(box$Garden)

ggplot(box, aes(x=Garden, y=Fitness)) + 
geom_errorbar(aes(ymin=(Fitness-Error), ymax=(Fitness+Error)), colour="black", width=.05, lwd=1) +
geom_point(size=6)+ 
theme(panel.background = element_rect(fill='white'), 
      axis.text=element_text(size=22), 
      axis.title=element_text(size=28,face="bold"),
      legend.key=element_rect(fill = "white"), 
      plot.title=element_text(size=30,face="bold", hjust=0.5),
      axis.title.y=element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)), 
      axis.ticks.length=unit(0, "lines"),
      legend.text=element_text(size=24),
      legend.title=element_text(size=26),
      legend.key.size = unit(1.5, 'lines')) +
      ggtitle("Fitness Comparison") + 
      labs(y="Ranked Fitness", x = "Common Garden") +
      scale_x_continuous(labels=c("Cool Limit","Warm Limit"), 
                         breaks=c(1,2), limits=c(0,3), expand=c(0,0))

enter image description here

like image 138
Marco Sandri Avatar answered Nov 11 '22 00:11

Marco Sandri