2016-01-15 68 views
5

我試圖用一個國家的同一迴歸的估計和置信區間生成一個圖。我使用dplyrgroup_by(country)運行迴歸,然後我將所有結果彙總到broomtidy()數據框中。ggplot2:geom_pointrange()facet_grid()與coord_flip()和自由標度

當創建從這個數據幀(稱爲bycountry1)的圖形,我運行下面的代碼:

ggplot(bycountry1, aes(x = country, y = estimate, ymin = estimate - std.error * 2, ymax = estimate + std.error * 2)) + 
    geom_hline(yintercept = 0, colour = "black", lty = 2) + 
    geom_pointrange() + 
    coord_flip() + facet_grid(. ~ term, scales = "free") 

here is the graph I get

這就是我想要的,但我想有每個盒子的比例要有所不同,以便所有盒子看起來更像religious1盒子。由於這是變異性最大的那個,因此它支配比例尺,然後在其他大多數盒子中都看不到方差。正如上面的代碼所示,我確實在facet_grid()中指出了scales = "free",並且我嘗試了所有變體,也使用了facet_wrap(),但我無法使其工作。

+2

如何使用'geom_errorbarh',而不是'coord_flip'得到的水平誤差棒? – aosmith

+0

這是個好主意。謝謝!之前我曾嘗試過,看起來不太好,但我剛剛看到您可以修改'geom_errorbarh'的'height',並添加一個'geom_point'來獲得我正在尋找的東西。我現在會給你的建議添加一個答案。再次感謝! –

回答

3

根據aosmith的建議,我使用geom_errorbarh並刪除了coord_flip()。我還必須將geom_errorbarhheight設置爲0,併爲估算添加一個geom_point。下面是代碼:

ggplot(bycountry1, aes(y = country, x = estimate, xmin = estimate - std.error * 2, xmax = estimate + std.error * 2)) + 
    geom_vline(xintercept = 0, colour = "black", lty = 2) + 
    geom_point() + 
    geom_errorbarh(height = 0) + 
    facet_grid(. ~ term, scales = "free") 

而產生的圖像

enter image description here

相關問題