[英]ggplot - How to conditional color points in a map
So I have a data frame that has all the information from climate_extremes.RData along with the city and country name, no.所以我有一个数据框,其中包含来自 climate_extremes.RData 的所有信息以及城市和国家名称,不。 population, latitude, longitude (from packages 'maps', data (world.cities) ) All of it it's joined, everything good.人口,纬度,经度(来自“地图”包,数据(world.cities))所有这些都加入了,一切都很好。
In the first bit of code I wanted to have a world map with all the cities as dots.在第一段代码中,我想要一个以所有城市为点的世界 map。 It worked.有效。
My problem comes on the second part where I had tried to have every city dot coloured according by the wsdi values.我的问题出现在第二部分,我试图根据 wsdi 值为每个城市点着色。 At the same time the city has to be grouped based on scenario and year.同时,必须根据场景和年份对城市进行分组。
I do not really understand why I get this error when everything is specified in the code?我真的不明白为什么在代码中指定了所有内容时会出现此错误? I am quite new to R and trying to learn it through a platform but... yeah.我对 R 还很陌生,我想通过一个平台来学习它,但是……是的。
Error in check_aesthetics(): : Aesthetics must be either length 1 or the same as the data (939): x and y check_aesthetics() 中的错误::美学必须是长度 1 或与数据相同 (939):x 和 y
` `
map <- ggplot() + borders("world",fill="white",colour="gray80") + geom_point(aes(x=data_frame1$lon, y=data_frame1$lat), colour ="blue", size = 0.003)
data_frame1 %>% group_by(city,wsdi) %>% filter(scenario == 'historical' & year == '1997') %>%
select(city) %>%
ggplot() + borders("world",fill="white",colour="gray80") +
geom_point(aes(x=lon, y=lat), fill = wsdi , size = 0.03)
` `
Thank you in advance!先感谢您!
I've also tried with color rather than fill, and tried to specify from where it is (like data_frame1&wsdi), same error.我也尝试过使用颜色而不是填充,并尝试指定它的位置(如 data_frame1&wsdi),同样的错误。 Also wsdi is a column with numerical values and it has a corespondent to every city. wsdi 也是一个带有数值的列,它对每个城市都有一个对应者。
The praise goes to @Isaiah for pointing out the answer.感谢@Isaiah 指出了答案。 You need to understand how {ggplot2}
uses layers and maps variable to aesthetics.您需要了解{ggplot2}
如何使用图层并将变量映射到美学。
Please also note that {ggplot2}
works nicely with data frames.另请注意, {ggplot2}
可以很好地处理数据框。 So you do not need to assign the vectors to the x- and y-aesthetics.因此,您无需将向量分配给 x 和 y 美学。
data数据
We simulate your wsdi (climate data) by assigning random integers to your data frame.我们通过将随机整数分配给您的数据框来模拟您的 wsdi(气候数据)。
0. data frame operations 0.数据框操作
Working with data frames: You group and filter, then pull the variable city.使用数据框:分组和过滤,然后拉取变量城市。
Please note that with ... %>% select(city)
you truncate your data frame to this "single" column.请注意,使用... %>% select(city)
您会将数据框截断到此“单个”列。
Note2: think about "ungrouping" data frames once you have done your calculations.注 2:完成计算后,请考虑“取消分组”数据框。 This might hurt you with other operations.这可能会伤害您的其他操作。
library(dplyr) # data crunching
set.seed(666) # set seed to replicate random draws
df1 <- maps::world.cities %>%
mutate(wsdi = sample(1:10 #simulated values of climate
, nrow(maps::world.cities) #length of vector to input
, replace = TRUE) #identical values are possible
)
head(df1)
name country.etc pop lat long capital wsdi
1 'Abasan al-Jadidah Palestine 5629 31.31 34.34 0 5
2 'Abasan al-Kabirah Palestine 18999 31.32 34.35 0 9
3 'Abdul Hakim Pakistan 47788 30.55 72.11 0 1
4 'Abdullah-as-Salam Kuwait 21817 29.36 47.98 0 2
5 'Abud Palestine 2456 32.03 35.07 0 3
6 'Abwein Palestine 3434 32.03 35.20 0 5
1. Fixed colors - your first example that worked 1. 修复了 colors - 你的第一个有效的例子
ggplot() +
borders("world", fill = "white", colour = "grey80") + # specific ggplot layer for world maps
geom_point( data = df1 # our data frame for this layer
#-------- with aesthetic mapping you assign "varying" values to graph properties
, aes(x = long, y = lat) # mapping of variables to aesthetics (here: x- and y-position on our graph)
#-------- then you can assign "fixed" (aka static) properties
, colour = "blue" # make all points "blue"
, size = 0.003 # plot all points with size 0.003
)
2. Have color varying with value of wsdi variable 2. 颜色随着 wsdi 变量的值而变化
As mentioned by @Isaiah, you want to "map" the point color to a "changing" variable.正如@Isaiah 所提到的,您想将点颜色“映射”到“变化的”变量。 Thus, we can no longer use a "fixed" assignment.因此,我们不能再使用“固定”赋值。 This is one of the characteristics of the grammar or graphics
.这是grammar or graphics
的特征之一。
We created the wsdi
variable in our data frame mutate above.我们在上面的数据框 mutate 中创建了wsdi
变量。
As a reminder ggplot works well with data frames.提醒一下,ggplot 可以很好地处理数据框。 The simulated df1
contains the desired wsdi
variable/column.模拟的df1
包含所需的wsdi
变量/列。 Thus we can work with this data frame.因此我们可以使用这个数据框。
ggplot() +
borders("world", fill = "white", colour = "grey80") +
geom_point(data = df1
#-------- with aesthetic mappings ... now we add varying color! -------
, aes(x = long, y = lat
, colour = wsdi # color property mapped to variable
)
#------- any fixed properties of our graph -----------------------------
, size = 0.003)
Note that wsdi
is a continuous
variable.请注意, wsdi
是一个continuous
变量。 Thus, ggplot uses a spectral palette.因此,ggplot 使用光谱调色板。 If you want to have dicrete
colors (ie you group certain ranges of wsdi
or factorise the values we simulated) you get a more colorful representation.如果你想要dicrete
的 colors(即你将wsdi
的某些范围分组或分解我们模拟的值)你会得到一个更丰富多彩的表示。
You can then move on to beautify the plot, eg add titles, change theme, etc.然后您可以继续美化 plot,例如添加标题、更改主题等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.