2017-05-15 60 views
0

我有一個簡單的ggplot,我需要合併一個對數刻度y軸英寸的麻煩。我明白,ggplot是正確的彎曲線一旦我的軸有對數刻度,但我需要線仍然線性連接我的數據點。ggplot對數刻度y軸直線

這是我的代碼:

forexample<-transform(example, EXP=factor(EXP, levels=unique(EXP))) 
plot<-ggplot(forexample, aes(x=EXP, y=concentration, shape=sample)) 
+ stat_summary(aes(group = sample), fun.y = mean, geom = 'line', alpha=1, size=0.5) 
+ stat_summary(aes(group = sample), fun.y = mean, geom = 'point', alpha=1, size=4) + 
theme_bw() + 
coord_trans(y = "log10") 

我的數據結構是這樣的:

sample concentration EXP 
H  0.08   Ex1 
H  0.07   Ex2 
M  2.00   Ex1 
M  0.50   Ex2 
R  0.01   Ex1 

...

我試圖Zoltáns建議在這個問題:「GGPLOT2登錄Y的規模軸導致曲線「,但它沒有爲我工作。 (ggplot2 log scale of y axis causing curved lines

如果有人能幫助我,我真的很高興! 謝謝:)

enter image description here

回答

2

這是coord_trans預期的行爲,是從scale_y_log10不同。另請參見:https://stackoverflow.com/a/25257463/3330437

require(dplyr) # for data construction 
require(scales) # for modifying the y-axis 

data_frame(x = rep(letters, 3), 
      y = rexp(26*3), 
      sample = rep(c("H", "M", "R"), each = 26)) %>% 
    ggplot(aes(x, y, shape = sample)) + theme_bw() + 
    geom_point() + geom_path(aes(group = sample)) + 
    scale_y_log10() 

enter image description here

如果你想y軸標籤和網格線看起來更像coord_trans默認設置,使用scale_y_log10(breaks = scales::pretty_breaks())

enter image description here

+0

非常感謝你:) – Pauline