Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2:图例不会显示_R_Plot_Ggplot2 - Fatal编程技术网

R ggplot2:图例不会显示

R ggplot2:图例不会显示,r,plot,ggplot2,R,Plot,Ggplot2,编辑为对@hrbrmstr伟大答案的回应: 我正在绘制两组国家的地图,并希望有一个传说与之相伴。因为我使用自己的数据,所以很难提供MRE。在我以前的代码中,我使用geom_polygon绘制从shapefile读取的形状,因为fortify会丢弃附加数据和关联的数据帧: ggplot() + aes(long, lat, group = group) + theme_bw() + scale_x_continuous(limits = c(-15, 35)) + scale_y_con

编辑为对@hrbrmstr伟大答案的回应:

我正在绘制两组国家的地图,并希望有一个传说与之相伴。因为我使用自己的数据,所以很难提供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类。我加强了它们,给它们一个虚拟变量,显示我需要的内容,然后尝试使用布尔列绘制它们以控制填充:

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
对于这些结果:

我想问题是我的填充参数不是aes,但它在这里。遗憾的是,我看不到像@hrbrmstr的回答那样使用第二个数据帧的方法,因为我的数据中没有合适的列,但我认为布尔列可以解决这个问题。虽然我可以从答案中破解代码,但我更喜欢自己的国家边界

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


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

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

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
尽管链接到的文档不建议使用它,但Gilbert也是一个不错的选择:

gg <- gg + coord_map("gilbert", xlim=c(-13, 35), ylim=c(32, 71))
PDF还建议方位角相等区域是首选选项:

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

这里有一个更完整的例子,特别是因为在原始问题中没有使用投影或提供数据:

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
尽管链接到的文档不建议使用它,但Gilbert也是一个不错的选择:

gg <- gg + coord_map("gilbert", xlim=c(-13, 35), ylim=c(32, 71))
PDF还建议方位角相等区域是首选选项:

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

我不是在关注你所有的问题和更新。但也许它可以是一个简单的设置指南=真的比例填充手册。请参阅。

我不会关注您的所有问题以及相关更新。但也许它可以是一个简单的设置指南=真的比例填充手册。请参阅。

您的填充=不在aes映射中这使我的图例得以显示,但没有正确的标签或颜色。您可以指定填充两次aesfill=受影响,填充=灰色。这些是相互矛盾的。如果需要图例,请仅指定填充aes。要手动选择填充颜色,请在操作过程中使用“缩放填充”手动。谢谢!成功了。如果您将其作为答案发布,我将接受:您的填充=不在aes映射中这显示了我的图例,但没有正确的标签或颜色。您指定填充两次aesfill=受影响,填充=灰色。这些是相互矛盾的。如果需要图例,请仅指定填充aes。要手动选择填充颜色,请在操作过程中使用“缩放填充”手动。谢谢!成功了。如果你把它作为一个答案,我会接受的:啊,我很高兴能向下滚动,看看你会使用什么投影。。。默认情况下,墨卡托:在给出投影之前,正在等待OP的初始反馈。但是我会用一个很好的lambert圆锥共形tho更新它,对于这些纬度,它不会有太大的改变。我感谢你的帮助!不过,我还是无法让它按我想要的方式工作。我已经适当地编辑了我的文章。我还没有设置投影,因为我想先显示图例-我的目标是显示最少的代码,但感谢额外的制图技巧:啊,我很高兴向下滚动,看看你会使用什么投影。。。默认情况下,墨卡托:在给出投影之前,正在等待OP的初始反馈。但是我会用一个很好的lambert圆锥共形tho更新它,对于这些纬度,它不会有太大的改变。我感谢你的帮助!不过,我还是无法让它按我想要的方式工作。我已经适当地编辑了我的文章。我还没有设置投影,因为我首先想要的是图例-我的目标是显示最少的代码,但感谢额外的制图技巧: