【问题标题】:ggplot2: Legend won't showggplot2:图例不会显示
【发布时间】:2015-03-26 23:18:00
【问题描述】:

作为对@hrbrmstr 出色答案的回应而编辑:

我正在绘制 2 组国家/地区的地图,并且想要一个图例来配合它。由于我使用的是自己的数据,因此很难提供 MRE。在我之前的代码中,我使用 geom_polygon 绘制形状(从 shapefile 读取),因为 fortify 丢弃了关联数据框的附加数据:

ggplot() + aes(long, lat, group = group) + theme_bw() + 
  scale_x_continuous(limits = c(-15, 35)) +
  scale_y_continuous(limits = c(25, 75)) +
  scale_fill_manual("Affected?", labels = c("Yes", "No"),
                    values = c("gray10", "gray80")) +
  geom_polygon(data = affected.countries, fill = "gray10", color = "black") +
  geom_polygon(data = unaffected.countries, fill = "gray80", color = "black")

结果:

现在我尝试从@hrbrmstr 的剧本中获取一页。因为fortify 丢弃了我的其他列,所以我制作了原始数据的两个子集,它们属于SpatialPolygonsDataFrame 类。我fortify 他们并给了他们一个虚拟变量来显示我需要什么,然后尝试使用布尔列来绘制它们来控制填充:

affected.countries <- fortify(affected.countries)
affected.countries$affected <- T
unaffected.countries <- fortify(unaffected.countries)
unaffected.countries$affected <- F
# all.countries now contains column affected
all.countries <- rbind(affected.countries, unaffected.countries)
gg <- ggplot() + coord_map(xlim = c(-13, 35), ylim = c(32, 71)) + theme_bw()
# Base map
gg <- gg + geom_map(data = all.countries, map = all.countries,
                    aes(x = long, y = lat, map_id = id),
                    fill = NA, color="gray10")
# Base map looks OK
gg
# Add filled data
gg <- gg + geom_map(data = all.countries, map = all.countries,
                    aes(fill = affected, map_id = id),
                    fill="gray10")
# For some reason, everything is filled!
gg <- gg + scale_fill_manual("Affected?", labels = c("No", "Yes"),
                             values = c("gray80", "gray10"))
# And the legend isn't shown
gg

对于这些结果:

我认为问题在于我的fill 参数不在aes 中,但它就在这里。可悲的是,我没有看到像@hrbrmstr 的回答那样使用第二个数据框的方法,因为我的数据中没有适当的列,但我认为布尔列可以解决它。虽然我可以通过破解答案中的代码来做到这一点,但我更喜欢我自己的国家边界。

值得注意的是,如果我在aes 之外但在geom_polygon 调用中包含fill 参数,则填充工作正常,但未显示图例。如果我在aes 中指定颜色,则会显示一种看似随机的颜色。

我缺少什么原则?再次感谢!

【问题讨论】:

  • 您的fill= 不在aes 映射中
  • 这显示了我的图例,但标签或颜色不正确。
  • 您指定了两次填充aes(fill = affected), fill = "gray"。这些都是矛盾的。如果您需要图例,指定填写aes。要手动选择填充颜色,请使用scale_fill_manual(如您所愿)。
  • 谢谢!做到了。如果您将其发布为答案,我会接受:)

标签: r plot ggplot2


【解决方案1】:

这是一个更完整的示例,尤其是因为原始问题中没有使用投影或提供数据:

library(ggplot2)

europe <- map_data('world', 
                   region=c('Switzerland', 'Liechtenstein', 'Luxembourg',
                            'UK', 'Belgium', 'Norway', 'Germany', 'Ireland',
                            'Austria', 'France', 'Netherlands', 'Sweden',
                            'Denmark','Italy' ))

condition <- data.frame(region=c("Ireland", "France", "Netherlands",
                                 "Sweden", "Finland", "UK", "Germany"),
                        affected=c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE),
                        stringsAsFactors=FALSE)

gg <- ggplot()

# base map

gg <- gg + geom_map(data=europe, map=europe,
                    aes(x=long, y=lat, map_id=region),
                    color="#7f7f7f", fill="white", size=0.25)

# fills according to affected

gg <- gg + geom_map(data=condition, map=europe,
                    aes(fill=affected, map_id=region),
                    color="#7f7f7f", size=0.01)

# a minimally decent projection tho one of the conical ones is prbly better

gg <- gg + coord_map(xlim=c(-13, 35),  ylim=c(32, 71))

# fills for the aes mapping

gg <- gg + scale_fill_manual("Affected?", labels = c("No", "Yes"),
                             values = c("gray80", "gray10"))

# no need for axis labels since it's a map

gg <- gg + labs(x=NULL, y=NULL)
gg <- gg + theme_bw()
gg

正如 Gregor 所指出的,这是一个次优的投影(但由于原始帖子中没有投影,我不想只在默认的墨卡托之外添加一个投影)。如果您想知道为什么我们中的一些人对 SO 的预测如此着迷,请以 this one report 为例(来自许多仅针对欧洲预测的研讨会之一的完整文档)。

还有其他几个选择。首先是 Lambert Conic Conformal,您可以通过将 coord_map 更改为:

gg <- gg + coord_map("lambert", lat0=32, lat1=71, xlim=c(-13, 35), ylim=c(32, 71))

尽管链接到的文档不建议使用它,但吉尔伯特 (IMO) 也是一个不错的选择:

gg <- gg + coord_map("gilbert", xlim=c(-13, 35), ylim=c(32, 71))

PDF 还建议 Azimuth Equal Area 是首选选项:

gg <- gg + coord_map("azequalarea", xlim=c(-13, 35), ylim=c(32, 71))

【讨论】:

  • Awww,我很高兴向下滚动看看你会使用什么投影......默认情况下是墨卡托:(
  • 在“放弃”投影之前等待 OP 的初步反馈。但我会用一个漂亮的兰伯特圆锥形保形更新它(尽管对于这些纬度,它并没有太大变化)。
  • 感谢您的帮助!我仍然无法让它按照我想要的方式工作。适当地编辑了我的帖子。我还没有设置投影,因为我首先想要图例 - 我的目标是显示最少的代码,但感谢额外的制图技巧 :)
【解决方案2】:

我没有关注您所涉及的更新的全部问题。但也许它可以像在scale_fill_manual() 中设置guide = TRUE 一样简单。见Turning off some legends in a ggplot

【讨论】:

    猜你喜欢
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多