2017-08-11 124 views
0

因此,我已經比較了兩個組與第三個使用一系列輸入。對於三個組中的每一個,我都有一個值和一個輸入範圍的置信區間。對於這兩個比較,我也有這個範圍輸入的p值。現在我想繪製所有五個數據系列,但是使用第二個軸作爲p值。如何指出每個圖來糾正y軸(許多情節,兩個y軸,在R與ggplot2)

我能夠做到這一點,除了一件事:我如何確保R知道哪些圖分配給第二軸?

這就是現在的樣子。底部的兩個數據系列應該放大到Y軸的右側。

ggplot(df) + 
    geom_pointrange(aes(x=x, ymin=minc, ymax=maxc, y=meanc, color="c")) + 
    geom_pointrange(aes(x=x, ymin=minb, ymax=maxb, y=meanb, color="b")) + 
    geom_pointrange(aes(x=x, ymin=mina, ymax=maxa, y=meana, color="a")) + 
    geom_point(aes(x=x, y=c, color="c")) + 
    geom_point(aes(x=x, y=b, color="b")) + 
    scale_y_continuous(sec.axis = sec_axis(~.*0.2)) 

df是一個數據幀,其列名是上面列出的所有變量,所有行值都是相應的數據點。

enter image description here

+1

您可以擴展的P值/ 0.2 –

+0

@SRivero哈,我像那個想法一樣。現在會使用它。雖然我會認爲有一種方法可以給每個繪圖一個定義它的單位或y軸的屬性,以便您不需要改變數據值就可以繪製它。 – Leo

+3

不使用ggplot。 https://stackoverflow.com/questions/3099219/plot-with-2-y-axes-one-y-axis-on-the-left-and-otherother-y-axis-on-the-right –

回答

2

你可以得到你想要的東西,堅守哈德利的大炮圖形福音的語法,如果你從廣角轉換您的DF長,並採用不同的AES(即形狀,顏色,填充)手段和CI之間。

你沒有提供一個可重複的例子,所以我僱傭了我自己的。 (Dput在帖子的末尾)

df2 <- df %>% 
     mutate(CatCI = if_else(is.na(CI), "", Cat)) # Create a categorical name to map the CI to the legend. 

ggplot(df2, aes(x = x)) + 
     geom_pointrange(aes(ymin = min, ymax = max, y = mean, color = Cat), shape = 16) + 
     geom_point(data = dplyr::filter(df2,!is.na(CI)), ## Filter the NA within the CI 
      aes(y = (CI/0.2), ## Transform the CI's y position to fit the right axis. 
      fill = CatCI), ## Call a second aes the aes 
      shape = 25, size = 5, alpha = 0.25) + ## I changed shape, size, and fillto help with visualization 
     scale_y_continuous(sec.axis = sec_axis(~.*0.2, name = "P Value")) + 
     labs(color = "Linerange\nSinister Axis", fill = "P value\nDexter Axis", y = "Mean") 

結果:

![enter image description here

數據框:

df <- structure(list(Cat = c("a", "b", "c", "a", "b", "c", "a", "b", 
"c", "a", "b", "c", "a", "b", "c"), x = c(2, 2, 2, 2.20689655172414, 
2.20689655172414, 2.20689655172414, 2.41379310344828, 2.41379310344828, 
2.41379310344828, 2.62068965517241, 2.62068965517241, 2.62068965517241, 
2.82758620689655, 2.82758620689655, 2.82758620689655), mean = c(0.753611797661977, 
0.772340941644911, 0.793970086962944, 0.822424652072316, 0.837015408776649, 
0.861417383841253, 0.87023105762465, 0.892894201949377, 0.930096326498796, 
0.960862178366363, 0.966600321596147, 0.991206984637544, 1.00714201832596, 
1.02025006679944, 1.03650896186786), max = c(0.869753641121797, 
0.928067675294351, 0.802815304215019, 0.884750162053761, 1.03609814491961, 
0.955909854315582, 1.07113399603486, 1.02170928767791, 1.05504846273091, 
1.09491706586801, 1.20235615364205, 1.12035782960649, 1.17387406039167, 
1.13909154635088, 1.0581878034897), min = c(0.632638511783381, 
0.713943701135991, 0.745868763626567, 0.797491261486603, 0.743382797144923, 
0.827693203320894, 0.793417962991821, 0.796917421637021, 0.92942504556723, 
0.89124101157585, 0.813058838839382, 0.91701749675892, 0.943744642652422, 
0.912869230576973, 0.951734254896252), CI = c(NA, 0.164201137643034, 
0.154868406784159, NA, 0.177948094206453, 0.178360305763648, 
NA, 0.181862670931493, 0.198447350829814, NA, 0.201541499248143, 
0.203737532636542, NA, 0.205196077692786, 0.200992205838595), 
    CatCI = c("", "b", "c", "", "b", "c", "", "b", "c", "", "b", 
    "c", "", "b", "c")), .Names = c("Cat", "x", "mean", "max", 
"min", "CI", "CatCI"), row.names = c(NA, 15L), class = "data.frame")