gpt4 book ai didi

r - 在ggplot中绘制两个图表

转载 作者:行者123 更新时间:2023-12-02 21:34:28 24 4
gpt4 key购买 nike

我在 ggplot 中遇到问题,我想在同一图中绘制单独的数据集。第一个数据集创建地理热图并根据状态显示机会值。

setwd("~/R Work Example")
library(ggplot2)
library(maps)
states <- map_data("state")
tf=read.csv("Geographic Opprotunity.csv")
mydata<-read.csv("top 200 geographic heatmap.csv")
tfmerged <- merge(states, tf, sort = FALSE, by = "region")
tfmerged <- tfmerged[order(tfmerged$order), ]
Map<- ggplot(tfmerged, aes(long,lat, group=group,
fill=Total.Annual.Opportunity.In.Millions))+ geom_polygon()+ coord_equal()
Map + scale_fill_gradient(low="white", high="red")
Map<-Map + scale_fill_gradient(low="white", high="red")

这非常有效,我非常兴奋。我下一步要绘制这个图表,但这一次进一步分割机会,我不是按州,而是按州内的城市进行分割:

p<-ggplot()
p <- Map + geom_point( data=mydata, aes(x=long, y=lat, size = Opportunity
), color="gray6") + scale_size(name="Opportunity")

当我运行脚本时,出现此错误:

Error in eval(expr, envir, enclos) : object 'group' not found.

我用谷歌搜索了它,并尝试了此页面上列出的一些解决方案,但仍然一无所获。我还为上面的脚本创建了另一个 ggplot 并让它正确绘制,我只是希望能够将第二个图中的实际数据点放在我在第一个脚本中创建的热图中。

您可以在这里找到我的数据集的片段:

这是我的数据:

