我在每个网格上都有经度,纬度和值的数据。网格可能有多个值,因此我设置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()
错了。蓝色网格更大。我找不到错误。我跟着link但是无法做到。我想我遗失了一些微不足道的东西。我的数据可以访问here。非常感谢提前。
答案 0 :(得分: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)
产生这个: