r - 绘图不显示负值
问题描述
我正在绘制世界地图,显示不同国家的损益。我已经绘制了地图,我准备并加入了数据,我在最后一步。
我的数据集是“data_profit”,“Total_profit”是我使用的值(负值和正值)——它用于根据值用颜色填充地图。其余代码是地图绘制。
ditch_the_axes <- theme(
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank()
)
terra<-ggplot(data_profit, aes( x = long, y = lat, group = group )) +
geom_polygon(aes(x = long, y = lat,fill = Total_profit),color = "black")+
coord_fixed(1.3) +
theme_bw() +
ditch_the_axes+
scale_fill_gradient2( low="black", high="red", space ="Lab" )
data_profit <-
structure(list(long = c(-69.8991241455078, -69.8957061767578,
-69.9421920776367, -70.004150390625, -70.0661163330078, -70.0508804321289
), lat = c(12.4520015716553, 12.4229984283447, 12.4385251998901,
12.50048828125, 12.5469722747803, 12.5970697402954), group = c(1,
1, 1, 1, 1, 1), order = 1:6, region = c("Aruba", "Aruba", "Aruba",
"Aruba", "Aruba", "Aruba"), Total_profit = c(0, 0, 0, 0, 0, 0
)), row.names = c(NA, 6L), class = "data.frame")
所以,问题是最终的地图没有显示负值(应该是黑色和灰色的阴影)。我检查了“Total_profit”值是否为数字(使用 is.finite 和 is.numeric)。您知道要在代码中更改什么吗?
解决方案
不幸的是,您的问题无法重现,因此我创建了一些不同的示例数据。我还建议使用“世界地图”而geom_map
不是geom_polygon
(见下文)。我认为您的情节中的问题是缺失值和scale_..._gradient
. 使用scale_...gradientn
带来了另一个问题 - 不对称中点。(见这里)
library(tidyverse)
library(scales) #load this for the mid-point problem and using scales::rescale (see below)
WorldData <- map_data('world') #easier way to draw countries than using geom_polygon
# In the next steps I am merging your profit data with the world map. I am creating a different sample data frame
data_profit <- data.frame(region = sample(WorldData$region,6), Total_profit = c(-5, -3, 0, 3, 5, 10))
map_profit <- dplyr::left_join(WorldData, data_profit, by ='region')
#> Warning: Column `region` joining character vector and factor, coercing into
#> character vector
# This plot is the easier one - using scale_fill_gradient(...). No mid-point problem. Note I am specifying the NA color using na.value.
# I am also specifying the limits using limits = ... I am not hard coding this, giving more flexibility for future graphs.
ggplot() +
geom_map(data = map_profit, map = WorldData,
aes(x = long, y = lat, map_id = region, fill = Total_profit, na.rm = FALSE)) +
scale_fill_gradient(low="black", high="red", na.value = 'blue',
limits = c(min(map_profit$Total_profit), max(map_profit$Total_profit)))
#> Warning: Ignoring unknown aesthetics: x, y, na.rm
# The next plot comes with the problem of an asymmetric mid-point
# I therefore rescale the values using rescale
ggplot() +
geom_map(data = map_profit, map = WorldData,
aes(x = long, y = lat, map_id = region, fill = Total_profit, na.rm = FALSE)) +
scale_fill_gradientn(colors = c("black", 'white', "red"), na.value = 'blue',
values = rescale(c(min(map_profit$Total_profit, na.rm = TRUE),0, max(map_profit$Total_profit,na.rm = TRUE))),
limits = c(min(map_profit$Total_profit), max(map_profit$Total_profit)))
#> Warning: Ignoring unknown aesthetics: x, y, na.rm
由reprex 包(v0.3.0)于 2019-07-11 创建
推荐阅读
- ansible - Ansible playbook 可在缺少时在 IOS 设备上添加本地帐户
- ios - CIImage在SWIFT中的矩形到正方形
- python - Tkinter OOP 在实例方法中更新小部件
- ruby-on-rails - 查找与控制器中其他记录相关的记录(ruby on rails)
- rest - 无法在 Postman 中发送 POST 请求
- c# - 索引器通过引用获取还是获取+设置?
- gtk - G_BEGIN_DECLS 和 G_END_DECLS 是干什么用的?
- git - 删除 git 中所有暂存文件的最快方法是什么?
- scala - Spark:将 Array[Byte] 数据转换为 RDD 或 DataFrame
- c++ - unique_ptr 比较器的优先级队列