繁体   English   中英

“ scale_fill_manual”错误

[英]Error in “scale_fill_manual”

我设法将它们分别绘制到数据集上,但是当我尝试将它们组合成一个图时,会收到错误消息:“错误:手动比例值不足。 需要34个,但仅提供8个。”问题在scale_fill_manual()中,请有人帮我解决这个问题。 我希望生成的数据能正常工作..我仍然遇到相同的错误

产生资料

i_country<-wrld_simpl@data$NAME[1:26]

e_country<-wrld_simpl@data$NAME[27:34]

i <- data.frame(
     country = i_country,
     import = c(sample(10:10000, 26)))

e <- data.frame(
     country =e_country,
     export = c(sample(10:10000, 8)))

准备wrld_simpl数据

data(wrld_simpl)
wrld_simpl@data$id <- wrld_simpl@data$NAME
wrld <- fortify(wrld_simpl, region="id")
wrld <- subset(wrld, id != "Antarctica")

i$brk <- cut(i$import, 
         breaks=c(0, sort(i$import)), 
         labels=as.character(i[order(i$import),]$country),
         include.lowest=TRUE)

e$brk <- cut(e$export, 
         breaks=c(0, sort(e$export)), 
         labels=as.character(e[order(e$export),]$country),
         include.lowest=TRUE)

情节

 library(ggplot2)

gg <- ggplot() + 
   geom_map(data=wrld, map=wrld, aes(map_id=id, x=long, y=lat), fill="white", color="#7f7f7f", size=0.25) +
   geom_map(data=i, map=wrld, aes(map_id=country, fill=brk),  color="white", size=0.25) + 
   geom_map(data=e, map=wrld, aes(map_id=country, fill=brk),  color="white", size=0.25) +
   scale_fill_manual(data=i, values=colorRampPalette(brewer.pal(9, 'Reds'))(length(i$import)), name="Export country") +
   scale_fill_manual(data=e, values=colorRampPalette(brewer.pal(9, "Greens"))(length(e$export)), name="Import country") +
   coord_map() + 
   theme(plot.background = element_rect(fill = "transparent", colour = NA),
         panel.border = element_blank(),
         panel.background = element_rect(fill = "transparent", colour = NA),
         panel.grid = element_blank(),
         axis.text = element_blank(),
         axis.ticks = element_blank(),
         legend.position =c( "left", "right")) 

gg

错误

手动刻度值不足。 需要34个,但仅提供8个。

我现在没有R,但请尝试在您的brewer.pal命令中将length(i$import)与9 brewer.pal 通常,此错误表示您的scale_fill_manual命令中没有足够的颜色或形状。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM