【问题标题】:Plot doesn't show negative values绘图不显示负值
【发布时间】:2019-07-10 14:39:57
【问题描述】:

我正在绘制显示不同国家盈亏的世界地图。 我已经绘制了地图,我准备并加入了数据,我在最后一步。

我的数据集是“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)。 您知道要在代码中更改什么吗?

【问题讨论】:

  • Marta New,欢迎来到 SO!您收到了反对票,因为您的问题既不可重现(我们没有您的数据样本),我们也看不到问题(没有图像!)。请使这个问题可重现,包括样本数据(最好是我们可以用dput(head(x))轻松复制的数据),以及显示问题的图表使用你的样本数据给我们。这意味着您需要找到足够具体的数据样本来显示问题,但又不能大到导致网页臃肿。推荐阅读:stackoverflow.com/questions/5963269
  • 感谢 Marta New 添加数据 - 但是,这不会重现示例!在发布问题和代码之前,我始终建议在空会话中运行它 - 在给定的示例数据上运行代码时,您将看到结果

标签: r ggplot2 statistics


【解决方案1】:

很遗憾,您的问题无法重现,因此我创建了一些不同的示例数据。我还建议使用“世界地图”和geom_map 而不是geom_polygon(见下文)。 我认为您的情节中的问题是缺少值和scale_..._gradient 中缺少正确限制的组合。使用scale_...gradientn 会带来另一个问题——不对称中点。 (see here)

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 package (v0.3.0) 于 2019 年 7 月 11 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-02
    • 2020-07-15
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多