清理美国县级别的等值线

时间:2017-12-13 17:07:26

标签: r ggplot2 choropleth

我已经为美国县级数据编写了一个等值线,但是在绘制它时发现它很难读。县可以变得非常拥挤并开始相互融合 - 很难分辨出一个州的结局和另一个州的开始。有没有人知道按县数据绘制等值线图并按州创建边界轮廓的方法?这是我到目前为止复制的代码:

library(ggplot2)
library(fiftystater)
library(colorplaner)
library(USAboundaries)
library(sf)

usc <- us_counties(resolution = c("low", "high"), states = NULL)

st_list = c("Alabama", "Arizona", "Arkansas", "California", "Colorado", 
"Connecticut", "Delaware", "Florida", "Georgia", "Idaho", "Illinois", 
"Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", 
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", 
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New 
Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", 
"Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", 
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West 
Virginia", "Wisconsin", "Wyoming")

plot_counties <- us_counties(states=st_list, resolution = 'high')


plot_counties$dumb <- runif(nrow(plot_counties), 1,100)

ggplot(plot_counties) + geom_sf(aes(fill=dumb)) + 
  scale_x_continuous(breaks = NULL) + 
  scale_y_continuous(breaks = NULL) +
  scale_fill_gradientn("Title",
                       colours = c('darkred', 'tomato1', 'cyan2', 
                       'darkblue'), 
                       values = c(0,.5,.5000001, 1)) + 
  theme(panel.background = element_blank(), axis.ticks = element_blank(), 
        axis.text = element_blank()) 

1 个答案:

答案 0 :(得分:1)

这是一个可能的解决方案:

package buffchan

import (
    "container/list"
    "sync"
)

// BufferedChannel provides go channel like interface with unlimited storage
type BufferedChannel struct {
    m *sync.Mutex
    l *list.List
    c *sync.Cond
}

// New Creates new buffer channel
func New() *BufferedChannel {
    m := new(sync.Mutex)
    return &BufferedChannel{
        m: m,
        l: list.New(),
        c: sync.NewCond(m),
    }
}

// Append adds given data at end of channel
func (b *BufferedChannel) Append(v interface{}) {
    b.m.Lock()
    defer b.m.Unlock()

    b.l.PushBack(v)
    b.c.Signal()

}

// Remove removes first element of list synchronously
func (b *BufferedChannel) Remove() interface{} {
    b.m.Lock()
    defer b.m.Unlock()

    for b.l.Len() == 0 {
        b.c.Wait()
    }

    v := b.l.Front()
    b.l.Remove(v)

    return v.Value
}

// Inspect first element of list if exists
func (b *BufferedChannel) Inspect() interface{} {
    b.m.Lock()
    defer b.m.Unlock()

    for b.l.Len() == 0 {
        return nil
    }

    return b.l.Front().Value
}

// AsyncRemove removes first element of list asynchronously
func (b *BufferedChannel) AsyncNonBlocking() interface{} {
    b.m.Lock()
    defer b.m.Unlock()

    for b.l.Len() == 0 {
        return nil
    }

    v := b.l.Front()
    b.l.Remove(v)

    return v.Value
}

enter image description here