我正在玩的数据来自下面列出的互联网资源
nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv", sep=",")
Run Code Online (Sandbox Code Playgroud)
我想要做的是创建一个2D点图,比较该表中的两个指标,每个玩家在图上表示一个点.我有以下代码:
nbaplot <- ggplot(nba, aes(x= MIN, y= PTS, colour="green", label=Name)) +
geom_point()
Run Code Online (Sandbox Code Playgroud)
这给了我以下内容:

我想要的是一个玩家名字的标签就在点旁边.我认为ggplot美学中的标签功能会为我做这个,但事实并非如此.
我也尝试过text()函数和textxy()函数library(calibrate),它们似乎都不适用于ggplot.
如何为这些点添加名称标签?
在尝试将新行重叠到现有ggplot时,我收到以下错误:
Error: ggplot2 doesn't know how to deal with data of class uneval
Run Code Online (Sandbox Code Playgroud)
我的代码的第一部分工作正常.以下是来自美国中西部电力市场的"近期"每小时风力发电数据的图像.

现在我想用Red重叠最后两天的观察值.它应该很容易,但我无法弄清楚为什么我会收到错误.
任何帮助将不胜感激.
以下是可重复的示例:
# Read in Wind data
fname <- "https://www.midwestiso.org/Library/Repository/Market%20Reports/20130510_hwd_HIST.csv"
df <- read.csv(fname, header=TRUE, sep="," , skip=7)
df <- df[1:(length(df$MKTHOUR)-5),]
# format variables
df$MWh <- as.numeric(df$MWh)
df$Datetime <- strptime(df$MKTHOUR, "%m/%d/%y %I:%M %p")
# Create some variables
df$Date <- as.Date(df$Datetime)
df$HrEnd <- df$Datetime$hour+1
# Subset recent and last data
last.obs <- range(df$Date)[2]
df.recent <- subset(df, Date %in% seq(last.obs-30, last.obs-2, by=1))
df.last <- subset(df, Date %in% seq(last.obs-2, …Run Code Online (Sandbox Code Playgroud) 默认情况下,ggplot2会生成灰色背景的图.如何更改绘图背景的颜色?
例如,由以下代码生成的图:
library(ggplot2)
myplot<-ggplot(data=data.frame(a=c(1,2,3), b=c(2,3,4)), aes(x=a, y=b)) + geom_line()
myplot
Run Code Online (Sandbox Code Playgroud) 我想用ggplot2(v.9)绘制世界地图,如果有信息,它会合并两个部分.以下示例说明:
library(rgdal)
library(ggplot2)
library(maptools)
# Data from http://thematicmapping.org/downloads/world_borders.php.
# Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
# Unpack and put the files in a dir 'data'
gpclibPermit()
world.map <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
world.ggmap <- fortify(world.map, region = "NAME")
n <- length(unique(world.ggmap$id))
df <- data.frame(id = unique(world.ggmap$id),
growth = 4*runif(n),
category = factor(sample(1:5, n, replace=T)))
## noise
df[c(sample(1:100,40)),c("growth", "category")] <- NA
ggplot(df, aes(map_id = id)) +
geom_map(aes(fill = growth, color = category), map =world.ggmap) +
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
scale_fill_gradient(low = "red", high …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ggplot2和地图来绘制纽约州的县名.我的方法是通过县找到纬度和经度的方法(我假设这是县的中心,但这可能是错误的思考),然后使用geom_text在地图上绘制名称.它没有像我预期的那样表现,因为它每个县都在绘制多个名字.
我正在寻找的结果是每个文本(县)的中心位于其各自县的中心.
除了解决这个问题,我还很高兴能帮助理解我对ggplot思考的问题.
先感谢您.
library(ggplot2); library(maps)
county_df <- map_data('county') #mappings of counties by state
ny <- subset(county_df, region=="new york") #subset just for NYS
ny$county <- ny$subregion
cnames <- aggregate(cbind(long, lat) ~ subregion, data=ny, FUN=mean)
p <- ggplot(ny, aes(long, lat, group=group)) + geom_polygon(colour='black', fill=NA)
p #p of course plots as expected
#now add some county names (3 wrong attempts)
p + geom_text(aes(long, lat, data = cnames, label = subregion, size=.5)) #not correct
#I said maybe I'm confusing it with the same …Run Code Online (Sandbox Code Playgroud) SpatialPoly数据:SpatialData
产量数据:产量数据
码:
## Loading packages
library(rgdal)
library(plyr)
library(maps)
library(maptools)
library(mapdata)
library(ggplot2)
library(RColorBrewer)
library(foreign)
library(sp)
## Loading shapefiles and .csv files
#Morocco <- readOGR(dsn=".", layer="Morocco_adm0")
MoroccoReg <- readOGR(dsn=".", layer="Morocco_adm1")
MoroccoYield <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/RMaps_Morocco/Morocco_Yield.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
## Reorder the data in the shapefile based on the category variable "ID_1" and change to dataframe
MoroccoReg <- MoroccoReg[order(MoroccoReg$ID_1), ]
MoroccoReg.df <- fortify(MoroccoReg)
## Add the yield impacts column to shapefile from the .csv file by "ID_1" …Run Code Online (Sandbox Code Playgroud)