最好的方法可能是使用facet_wrap
設置爲1行來分隔出狀態。請注意,似乎有些州的丟失數據,因此他們需要被過濾掉(否則facet_wrap
嘗試設置爲空地塊x軸限制時失敗:
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
列到任何順序您希望他們在的因子水平。
![the plot](https://i.stack.imgur.com/LX2eJ.png)
如果你想它顯示的狀態的「大小」,你可以使用,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"))
但請注意,如果標籤太窄而無法貼標籤,則可能需要填充一些狀態。
![enter image description here](https://i.stack.imgur.com/amhkS.png)
我繼續添加代碼墊所有國家的一個任意所需的寬度和值進行排序:
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"))
![enter image description here](https://i.stack.imgur.com/8YeUm.png)
只是偶然回到這一點 - 如果我在下面的內容仍然不是你正在尋找的東西,我很想知道它缺少的是什麼。這是一個有趣的教自己一些'ggplot2'的東西,所以我可能會很想深入挖掘。 –