簡體   English   中英

躲避ggplot2中的列

[英]dodge columns in ggplot2

我正在嘗試創建一張匯總我的數據的圖片。 有關來自不同國家的不同實踐獲得的毒品使用率的數據。 每種做法都貢獻了不同數量的數據,我希望在我的圖片中顯示所有這些信息。

這是要處理的數據的子集:

gr<-data.frame(matrix(0,36))
gr$drug<-c("a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","b","b","b","b","b","b","b","b","b","b","b","b")
gr$practice<-c("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r")
gr$country<-c("c1","c1","c1","c1","c1","c1","c1","c1","c1","c1","c2","c2","c2","c2","c2","c2","c3","c3","c1","c1","c1","c1","c1","c1","c1","c1","c1","c1","c2","c2","c2","c2","c2","c2","c3","c3")
gr$prevalence<-c(9.14,5.53,16.74,1.93,8.51,14.96,18.90,11.18,15.00,20.10,24.56,22.29,19.41,20.25,25.01,25.87,29.33,20.76,18.94,24.60,26.51,13.37,23.84,21.82,23.69,20.56,30.53,16.66,28.71,23.83,21.16,24.66,26.42,27.38,32.46,25.34)
gr$prop<-c(0.027,0.023,0.002,0.500,0.011,0.185,0.097,0.067,0.066,0.023,0.433,0.117,0.053,0.199,0.098,0.100,0.594,0.406,0.027,0.023,0.002,0.500,0.011,0.185,0.097,0.067,0.066,0.023,0.433,0.117,0.053,0.199,0.098,0.100,0.594,0.406)
gr$low.CI<-c(8.27,4.80,12.35,1.83,7.22,14.53,18.25,10.56,14.28,18.76,24.25,21.72,18.62,19.83,24.36,25.22,28.80,20.20,17.73,23.15,21.06,13.12,21.79,21.32,22.99,19.76,29.60,15.41,28.39,23.25,20.34,24.20,25.76,26.72,31.92,24.73)
gr$high.CI<-c(10.10,6.37,22.31,2.04,10.00,15.40,19.56,11.83,15.74,21.52,24.87,22.86,20.23,20.68,25.67,26.53,29.86,21.34,20.21,26.10,32.79,13.63,26.02,22.33,24.41,21.39,31.48,17.98,29.04,24.43,22.01,25.12,27.09,28.05,33.01,25.95)

我寫的代碼是這樣

p<-ggplot(data=gr, aes(x=factor(drug), y=as.numeric(gr$prevalence), ymax=max(high.CI),position="dodge",fill=practice,width=prop))
colour<-c(rep("gray79",10),rep("gray60",6),rep("gray39",2))
p + theme_bw()+
  geom_bar(stat="identity",position = position_dodge(0.9)) +
  labs(x="Drug",y="Prevalence") + 
  geom_errorbar(ymax=gr$high.CI,ymin=gr$low.CI,position=position_dodge(0.9),width=0.25,size=0.25,colour="black",aes(x=factor(drug), y=as.numeric(gr$prevalence), fill=practice)) +
  ggtitle("Drug usage by country and practice") +
  scale_fill_manual(values = colour)+ guides(fill=F)

我得到的圖是這樣一個圖,其中的條形圖相互重疊,而我希望它們“躲閃”。

在此處輸入圖片說明

我還收到以下警告:

未定義ymax:改為使用y來調整位置警告消息:position_dodge需要不重疊的x間隔

理想情況下,我會使每個小節彼此接近,並且它們的錯誤小節位於其小節的中間,並按國家/地區進行組織。

我還應該擔心警告(我顯然不完全理解該警告)嗎?

我希望這是有道理的。 我希望我足夠親近,但是我似乎什么都不會走,因此,不勝感激。

謝謝

ggplot的geom_bar()接受width參數,但是默認情況下,它們不會以躲避的位置整齊地排列在一起。 以下變通辦法在此處引用了解決方案:

library(dplyr)

# calculate x-axis position for bars of varying width
gr <- gr %>%
  group_by(drug) %>%
  arrange(practice) %>%
  mutate(pos = 0.5 * (cumsum(prop) + cumsum(c(0, prop[-length(prop)])))) %>%
  ungroup()

x.labels <- gr$practice[gr$drug == "a"]
x.pos <- gr$pos[gr$drug == "a"]

ggplot(gr,
       aes(x = pos, y = prevalence, 
           fill = country, width = prop,
           ymin = low.CI, ymax = high.CI)) +
  geom_col(col = "black") +
  geom_errorbar(size = 0.25, colour = "black") +
  facet_wrap(~drug) +
  scale_fill_manual(values = c("c1" = "gray79",
                               "c2" = "gray60",
                               "c3" = "gray39"),
                    guide = F) +
  scale_x_continuous(name = "Drug",
                     labels = x.labels,
                     breaks = x.pos) +
  labs(title = "Drug usage by country and practice", y = "Prevalence") +
  theme_classic()

情節

您要在此處傳達很多信息-為了使用國家/地區對毒品A和毒品B進行對比,並計算比例,您可以使用facet_grid函數。 嘗試這個:

      colour<-c(rep("gray79",10),rep("gray60",6),rep("gray39",2))




      gr$drug <- paste("Drug", gr$drug)
      p<-ggplot(data=gr, aes(x=factor(practice), y=as.numeric(prevalence), 
                             ymax=high.CI,ymin = low.CI, 
                             position="dodge",fill=practice, width=prop))


        p + theme_bw()+ facet_grid(drug~country, scales="free")  +
        geom_bar(stat="identity") +
        labs(x="Practice",y="Prevalence") + 
        geom_errorbar(position=position_dodge(0.9), width=0.25,size=0.25,colour="black") +
        ggtitle("Drug usage by country and practice") +
        scale_fill_manual(values = colour)+ guides(fill=F)

在此處輸入圖片說明

在C1國家/地區,寬度太小,正如您所指出的,一家診所很有影響力。

另外,您可以使用ggplot(aes(...))來指定外觀,而不必重置它,並且不需要在ggplot調用的aes函數中包括數據框對象名稱。

暫無
暫無

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

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