myPalette <- colorRampPalette(rev(brewer.pal(10, "Spectral")))
usamap <- map_data("state")
ggplot(data=all_data,aes(x=col,y=row,color=m)) +
geom_point()+
scale_colour_gradientn(name = "Flashiness",colours = myPalette(10), limits=c(0,1))+
geom_polygon( data=usamap, aes(x=long, y=lat,group=group),size=0.3, colour="black",fill=NA)+
theme_bw()+
theme(line = element_blank())+
theme(legend.position = c(.93,.20),panel.grid.major = element_line(colour = "#854440"))+
ggsave("test.png",width=10, height=8,dpi=300)
给我这个情节
然而,我想要像素/瓷砖,而不是瓷砖/像素相互接触的点。这种性质的东西。
我该怎么做?我尝试使用geom_raster,但我无法使其工作。这是新代码:
ggplot(all_data) +
geom_raster(aes(x=col, y=row))+
scale_colour_gradientn(name = "Flashiness",colours = myPalette(100), limits=c(0,1))+
ggsave("test_pixel.png",width=10, height=8,dpi=300)
新的数字没有颜色,盒子之间也有间隙。
完整数据集:https://www.dropbox.com/s/hrpgk3uqx945hgf/temp.csv?dl=0
数据预览:
dput(head(all_data,10))
structure(list(row = c(48.16, 48.33, 48.15, 48.1, 48.18, 48.14,
48.23, 48.22, 48.18, 48.14), col = c(-124.7, -124.69, -124.69,
-124.69, -124.68, -124.68, -124.67, -124.67, -124.67, -124.67
), m = c(0.4713, 0.8998, 0.4891, 0.8418, 0.7998, 0.5292, 0.8115,
0.8826, 1, 0.5716), flagar = c(0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L,
1L, 0L), flagk = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), flagsi = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), flags2o = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 2L, 0L), flagap = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L), flagmt = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), flagcn = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), flagkf = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), flagrd = c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), flagrv = c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 0L, 2L), flagpt = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
)), .Names = c("row", "col", "m", "flagar", "flagk", "flagsi",
"flags2o", "flagap", "flagmt", "flagcn", "flagkf", "flagrd",
"flagrv", "flagpt"), row.names = c(NA, 10L), class = "data.frame")
答案 0 :(得分:0)
我认为您需要从点到栅格网格进行空间插值。使用发布的数据
library(gstat)
library(sp)
library(maptools)
library(viridis)
# Create a spatial object
coordinates(dat) = ~x + y
# Define the extent for spatial interpolation
x.range <- c(min(dat$x)-0.0125, max(dat$x)+0.0125)
y.range <- c(min(dat$y)-0.0125, max(dat$y)+0.0125)
# Create desired grid
grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 0.0125),
y = seq(from = y.range[1], to = y.range[2], by = 0.0125))
coordinates(grd) <- ~x + y
gridded(grd) <- TRUE
# Interpolate using Inverse Distance Weighted
idw <- idw(formula = m ~ 1, locations = dat, newdata = grd)
# Output clean up
idw.output = as.data.frame(idw)
names(idw.output)[1:3] <- c("Longitude", "Latitude", "Flashiness")
# Plot
p1 <- ggplot() + geom_tile(data = idw.output, aes(x = Longitude, y = Latitude, fill = Flashiness)) +
scale_fill_viridis() + geom_point(data = all_data, aes(x = col, y = row, size = m), shape = 21, colour = "red") +
scale_size_continuous(name = "") +
theme_bw()
p1
结果:
此post
中的更多信息