2016-07-22 167 views
0

我想創建一個帶有日誌x軸但x標記不在日誌中的ggplot2的繪圖。要明確:R和ggplot2:scale_x_continuous(trans =「log」)帶有不在日誌中的刻度標記

假設有一些x值10.因爲我使用scale_x_continuous(trans = "log")它將在x軸上顯示爲2.3(log(10) = 2.302585)。而是我想的是,10號出現,因爲我只用scale_x_continuous(trans = "log")爲了提高2張密度圖,是非常扭曲的可比性。

+1

您可以通過'labels'參數直接歪曲x軸刻度標籤。 – hrbrmstr

+0

@hrbrmstr:你可以舉例說明如何在'scale_x_continuous'中指定'labels'選項嗎? –

+0

http://stackoverflow.com/search?q=user%3A1457051+%5Br%5D+scale_x_continuous+label – hrbrmstr

回答

0

還是有點在你的真實意圖混淆。也許這會幫助你看到在ggplot2的封面下面發生了什麼。 !另外,log()trans="log" == log10()trans="log10"

library(ggplot2) 
library(tibble) 
library(gridExtra) 

set.seed(1492) 
df <- data_frame(x=abs(rnorm(10) * 100) + 1, 
       y=seq(0, 1, length.out=10)) 

gg <- ggplot(df, aes(x, y)) + geom_point() 
gg <- gg + geom_vline(xintercept=10) 

gg1 <- gg + scale_x_continuous() 
gg2 <- gg + scale_x_continuous(breaks=c(10, 50, 100, 150)) 
gg3 <- gg + scale_x_continuous(trans="log") 
gg4 <- gg + scale_x_continuous(trans="log", breaks=c(10, 50, 100, 150)) 

grid.arrange(
    gg1, 
    gg2, 
    gg3, 
    gg4, 
    ncol=1 
) 

enter image description here

+0

非常感謝您的示例和耐心。之前,我沒有意識到刻度標籤總是指實際的刻度(而不是日誌中的刻度)。我來到Stata,你必須明確告訴程序來做到這一點。 –