簡體   English   中英

在對數刻度上繪制(帶有漸近線),ggplot2中為零

[英]plotting on logarithmic scale (with asymptotes) and Zero in ggplot2

我正在繪制具有漸近尾巴的劑量反應曲線。 我真的很想在顯示0的圖中包含車輛(對照)劑量

通常將0計算為.0000000001的劑量-在這些圖中常見的做法。

我真的很喜歡下面的圖像顯示該圖像的方法,該圖像是從pdf上獲取的,有關如何使用該程序進行繪圖的信息: GraphPad:PRISM

旁注:我已經找到了使用基本圖形而不是使用ggplot2的方法。

在此處輸入圖片說明

關於matlab提出了類似但不同的SE問題: 此處

我的R代碼如下:

library(ggplot2)
library(scales)

ggplot(df, aes(x=dose,y=probability, group=model))+

  geom_ribbon(aes(ymin=Lower,ymax=Upper,x=dose,
                  fill=model, col=model,alpha=2))+

  #Axis log transformation:
  annotation_logticks(scaled = TRUE,sides="b") +
  scale_x_log10(breaks = 10^(-1:10),
                labels = trans_format("log10", math_format(10^.x)))+
  #Axes labels:
  labs(x="dosage (log scale)", y="response",size=1)

數據:

df<-structure(list(dose = c(1.0000001, 1.04737100217022, 1.09698590648847,1.14895111335032, 1.20337795869652, 1.26038305255123, 1.32008852886009,1.38262230716338, 1.44811836666478, 1.51671703328309, 61.5098612858473,64.4236386159454, 67.4754441930906, 70.6718165392165, 74.0196039119089,77.525978976861, 81.1984541753771, 85.0448978198478, 89.073550951683,93.2930449978201, 97.7124202636365, 102.341145301888, 107.189137199173,112.266782823381, 117.584961077656, 123.155066208544, 128.98903221828,135.099358433491, 141.499136285126, 148.202077356965, 155.222542762819,162.575573915347, 6294.98902185499, 6593.18830115198, 6905.51354792318,7232.63392192496, 7575.25028161186, 7934.09668573241, 8309.9419660568,8703.59137460616, 9115.88830891252, 9547.71611900591, 10000),probability = c(0.000541224108467882, 0.000604351222894496,0.000674836364822755, 0.000753535950764922, 0.000841405774529429,0.000939512464066553, 0.00104904624066603, 0.00117133512397466,0.00130786074096225, 0.00146027591282991, 0.909137675722837,0.917853710549939, 0.925801889950727, 0.933037137930218,0.939612877653328, 0.945580543661554, 0.950989225589167,0.955885424480555, 0.960312903543462, 0.964312616478569,0.967922698169444, 0.971178504317829, 0.974112688435394,0.976755306362936, 0.979133940122965, 0.981273834388139,0.983198040151417, 0.984927561312444, 0.986481500855764,0.987877204102597, 0.989130397184622, 0.990255319432183,0.99999839386077, 0.999998561719608, 0.999998712035413, 0.999998846641614,0.999998967180029, 0.999999075120889, 0.99999917178077, 0.999999258338655,0.999999335850309, 0.999999405261159, 0.999999467417826),Lower = c(-0.000843143037924429, -0.000920390477371509, -0.00100418185380549,-0.0010949913193486, -0.001193314249806, -0.0012996656659587,-0.00141457798197374, -0.00153859794267898, -0.00167228258800459,-0.00181619405586472, 0.844594565230258, 0.856774199246587,0.868144364095382, 0.878738700796645, 0.888592208066634,0.897740830817022, 0.906221056637379, 0.914069534310229,0.92132272464159, 0.928016590318475, 0.934186328384788, 0.939866146372578,0.94508908115546, 0.949886858166262, 0.954289787671259, 0.958326694236121,0.962024875270718, 0.965410084524652, 0.968506536556353,0.971336928460753, 0.973922475470259, 0.976282957407176,0.999991663016519, 0.999992483777835, 0.999993224038698,0.999993891661212, 0.99999449374333, 0.999995036692715, 0.999995526293503,0.999995967766653, 0.999996365824493, 0.999996724720012,0.999997048291405), Upper = c(0.00192559125486019, 0.0021290929231605,0.002353854583451, 0.00260206322087845, 0.00287612579886485,0.0031786905940918, 0.00351267046330581, 0.0038812681906283,0.00428800406992909, 0.00473674588152455, 0.973680786215416,0.978933221853291, 0.983459415806071, 0.987335575063791,0.990633547240022, 0.993420256506086, 0.995757394540955,0.997701314650881, 0.999303082445333, 1.00060864263866, 1.0016590679541,1.00249086226308, 1.00313629571533, 1.00362375455961, 1.00397809257467,1.00422097454016, 1.00437120503212, 1.00444503810023, 1.00445646515518,1.00441747974444, 1.00433831889898, 1.00422768145719, 1.00000512470502,1.00000463966138, 1.00000420003213, 1.00000380162202, 1.00000344061673,1.00000311354906, 1.00000281726804, 1.00000254891066, 1.00000230587612,1.00000208580231, 1.00000188654425), model = c("mod3", "mod3","mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3","mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3","mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3","mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3","mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3","mod3")), .Names = c("dose", "probability", "Lower", "Upper","model"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L,102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 190L,191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 200L), class = "data.frame")

結果是:

在此處輸入圖片說明

編輯9/1/17:在下面,用戶dww還提供了一種使用R包plotrix在r基礎圖形中執行此操作的方法

這樣的事情怎么樣:

df$dose[1] <- 0.000001

df$facet <- ifelse(df$dose == min(df$dose), 1, 2)
ggplot(df, aes(x=dose,y=probability, group=model))+
  geom_point(data = subset(df, facet == 1)) +
  geom_ribbon(aes(ymin=Lower,ymax=Upper,x=dose,
                  fill=model, col=model,alpha=2))+

  #Axis log transformation:
  annotation_logticks(scaled = TRUE,sides="b") +
  scale_x_log10(breaks = c(0.000001, 10^(0:10)), 
                labels = c(0, math_format()(0:10))) +
  #Axes labels:
  labs(x="dosage (log scale)", y="response",size=1) +
  facet_grid(~facet, scales = 'free', space = 'free') +
  theme(strip.background = element_blank(),
        strip.text = element_blank())

在此處輸入圖片說明

您可以將plotrix用於斷軸:

library(plotrix)
plot(df$dose, df$probability, log='x', xlim=c(0.1,1e4), xaxt='n', col=NA)
axis(1, at=10^(-1:4), labels=c(0, 10^(0:4)))
axis.break(breakpos = 0.4)
polygon(c(df$dose, rev(df$dose)), c(df$Lower, rev(df$Upper)), col = rgb(0.8,0.1,0.1,0.5))
points(0.1,0, pch=19, cex=0.5)

在此處輸入圖片說明

分解如下:

  1. 我們在plot命令中使用xlim將軸啟動為比實際數據點低一個數量級。
  2. 我們在axis命令中將laels設置為將x軸上的最低點標記為零(即使實際上位於x = 0.1處)
  3. 然后,我們在前兩個標簽之間插入一個軸心中斷
  4. 最后,我們在(0.1,0)處繪制了一個點,該點似乎在(0,0)處,因為我們插入了軸中斷並將此處的軸標記為零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM