在Shiny ggplot中无法控制Legend

时间:2015-02-20 15:04:16

标签: r ggplot2 shiny

我有两个问题。首先,我希望能够格式化图例中的数字。在Shiny之外我可以使用geom_point()调用中的格式(val,big.mark =“,”)来做到这一点。但是当我到达Shiny时,它将无法工作。另外,正如您所看到的,图例中只有两点。我想要更多积分。如果每个值都可以,但我也希望能够控制它。我尝试过scale_fill_manual(values = factor(val))和scale_color_manual()相同,但似乎既没有在Shiny中工作,也没有在R中工作。

我的闪亮服务器代码如下:

shinyServer(function(input, output) {

#spend <- spend[order(spend$Tier.1.Contract.Category),]
spend <- read.csv("data/Construction_data.csv")

centpop <- aggregate(cbind(Action.Obligation, Action_Absolute_Value) ~ Principal.Place.of.Performance.Country.Name + FY, spend, sum)
#centmap <- map("world", region = toupper(centpop$Principal.Place.of.Performance.Country.Name))

mapdata <- map_data("world2", region = unique(centpop$Principal.Place.of.Performance.Country.Name), na.rm = TRUE)

mapdata$countries <- toupper(mapdata$region)

centmerge <- merge(x = centpop, y = mapdata, by.x = "Principal.Place.of.Performance.Country.Name", by.y = "countries", na.rm +TRUE)
centmerge <- centmerge[order(centmerge$Principal.Place.of.Performance.Country.Name,centmerge$order),]
centavg <- aggregate(cbind(Action_Absolute_Value, Action.Obligation, lat, long, group)~  Principal.Place.of.Performance.Country.Name + FY, centmerge, mean)

output$GMap <- renderPlot({
options(scipen = 999)
centavg <-  centavg[centavg$FY == input$FY,]

ggplot(data = centmerge, aes_string(x="long", y="lat", group = "group")) +
geom_polygon(stat = "identity", fill = "#CC6600", color = "black", alpha =.7) +
geom_point(data = centavg, aes_string(x = "long", y = "lat", group = "Principal.Place.of.Performance.Country.Name", size = input$SpendType), stat = "identity", color = "#990000")+  
scale_size_continuous(range = c(3,8)) +
ggtitle("Contract Spending in the CENTCOM AOR") +
coord_map("polyconic")+
theme(axis.text.y = element_blank(), axis.text.x = element_blank(), axis.title.x=element_blank(),
axis.title.y=element_blank(),axis.ticks = element_blank(), panel.background = element_blank(), plot.title=element_text(face="bold", size=20))+
labs(size = "Amount ($M)")
})

我的ui如下:

library(shiny)
spend <- read.csv("data/CENTCOMdata.csv")
shinyUI(fluidPage(

titlePanel("Contract Spend"),

    sidebarLayout(

     sidebarPanel( 
        selectInput("SpendType", label = "Select Action Obligation Type",
                choices = colnames(spend)[11:12]         
                ),

        selectInput("FY", label = "Fiscal Year",
                    choices = unique(spend$FY),
                selected = min(unique(spend$FY))
                        )
    ),

    mainPanel(plotOutput("GMap")
)
)
))

它生成如下图表: enter image description here

由于

更新:以下是我的数据示例。重现整个事情是不够的,但是会给出字段和数据类型的感觉。

Principal.Place.of.Performance.Country.Name = c("BAHRAIN","BAHRAIN","BAHRAIN","EGYPT","EGYPT","EGYPT","IRAN","IRAN","IRAQ","IRAQ","IRAQ","JORDAN","JORDAN","JORDAN","KUWAIT","KUWAIT") 
FY = c(2013,2014,2015,2013,2014,2015,2013,2014,2013,2014,2015,2013,2014,2015,2013,2014)
Action_Absolute_Value = c(771321456.1,479582869.8,514214922.1,97286075.83,69121513.24,23071035.64,1382620,24423.76,2022560127,1999064521,120073800.5,266136924.4,380041091.5,54896224.32,3189939809,1982808077) 
Action.Obligation = c(758755260,440975903.7,507433161.8,85876698.15,55720022.7,21882002.7,1382620,-24423.76,1146078095,647903735.5,77122340.53,244683999.1,368479216.8,49013205.78,2970687191,1752387003)
lat = c(26.1272188822428,26.1272188822428,26.1272188822428,29.0995075001436,29.0995075001436,29.0995075001436,32.9537412255211,32.9537412255211,32.83929874051,32.83929874051,32.83929874051,31.2209393637521,31.2209393637521,31.2209393637521,29.4381094614665,29.4381094614665)
long = c(50.5078544616699,50.5078544616699,50.5078544616699,31.8482420528636,31.8482420528636,31.8482420528636,53.2439389524207,53.2439389524207,45.3801684841033,45.3801684841033,45.3801684841033,36.4073101225353,36.4073101225353,36.4073101225353,47.735088857015,47.735088857015)
group = c(22,22,22,8.61176470588235,8.61176470588235,8.61176470588235,5.16814159292035,5.16814159292035,8.03225806451613,8.03225806451613,8.03225806451613,8,8,8,18.3333333333333,18.3333333333333)

data.frame(Principal.Place.of.Performance.Country.Name,FY,Action_Absolute_Value, Action.Obligation, lat, long, group)

0 个答案:

没有答案