簡體   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