2017-09-01 390 views
2

我使用ggplot2來製作一個簡單的圖形,但它有太多的空白。scale_x_discrete中x軸刻度線之間的距離太遠

Comparison graph

我想的兩個點是更靠近在一起,在x軸,使整體圖像較小。我使用scale_x_discrete用下面的代碼:

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)) 

回答

0

一種用於降低兩個誤差條之間的空白空間的方法是使用連續的變量x與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

+0

驚人!非常感謝。像魅力一樣工作。 –