R:在地图上绘制时,geom_scatterpie不采用color参数

时间:2018-08-09 19:26:03

标签: r dictionary ggplot2

我想绘制类似于here末尾的情节。

我对从多个位置采样的生态变量进行了DAPC。我使用DAPC的后代代替祖先系数。

数据如下:

head(ematrix) # the posteriors of the DAPC
x              1            2           3            4            5            6            7
1 5.589864e-01 7.558359e-06 0.440094949 1.636432e-04 3.484658e-06 7.439948e-04 6.297422e-13
2 3.375382e-04 5.844435e-01 0.415140580 2.505983e-05 5.330442e-05 2.627631e-09 2.508273e-21
3 7.674127e-09 9.987311e-01 0.001265920 8.093470e-07 2.168683e-06 1.059058e-12 2.171669e-24
4 9.580974e-01 2.426956e-11 0.004064812 4.570989e-06 4.120601e-09 3.783324e-02 3.065723e-09

head(coord)
x    Longitude Latitude
1  35.13092 32.51011
2  35.34025 32.55186
3  35.54331 32.64583

head(tmp)
x    Longitude Latitude  colour ID          V1          V2          V3          V4
1  35.13092 32.51011 #9ad0f3  1 0.238149000 0.000099991 0.759206000 0.002544900
2  35.34025 32.55186 #e79f00  2 0.457091000 0.542709000 0.000099982 0.000099982
3  35.54331 32.64583 #7fc97f  3 0.934375000 0.065424700 0.000099982 0.000099982

这是我第一次使用的代码,它可以正常工作。

# define a eco and gen colour vector
> myCol1=c("#7fc97f", "#e79f00", "#9ad0f3", "#0072B2", "#CC79A7",
>          "#D55E00", "#F0E442", "antiquewhite4")
> myCol2=hcl(h = seq(15, 375, length = 8 + 1), l = 65, c = 100)[1:8> ]
> 
> library(ggplot2)
> library(rworldmap)
> 
> map.polygon <- getMap(resolution = "high")
> 
> pl <- ggtess3Q(ematrix, coord, map.polygon = map.polygon, col.palette=
>                  CreatePalette(color.vector = myCol2[1:7], 
>                                palette.length = 9))
> pl +
>   geom_path(data = map.polygon, aes(x = long, y = lat)) +
>   xlim(min(coord$Longitude)-0.05, max(coord$Longitude)+0.05) + 
>   ylim(min(coord$Latitude)-0.05, max(coord$Latitude)+0.05) + 
>   coord_equal() + 
>   xlab("") + ylab("") +
>   theme_bw() +
>   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
>     axis.text.x = element_blank(),axis.text.y = element_blank(),
>     axis.ticks = element_blank(),panel.border = element_blank()) + 
>   geom_point(data = coord, aes(x = Longitude, y = Latitude), 
>              size = 3,color=map.col.admcl$col) +
>   geom_point(data = coord, aes(x = Longitude, y = Latitude), 
>             size = 3,pch=1)+
>   geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2), 
>                data=data.frame("x1"=34.79,"y1"=31.31,"x2"=34.79,"y2"=33.34)) +
>   geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2), 
>                data=data.frame("x1"=34.79,"y1"=33.34,"x2"=35.88,"y2"=33.34)) +
>   geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2), 
>                data=data.frame("x1"=35.88,"y1"=33.34,"x2"=35.88,"y2"=31.31)) +
>   geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2), 
>                data=data.frame("x1"=35.88,"y1"=31.31,"x2"=34.79,"y2"=31.31))

The plot already looks similar to what I want.

现在,我想用我采样的个体(tmp中的V1-V4)将彩色圆点与祖先系数的饼图交换。我为此使用了sactterpie软件包。

> library(scatterpie)
> 
> tmp=data.frame(coord,"colour"=map.col.admcl$col,"ID"=c(1:nrow(qmatrix)),qmatrix)
> 
> pl +
>   geom_path(data = map.polygon, aes(x = long, y = lat)) +
>   xlim(min(coord$Longitude)-0.05, max(coord$Longitude)+0.05) + 
>   ylim(min(coord$Latitude)-0.05, max(coord$Latitude)+0.05) + 
>   coord_equal() + 
>   xlab("") + ylab("") +
>   theme_bw() +
>   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
>         axis.text.x = element_blank(),axis.text.y = element_blank(),
>         axis.ticks = element_blank(),panel.border = element_blank()) + 
>   geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2), 
>                data=data.frame("x1"=34.79,"y1"=31.31,"x2"=34.79,"y2"=33.34)) +
>   geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2), 
>                data=data.frame("x1"=34.79,"y1"=33.34,"x2"=35.88,"y2"=33.34)) +
>   geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2), 
>                data=data.frame("x1"=35.88,"y1"=33.34,"x2"=35.88,"y2"=31.31)) +
>   geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2), 
>                data=data.frame("x1"=35.88,"y1"=31.31,"x2"=34.79,"y2"=31.31)) +
>   geom_scatterpie(aes(x=Longitude, y=Latitude, group=ID,r=1),
>                   cols=paste0("V",c(1:length(5:ncol(tmp)))), color=tmp$colour,
>                   data=tmp) 

这总是导致错误:

  

错误:美学必须为长度1或与数据(43552):颜色相同

我不知道哪个数据有43552个条目(可能是多边形?)。所以我将颜色参数更改为“绿色”,只是为了看看如果美学长度为1时会发生什么情况。

> ... +
>   geom_scatterpie(aes(x=Longitude, y=Latitude, group=ID,r=1),
>                   cols=paste0("V",c(1:length(5:ncol(tmp)))), color="green",
>                   data=tmp)

我收到以下错误:

  

grDevices :: col2rgb(colour,TRUE)中的错误:颜色名称'V1'无效

然后我尝试删除color参数。

> ... +
>   geom_scatterpie(aes(x=Longitude, y=Latitude, group=ID,r=1),
>                   cols=paste0("V",c(1:length(5:ncol(tmp)))),
>                   data=tmp)

错误:

  

grDevices :: col2rgb(colour,TRUE)中的错误:颜色名称'V1'无效

有什么想法吗?

谢谢!

0 个答案:

没有答案