根据列绘制具有不同颜色的 data.frame [英] Plotting data.frame with different colour according to a column
问题描述
我有一个这样的数据框:
I have a data frame like this:
> video
id oldid year month day lon lat colour
01208513 Continent 1885 1 1 -30.7 -32.65 #FFFFFF
04960001 05890280 1885 1 1 122.72 26.17 #FDFEFF
05520001 CornFMort 1885 1 1 -22.84 48.34 #FBFDFF
06058 06058 1885 1 1 -61.5 36.5 #F9FCFF
06251 06251 1885 1 1 -113.5 -2.5 #F7FBFF
06323 06323 1885 1 1 174.5 -26.5 #F5FAFF
06466 06466 1885 1 1 -115.5 -40.5 #F3F9FF
106323 106323 1885 1 1 177.5 -26.5 #F1F8FF
我根据其 id 为每个数据分配了一种颜色
where I have assigned a colour to every data according to its id
z <- unique(video$id)
range01 <- function(x)(x-min(x))/diff(range(x))
rainbow(7)
cRamp <- function(x){
cols <- colorRamp(colours())(range01(x))
apply(cols, 1, function(xt)rgb(xt[1], xt[2], xt[3], maxColorValue=255))
}
video$col <- as.factor(cRamp(match(video$id,z)))
实际上我想做的是绘制每天 lon
和 lat
的值,用颜色区分不同的 id,但到目前为止还没有似乎工作.是否存在与 video$colour
类相关的任何问题,或者我应该以不同的方式分配颜色?
What I would like to do in fact is to plot the value of lon
and lat
for every day with the colours distinguishing the different ids but so far it does not seem to work. Can be there any issues related to the class of video$colour
or should I assign the colour in a different way?
要绘制我将数据框划分为年/月/日文件:
To plot I dividing the data frame in year/month/day files:
YRMODYlist <- list()
YRMODYlist <- split(video, data.frame(video$yr, video$mo, video$dy))
然后绘图
for (i in 1:length(YRMODYlist)){
colnames(YRMODYlist[[i]]) <-c("id","oldid","yr","mo","dy","lon","lat","col")
plot(YRMODYlist[[i]]$lon, YRMODYlist[[i]]$lat, ylim=c(-90,90),xlim=c(-180,180),col = YRMODYlist[[i]]$col)
}
推荐答案
看着你的数据,我怀疑这是某种面向地理的数据集(因为使用了 lon
和 lat
变量,通常用于识别地理位置)以及分布在世界各地的位置.
Looking at your data, I suspect that this some kind of geographically oriented dataset (because of the use of the lon
and lat
variables, which are usually used for identifiing geographic locations) with locations scattered around the world.
假设我是对的,你可能会按如下方式解决这个问题:
Assuming that I'm right, you might tackle this problem as follows:
# reading the data
video <- read.table(header=TRUE, comment.char="",
text="id oldid year month day lon lat colour
01208513 Continent 1885 1 1 -30.7 -32.65 #FFFFFF
04960001 05890280 1885 1 1 122.72 26.17 #FDFEFF
05520001 CornFMort 1885 1 1 -22.84 48.34 #FBFDFF
06058 06058 1885 1 1 -61.5 36.5 #F9FCFF
06251 06251 1885 1 1 -113.5 -2.5 #F7FBFF
06323 06323 1885 1 1 174.5 -26.5 #F5FAFF
06466 06466 1885 1 1 -115.5 -40.5 #F3F9FF
106323 106323 1885 1 1 177.5 -26.5 #F1F8FF")
# getting the required packages
library(ggmap)
library(ggplot2)
# splitting the dataframe into a list of dataframes by id
video$id <- as.factor(video$id)
idlist <- split(video, video$id)
# creating a map for each location with the corresponding points plotted on it
for (i in 1:length(idlist)){
x <- get_map(location = c(lon=mean(idlist[[i]]$lon), lat=mean(idlist[[i]]$lat)), zoom=3, maptype="satellite", scale=2)
p <- ggmap(x) + geom_point(data=idlist[[i]], aes(x=lon, y=lat, fill=as.factor(id)), size = 4, shape = 21, show_guide=FALSE)
print(p)
}
这将产生一系列像这样的图:
this will produce a series of plots like this:
这篇关于根据列绘制具有不同颜色的 data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!