2

我在每个网格上有一个经度、纬度和值的数据。一个网格可能有多个值,所以我设置alpha为可视化多个值。我的目标是用三个不同的范围填充网格。如果该值为零,则该网格将为空。

library(maps)
library(ggplot2)
data <- read.csv("G:/mydata.csv")
g1 <- ggplot(aes(x=x, y=y, fill= A), data=data) +
  geom_tile(data=subset(data, A > 1970 &  A < 1980),fill = "black", alpha = 0.5)+
  geom_tile(data=subset(data, B > 1970 & B < 1980),fill = "black", alpha = 0.5)+
  geom_tile(data=subset(data, C > 1970 & C < 1980),fill = "black", alpha = 0.5)+
  geom_tile(data=subset(data, A > 1979 & A < 1990),fill = "blue", alpha = 0.5)+
  geom_tile(data=subset(data, B> 1979 & B < 1990), fill = "blue", alpha = 0.5)+
  geom_tile(data=subset(data, C > 1979 & C < 1990),fill = "blue", alpha = 0.5)+
  geom_tile(data=subset(data, A > 1989),fill = "red", alpha = 0.5)+
  geom_tile(data=subset(data, B > 1989),fill = "red", alpha = 0.5)+
  geom_tile(data=subset(data, C > 1989),fill = "red", alpha = 0.5)+
  theme_classic()

图片

是错的。由于蓝色网格更大。我找不到错误。我点击了链接,但无法成功。我想我缺少一些微不足道的东西。我的数据可以在这里访问。提前谢谢了。

4

1 回答 1

2

抱歉,无法按照您的设想进行。我看到的灵活性不够。但是可以这样做:

library(maps)
library(ggplot2)

ddf <- read.csv("mydata.csv")

setz <- function(dddf,zvek,lev=0,fillclr){
  dddf$z <- as.numeric(zvek)
  dddf$lev <- lev
  dddf$color <- "white"
  dddf$fill <- ifelse(zvek,fillclr,"gray")
  return(dddf)
}
df1<-setz(ddf,ddf$A>1970 & ddf$A<1980,"A>1970 & A<1980","black")
df2<-setz(ddf,ddf$B>1970 & ddf$B<1980,"B>1970 & B<1980","black")
df3<-setz(ddf,ddf$C>1970 & ddf$C<1980,"C>1970 & C<1980","black")

df4<-setz(ddf,ddf$A>1979 & ddf$A<1990,"A>1979 & A<1990","blue")
df5<-setz(ddf,ddf$B>1979 & ddf$B<1990,"B>1979 & B<1990","blue")
df6<-setz(ddf,ddf$C>1979 & ddf$C<1990,"C>1979 & C<1990","blue")

df7<-setz(ddf,ddf$A>1989,"A>1989","red")
df8<-setz(ddf,ddf$B>1989,"B>1989","red")
df9<-setz(ddf,ddf$C>1989,"C>1989","red")

ddg <- rbind( df1,df2,df3, df4,df5,df6, df7,df8,df9 )

g1 <- ggplot(data=ddg,aes(x=x, y=y,fill=fill,color=color)) +
  geom_tile() +
  scale_color_identity() +
  scale_fill_identity() +
  facet_wrap(~lev)
  theme_classic()
print(g1)

这产生了这个:

在此处输入图像描述

于 2015-12-21T16:38:11.163 回答