簡體   English   中英

在多級ggplot2圖中用標簽替換圖例

[英]Replacing legend with labels in multilevel ggplot2 plot

我創建了這張圖表。 標簽占用太多空間,很難看到哪個狀態。 因此,我想用圖形或波紋管x軸內的州代碼標簽代替圖例。 是否有捷徑可尋?

圖表: 在此處輸入圖片說明

生成它的代碼:

url <- 'https://www.dropbox.com/s/f046jroutvt8ctk/SO_example_data_put_labels_in_graph.csv?raw=1'
d <- read_csv(url)

d %>% 
  ggplot(aes(x=popNb,y=tx_atendimento)) +
  geom_rect(aes(xmin=pop1b,xmax=popNb,
                ymin=tx0,ymax=tx_atendimento,
                fill=UF)) +
  geom_segment(aes(x=pop1b,xend=popNb,
                   y=tx_atendimento_UF,yend=tx_atendimento_UF)) +
  theme(legend.position = "bottom", legend.direction = "horizontal" ) +
  ggtitle('Daycare provision rate per state and municipality in Brazil (2014)') +
  ggsave('plot_rec_bar_needs_labels.png')

說明:

請注意,這不是正常的條形圖。 每個自治市(數據集中的行)均由一個矩形表示,其寬度對應於該年齡段的兒童人數,高度對應於供應率。 我創建了x值,因此首先按州(UF)的平均供給率來安排市,然后按州的市(cod_mun6)中的相同率來安排市。 我還添加了顯示每個城市平均值的條形圖,盡管這些數據僅在每個城市的首次觀察中可用。

因此,此圖將市政和州級別的信息混合在一起(盡管它們只是在一個data.frame中被“非規范化”了)。

我知道可以通過以下方式刪除圖例: + theme(legend.position = "none", )我嘗試使用以下方式添加標簽:

  • + geom_label(aes(x=mean_popNb_uf,label=UF2), nudge_y =.4,label.size = 0.05 )
  • + geom_text(aes(label=UF2))

但是結果標簽看起來很混亂,而且定位看起來很奇怪。

我還嘗試了ggrepel軟件包中的geom_text_repel(aes(label=UF2)) ,但未顯示任何標簽。 過去,我過去使用過包directlabels ,但在這種情況下不知道如何使用它。

最好的方法可能是將facet_wrap設置為1行以分離出狀態。 請注意,似乎某些狀態缺少數據,因此需要將其過濾掉(否則,當嘗試為空圖設置x軸限制時, facet_wrap失敗:

d %>% 
  filter(!is.na(pop1b)
         , !is.na(popNb)) %>%
  ggplot(aes(x=popNb,y=tx_atendimento)) +
  geom_rect(aes(xmin=pop1b,xmax=popNb,
                ymin=tx0,ymax=tx_atendimento
                )) +
  geom_segment(aes(x=pop1b,xend=popNb,
                   y=tx_atendimento_UF,yend=tx_atendimento_UF)) +
  theme(axis.text.x = element_blank()) +
  ggtitle('Daycare provision rate per state and municipality in Brazil (2014)') +
  facet_wrap(~UF, scales = "free_x", nrow = 1, switch = "x") +
  theme_minimal()

請注意,如果要更改排序,則需要將UF列的因子級別設置為所需的任何順序。

劇情

如果需要顯示狀態的“大小”,可以使用facet_grid其中space = "free"例如

d %>% 
  filter(!is.na(pop1b)
         , !is.na(popNb)) %>%
  ggplot(aes(x=popNb,y=tx_atendimento)) +
  geom_rect(aes(xmin=pop1b,xmax=popNb,
                ymin=tx0,ymax=tx_atendimento
                )) +
  geom_segment(aes(x=pop1b,xend=popNb,
                   y=tx_atendimento_UF,yend=tx_atendimento_UF)) +
  ggtitle('Daycare provision rate per state and municipality in Brazil (2014)') +
  # facet_wrap(~UF, scales = "free_x", nrow = 1, switch = "x") +
  facet_grid(~UF, scales = "free_x", switch = "x", space = "free") +
  theme_minimal() +
  theme(axis.text.x = element_blank()
        , panel.margin.x = unit(0,"in"))

但是請注意,如果某些狀態太窄而無法容納標簽,則可能需要填充一些狀態。

在此處輸入圖片說明

我繼續並添加了代碼以將所有狀態填充到任意所需的寬度並對值進行排序:

library(dplyr)
library(ggplot2)
library(magrittr)
url <- 'https://www.dropbox.com/s/f046jroutvt8ctk/SO_example_data_put_labels_in_graph.csv?raw=1'
# d <- read.csv(url)

desiredWidth <- 350000

toPlot <-
  d %>%
  filter(!is.na(pop1b)
         , !is.na(popNb)
         , !is.na(UF)) %>%
  split(.$UF) %>%
  lapply(function(thisState){
    # thisState <- d %>% filter(UF == "AC")
    # Find current range:
    currRange <-
      thisState %>%
      {max(.$popNb, na.rm = TRUE) -
          min(.$pop1b, na.rm = TRUE)}

    spacing <- (desiredWidth - currRange)/2

    # Add the spacing
    temp <- thisState[1:2,]
    temp$pop1b <-
      c(min(thisState$pop1b, na.rm = TRUE) - spacing
        , max(thisState$popNb, na.rm = TRUE) + 1
        )
    temp$popNb <-
      c(min(thisState$pop1b, na.rm = TRUE) - 1
        , max(thisState$popNb, na.rm = TRUE) + spacing
      )
    temp$tx_atendimento <- 0
    return(rbind(thisState , temp))
  }) %>%
  bind_rows %>%
  filter(!is.na(UF)) %>%
  droplevels

# summary values
sumVal <-
  toPlot %>%
  group_by(UF) %>%
  summarise(sumVal = tx_atendimento_UF[1])

# Sort the states:
toPlot$UF <-
  factor(
    toPlot$UF
    , levels = as.character(sumVal$UF)[order(sumVal$sumVal)]
  )


toPlot %>% 
  ggplot(aes(x=popNb,y=tx_atendimento)) +
  geom_rect(aes(xmin=pop1b,xmax=popNb,
                ymin=tx0,ymax=tx_atendimento
                )) +
  geom_segment(aes(x=pop1b,xend=popNb,
                   y=tx_atendimento_UF,yend=tx_atendimento_UF)) +
  ggtitle('Daycare provision rate per state and municipality in Brazil (2014)') +
  # facet_wrap(~UF, scales = "free_x", nrow = 1, switch = "x") +
  facet_grid(~UF, scales = "free_x", switch = "x", space = "free") +
  theme_minimal() +
  theme(axis.text.x = element_blank()
        , panel.margin.x = unit(0,"in"))

在此處輸入圖片說明

暫無
暫無

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

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