我正在使用等值线图(ggplot2),我在正确实现geom_text时遇到了问题。在输出中它显示文本在地图上,但我可以看到许多重复的名称,不知道为什么,并需要帮助来解决这个问题。使用R studio,Australia将形状文件和数据映射到csv文件中。此外,我有意在地图上显示值而不是州名,不知道如何。
UI.R文件中的library(shiny)
# Define UI for application
shinyUI(fluidPage(
# Application title
headerPanel("Unemployment rate Data"),
# Sidebar with controls to select the variable to plot against
# specify whether outliers should be included
sidebarLayout(
sidebarPanel(
selectInput("variable", "Type:",
c("States" = "region",
"Unemployment_Rate" = "unemployment_rate_2015"
)
)
#checkboxInput("outliers", "Show outliers", FALSE)
),
# Show the caption and plot of the requested variable against population data data
mainPanel(
h3(textOutput("caption")),
plotOutput("datPlot")
)
)#----/sidebar layout
))#----/shinyUI
Server.R文件中的require(maptools)
require(ggmap)
require(maps)
require(mapproj)
require(data.table)
library(rgeos)
library(plyr)
library(shiny)
library(datasets)
library(ggplot2) # load ggplot
setwd("C:/Users/AbdullahAl/Documents/AU-Shapefile")
pop <- read.csv("unemployment.csv")
AUS = readShapeSpatial("AUS_adm1")
shinyServer(function(input, output) {
# Compute the forumla text in a reactive expression since it is
# shared by the output$caption and output$mpgPlot expressions
output$caption <- reactiveText(function(){
paste("States ~", input$variable)
})
# ggplot version
output$datPlot <- reactivePlot(function() {
pop <- data.table(States = pop$region, var = factor(pop[[input$variable]]))
# Generate a plot of the requested variable against year and only
# include outliers if requested
AUS <- fortify(AUS, region = "NAME_1")
#add color="black" inside geom_map to have the boarder black
ggplot() + geom_map(data = pop, aes(map_id = States, fill = var), map = AUS) +
expand_limits(x = AUS$long, y = AUS$lat) +
# to add values over
geom_text(data = AUS, aes(x =long , y = lat, label = id, size = 2.5),
check_overlap = TRUE, position = "jitter", parse = FALSE) +
geom_label()
# facet_wrap( ~ var) - use this to show each divided states
})
})
输出为图像 the image is showing Australis's map and repeated name of the states over it