相关疑难解决方法(0)

在ggplot2中,如何添加其他图例?

我正在尝试ggplot2使用来自不同数据帧的数据构建映射.

library(maptools)

xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

xx.sub1 <- subset(xx, xx$FIPSNO < 37010)
xx.sub2 <- subset(xx, xx$FIPSNO > 37010)

xx.sub1@data$id <- rownames(xx.sub1@data)
xx.sub1.points <- fortify(xx.sub1, region="id")
xx.sub1.df = plyr::join(xx.sub1.points, xx.sub1@data, by="id")

xx.sub2@data$id <- rownames(xx.sub2@data)
xx.sub2.points <- fortify(xx.sub2, region="id")
xx.sub2.df = plyr::join(xx.sub2.points, xx.sub2@data, by="id")

ggplot(xx.sub2.df) + 
  aes(long, lat, fill = (SID79/BIR79)*1000, group = group) + 
  geom_polygon() + geom_path(color="grey80") +
  coord_equal() + 
  scale_fill_gradientn(colours = RColorBrewer::brewer.pal(7, "YlOrBr")) +
  geom_polygon(data = xx.sub1.df, fill = "grey50") + 
  geom_path(data = xx.sub1.df, color="grey80") …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

21
推荐指数
1
解决办法
9531
查看次数

ggplot2:如何在图例键中分离geom_polygon和geom_line?

我想要:

  • 删除geom_polygon图例键中的行和
  • 删除geom_line图例键周围的边框.

期望的输出将是

在此输入图像描述

到目前为止我失败的尝试.预先感谢您的任何帮助!

library(ggplot2)

set.seed(1337)

dat <- structure(list(id = structure(c(2L, 2L, 2L, 2L), 
                                    .Label = c("1.1", "1.2", "1.3", "2.1", "2.2", "2.3"), 
                                    class = "factor"), 
                     value = c(3.1, 3.1, 3.1, 3.1),
                     x = c(2.2, 1.1, 1.2, 2.5), 
                     y = c(0.5, 1, 2.1, 1.7)), 
                class = "data.frame", 
                row.names = c(NA, -4L))

line <- data.frame(
  x = cumsum(runif(50, max = 0.1)),
  y = cumsum(runif(50, max = 0.1))
)

ggplot(dat, aes(x = x, y = y)) +
  geom_polygon(aes(color = "Border", group = …
Run Code Online (Sandbox Code Playgroud)

r legend ggplot2

6
推荐指数
1
解决办法
446
查看次数

标签 统计

ggplot2 ×2

r ×2

legend ×1