R中的错误用于创建箭头图链接的micromap

时间:2015-06-10 20:29:52

标签: r ggplot2

尝试为链接的micromap制作箭头图,但不断收到以下错误:

Error in as.numeric(angle):  cannot coerce type 'builtin' to vector of type 'double'.

箭头图必须在ggplot中生成,然后才能放入链接的micromap中。从我看到它不能像图形点,boxplot,dot_cl等生成。我在网上找到了这个代码,并试图用我的数据来显示从2004年到2012年的费率变化。

library(micromap)
library(ggplot2)
library(grid)

setwd        ("//cdc.gov/private/L119/GON6/Stateincid_bayes/linkedmicromap/arrowplot")

ger<-readShapePoly("states_repositioned_2006_albers.shp")

statePolys<- create_map_table(ger,"ST_FIPS")
head(statePolys)

smoothedrates<-read.csv('smoothedrates.csv')

data(smoothedrates)
myStats <- smoothedrates
head(myStats)

myNewStats <- create_DF_rank(myStats, ord.by="Rate_12", group=5)
head(myNewStats)

ggplot(myNewStats) + 
    geom_segment(aes(x=Rate_04, y=-pGrpOrd,
    xend=Rate_12, yend=-pGrpOrd, colour=factor(color)),
    arrow=arrow(length=unit(0.1,"cm"))) +
    facet_grid(pGrp~., scales="free_y") +
    scale_colour_manual(values=c    ("red","orange","green","blue","purple"),
    guide="none")
ggsave    (file="//cdc.gov/private/L119/GON6/Stateincid_bayes/linkedmicromap/arrowplot/tes    tarrow.png", dpi = 300)

myAtts <- sample_att()
myNumber <- 1
myAtts$colors <- c("red","orange","green","blue","purple")
myAtts[[myNumber]]$panel.data <- c("Rate_04","Rate_12")

myColors <- myAtts$colors
myColumns <- myAtts[[myNumber]]$panel.data

myNewStats$data1 <- myNewStats[, myColumns[1]]
myNewStats$data2 <- myNewStats[, myColumns[2]]
myPanel <- ggplot(myNewStats) + 
    geom_segment(aes(x=data1, y=-pGrpOrd,
    xend = data2, yend=-pGrpOrd, colour=factor(color)),
    arrow=arrow(length=unit(0.1,"cm"))) +
    facet_grid(pGrp~.) +
    scale_colour_manual(values=myColors,
    guide="none")
myPanel
assimilatePlot(myPanel, myNumber, myAtts)
ggsave     (file="//cdc.gov/private/L119/GON6/Stateincid_bayes/linkedmicromap/arrowplot/tes     tarrow2.png", dpi = 300)

arrow_plot_build <- function(myPanel, myNumber, myNewStats, myAtts){
    myColors <- myAtts$colors
    myColumns <- myAtts[[myNumber]]$panel.data
    myNewStats$data1 <- myNewStats[, myColumns[1]]
    myNewStats$data2 <- myNewStats[, myColumns[2]]
    myNewStats <- alterForMedian(myNewStats, myAtts)
    myPanel <- ggplot(myNewStats) + 
    geom_segment(aes(x=data1, y=-pGrpOrd,
    xend = data2, yend=-pGrpOrd, colour=factor(color)),
    arrow=arrow(length=unit(0.1,"cm"))) +
    facet_grid(pGrp~.) +
    scale_colour_manual(values=myColors, guide="none")
 myPanel <- assimilatePlot(myPanel, myNumber, myAtts)
}
myPanel

myPanelAtts <- standard_att()
myPanelAtts <- append(myPanelAtts,
    list(line.width=1, tip.length=1))
arrow_plot_att <- function(){
    myPanelAtts <- standard_att()
    myPanelAtts <- append(myPanelAtts,
    list(line.width=1, tip.length=1))
}

arrow_plot_build <- function(myPanetl, myNumber, myNewStats, myAtts){
    myColors <- myAtts$colors
    myColumns <- myAtts[[myNumber]]$panel.data
    myLineWidth <- myAtts[[myNumber]]$line.width
    myTipLength <- myAtts[[myNumber]]$tip.length
    myNewStats$data1 <- myNewStats[, myColumns[1]]
    myNewStats$data2 <- myNewStats[, myColumns[2]]
    myPanel<- ggplot(myNewStats) +
    geom_segment(aes(x=data1, y=-pGrpOrd,
    xend = data2, yend=-pGrpOrd,
    colour=factor(color)),
    arrow=arrow(length,unit(0.1*myTipLength,"cm")),
    size=myLineWidth) +
    facet_grid(pGrp~., space="free", scales="free_y") +
    scale_colour_manual(values=myColors, guide="none")
    myPanel <- assimilatePlot(myPanel, myNumber, myAtts)
}
myPanel






mmplot(stat.data=myStats,
map.data=statePolys,
panel.types=c("map","labels","arrow_plot"),
panel.data=list(NA,"state",list("Rate_04","Rate_12")),
ord.by="Rate_12",
grouping=5,
map.link=c("ST_FIPS","ID"),
print.file="//cdc.gov/private/L119/GON6/Stateincid_bayes/linkedmicromap/arrowplot/testarrow3.jpeg", print.res=300)

0 个答案:

没有答案