dput(head(mydata))
structure(list(label = structure(c(79L, 51L, 138L, 161L, 45L,
125L), .Label = c("ABILENE", "AIKEN", "ALBANY", "AMARILLO", "ANN ARBOR",
"ANNAPOLIS", "APPLE VALLEY", "ARLINGTON HEIGHTS", "ATHENS", "ATLANTA",
"AUGUSTA", "BAKERSFIELD", "BALTIMORE", "BANGOR", "BELLEVILLE",
"BETHLEHEM", "BEVERLY", "BIRMINGHAM", "BOCA RATON", "BOISE",
"BOSTON", "BOYNTON BEACH", "BRADENTON", "BRISTOL", "BRONX", "BROOKLYN",
"BRYAN", "BUFFALO", "BURLINGTON", "CAMDEN", "CHARLESTON", "CHARLOTTE",
"CHARLOTTESVILLE", "CHELSEA", "CHICAGO", "CHICAGO HEIGHTS", "CHICO",
"CLINTON", "CLYDE", "COLUMBUS", "COMMERCE TOWNSHIP", "CORBIN",
"CORONA", "CROWN POINT", "DALLAS", "DARBY", "DARIEN", "DECATUR",
"DENISON", "DENVER", "DETROIT", "DOVER", "DUARTE", "DUBUQUE",
"DURHAM", "EAU CLAIRE", "EDINBURG", "EFFINGHAM", "ELMHURST",
"ENID", "EUREKA", "EVANSTON", "FLUSHING", "FORT LAUDERDALE",
"FORT WORTH", "FREDERICKSBURG", "FRESNO", "FULLERTON", "GENEVA",
"GLENDALE", "GRAND RAPIDS", "GREELEY", "GREEN BAY", "GREENVILLE",
"HACKENSACK", "HAMMOND", "HANFORD", "HERSHEY", "HOUSTON", "INDEPENDENCE",
"INDIANAPOLIS", "JACKSON", "JACKSONVILLE", "JAMAICA", "JONESBORO",
"JUPITER", "KANSAS CITY", "LA CROSSE", "LAFAYETTE", "LAKEWOOD",
"LANGHORNE", "LANSING", "LAREDO", "LAS VEGAS", "LAURINBURG",
"LEONARDTOWN", "LEXINGTON", "LIVINGSTON", "LIVONIA", "LOMA LINDA",
"LONG BRANCH", "LONGVIEW", "LOS ANGELES", "LOUISVILLE", "LOVELAND",
"LUBBOCK", "LYNCHBURG", "MADISON", "MADISONVILLE", "MANCHESTER",
"MANHASSET", "MANKATO", "MASON CITY", "MAYWOOD", "MELBOURNE",
"MEMPHIS", "MESA", "MIAMI", "MIAMI BEACH", "MIAMISBURG", "MICHIGAN CITY",
"MIDDLETON", "MIDLAND", "MILWAUKEE", "MINNEAPOLIS", "MISHAWAKA",
"MISSION VIEJO", "MOBILE", "MONROEVILLE", "MONTCLAIR", "MORRISTOWN",
"MUSKEGON", "NAPA", "NATRONA HEIGHTS", "NEEDHAM", "NEW ALBANY",
"NEW HYDE PARK", "NEW YORK", "NEWARK", "NORFOLK", "NORRISTOWN",
"NORTH WILKESBORO", "ODESSA", "OMAHA", "ORANGE", "OXFORD", "PADUCAH",
"PALMDALE", "PANAMA CITY", "PARADISE", "PHILADELPHIA", "PHOENIX",
"PITTSBURGH", "PLANO", "POCATELLO", "PONTIAC", "PORTLAND", "POUGHKEEPSIE",
"PRESQUE ISLE", "RALEIGH", "ROCHESTER", "SAINT HELENA", "SAINT LOUIS",
"SAN ANGELO", "SAN FRANCISCO", "SANTA ROSA", "SHELBY", "SHERMAN",
"SILVER SPRING", "SIMI VALLEY", "SONORA", "SOUTHAVEN", "SPARTANBURG",
"SPRINGFIELD", "STATEN ISLAND", "STUART", "SYLVA", "SYRACUSE",
"TEMPLE", "TOMS RIVER", "TRENTON", "TUCSON", "TUSCALOOSA", "TYLER",
"VERO BEACH", "VISALIA", "WACO", "WASHINGTON", "WAYCROSS", "WEST PALM BEACH",
"WHITEVILLE", "WICHITA FALLS", "WILDOMAR", "WILMINGTON", "WINFIELD",
"WINSTON SALEM", "YONKERS", "YORK"), class = "factor"), region = structure(c(34L,
19L, 28L, 20L, 34L, 20L), .Label = c("al", "ar", "az", "ca",
"co", "dc", "de", "fl", "ga", "ia", "id", "il", "in", "ks", "ky",
"ma", "md", "me", "mi", "mn", "mo", "ms", "nc", "ne", "nh", "nj",
"nv", "ny", "oh", "ok", "pa", "sc", "tn", "tx", "va", "wi", "wv"
), class = "factor"), lat = c(29.9519265, 42.3486635, 40.7305991,
44.0226213, 32.7801052, 44.9772995), long = c(-95.54091698, -83.0567375,
-73.9865812, -92.4630094, -96.8000082, -93.2654692), Opportunity = c(20.4723937,
14.15191147, 14.06937574, 13.5368484, 11.46484222, 11.16776426
), Group = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("label", "region",
"lat", "long", "Opportunity", "Group"), row.names = c(NA, 6L), class = "data.frame")

第一个图像对应于 mydata,第二个图像对应于 tfmerged!

最佳答案

您现在的问题是您在顶层指定aes(group=group)(即在ggplot(...)内部),因此当当您添加使用不同数据集的 geom_point 时,即使您未指定 group<,ggplot 也会尝试将顶级美学应用到该新图层 那个的审美。三种解决方案:

  • ggplot(aes(...)) 中删除有问题的美学,并将其移至直接应用的图层
  • 对于不使用这些全局美感的图层,强制它们使用固定值(例如 geom_point(data=mydata, aes(group=1, fill=1, ...))
  • mydata 添加一个(可能)虚拟 group 变量

编辑:现在您提供了数据,这是一个可重现的解决方案(请注意,您没有包含 tf,所以我只是使用 state.x77 中的人口作为填充对于多边形:

library(ggplot2)
library(maps)
states <- map_data("state")
states$pop <- state.x77[match(states$region, tolower(rownames(state.x77)))]
tfmerged <- states
Map <- ggplot(
tfmerged,
aes(long,lat, fill=pop, group=region)) +
geom_polygon() + coord_equal() +
scale_fill_gradient(low="white", high="red")

Map + geom_point(
data=mydata, aes(x=long, y=lat, fill=1, group=1, size=Opportunity),
color="gray6") +
scale_size(name="Opportunity")

请注意,这只会添加属于 dput 数据集一部分的数据。

enter image description here

关于r - 在ggplot中绘制两个图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com