2017-10-16 141 views
1

我想在Rstudio中使用ggplot2的對數刻度作圖,最後x軸刻度標籤將不適合圖形。x軸刻度標籤不適合在Rstudio中使用ggplot2

這裏是我的繪圖代碼

ggplot(data=user.counts, aes(x=counter, y=Number_obs)) + 
geom_line()+ 
scale_y_log10(breaks=c(1,10,100,1000,10000),labels=c(1,10,100,1000,10000))+ 
scale_x_log10(breaks=c(1,10,100,1000,10000),labels=c(1,10,100,1000,10000))+ 
labs(x="No. observers",y="No. observations",title="")+ 
theme_bw(base_size = 20) 

,這是我圖的截圖:

graph

我試圖使在RStudio情節窗格更大,飛出情節窗格和兩種方法沒有什麼區別。

非常感謝

+0

是否有在可視範圍內的任何數據1000和10000個觀察者之間(沒有觀察。)? –

+0

在'theme_bw()'中嘗試一個更小的'base_size'。 – neilfws

回答

0

選項1。右對齊x軸標籤:

ggplot(data=user.counts, aes(x=counter, y=Number_obs)) + 
    geom_line()+ 
    scale_y_log10(breaks=c(1,10,100,1000,10000), labels=c(1,10,100,1000,10000))+ 
    scale_x_log10(breaks=c(1,10,100,1000,10000), labels=c(1,10,100,1000,10000))+ 
    labs(x="No. observers",y="No. observations",title="")+ 
    theme_bw(base_size = 20) + 
    theme(axis.text.x = element_text(hjust = 1)) # default is hjust = 0.5 

plot1

選項2。增加膨脹常數:使用

ggplot(data=user.counts, aes(x=counter, y=Number_obs)) + 
    geom_line()+ 
    scale_y_log10(breaks=c(1,10,100,1000,10000), labels=c(1,10,100,1000,10000))+ 
    scale_x_log10(breaks=c(1,10,100,1000,10000), labels=c(1,10,100,1000,10000), 
       expand = c(0.1, 0))+ # default is c(0.05, 0) for continuous variable 
    labs(x="No. observers", y="No. observations", title="")+ 
    theme_bw(base_size = 20) 

plot2

樣本數據:

set.seed(1) 
user.counts <- data.frame(
    counter = seq(1, 1000), 
    Number_obs = seq(1000, 1) * 10 + rnorm(1000) 
) 
相關問題