如何在ggplot2中分别缩放线和点的大小
How to scale the size of line and point separately in ggplot2
代码如下:
set.seed(123)
d1=data.frame(x=runif(10),y=runif(10),z=runif(10,1,10))
d2=data.frame(x=runif(10),y=runif(10),z=runif(10,100,1000))
ggplot()+geom_point(aes(x,y,size=z),data=d1)+
geom_line(aes(x,y,size=z),data=d2)
结果是这样的:
点的大小太小,所以我想将其大小更改 scale_size
。但是,似乎线和点都受到影响。所以我想知道是否有一种方法可以使用单独的图例分别缩放线和点?
我能想到的两种方法是 1) 结合两个传奇 grob 或 2) 破解另一个传奇美学。 @Mike Wise 在上面的评论中提到了这两个。
方法 #1: 使用 grobs 将 2 个独立的图例组合在同一个图中。
我使用了这个 answer to grab the legend. Baptiste's arrangeGrob vignette 的代码是一个有用的参考。
library(grid); library(gridExtra)
#Function to extract legend grob
g_legend <- function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
legend
}
#Create plots
p1 <- ggplot()+ geom_point(aes(x,y,size=z),data=d1) + scale_size(name = "point")
p2 <- ggplot()+ geom_line(aes(x,y,size=z),data=d2) + scale_size(name = "line")
p3 <- ggplot()+ geom_line(aes(x,y,size=z),data=d2) +
geom_point(aes(x,y, size=z * 100),data=d1) # Combined plot
legend1 <- g_legend(p1)
legend2 <- g_legend(p2)
legend.width <- sum(legend2$width)
gplot <- grid.arrange(p3 +theme(legend.position = "none"), legend1, legend2,
ncol = 2, nrow = 2,
layout_matrix = rbind(c(1,2 ),
c(1,3 )),
widths = unit.c(unit(1, "npc") - legend.width, legend.width))
grid.draw(gplot)
printing 的注意事项:使用 arrangeGrob()
而不是 grid.arrange()
。我不得不使用 png; grid.draw; dev.off
来保存 (arrangeGrob) 图。
方法 #2: 破解另一个美学传奇。
MilanoR 在这方面有很好的 post,关注颜色而不是大小。
更多 SO 示例:1) discrete colour and 2) colour gradient.
#Create discrete levels for point sizes (because points will be mapped to fill)
d1$z.bin <- findInterval(d1$z, c(0,2,4,6,8,10), all.inside= TRUE) #Create bins
#Scale the points to the same size as the lines (points * 100).
#Map points to a dummy aesthetic (fill)
#Hack the fill properties.
ggplot()+ geom_line(aes(x,y,size=z),data=d2) +
geom_point(aes(x,y, size=z * 100, fill = as.character(z.bin)),data=d1) +
scale_size("line", range = c(1,5)) +
scale_fill_manual("points", values = rep(1, 10) ,
guide = guide_legend(override.aes =
list(colour = "black",
size = sort(unique(d1$z.bin)) )))
我是编程小白,不过你可以试试这个方法。如您所见,我的代码使用了点和路径。我定义了路径数量长度的向量。我的线的大小为 1。然后我在该向量的后面添加我的点的大小。
size_vec<-c(rep(1, length(unique(Data$Satellite))), 1.4, 4.6, 4.2, 5.5)
plot <- ggplot(data) +
geom_point(aes(x = x_cor, y = y_cor, shape=Type, size=Type)) +
geom_path(aes(x = x_cor, y = y_cor, group = Tour, size=factor(Satellite))) +
scale_size_manual(values = size_vec, guide ='none')
代码如下:
set.seed(123)
d1=data.frame(x=runif(10),y=runif(10),z=runif(10,1,10))
d2=data.frame(x=runif(10),y=runif(10),z=runif(10,100,1000))
ggplot()+geom_point(aes(x,y,size=z),data=d1)+
geom_line(aes(x,y,size=z),data=d2)
结果是这样的:
点的大小太小,所以我想将其大小更改 scale_size
。但是,似乎线和点都受到影响。所以我想知道是否有一种方法可以使用单独的图例分别缩放线和点?
我能想到的两种方法是 1) 结合两个传奇 grob 或 2) 破解另一个传奇美学。 @Mike Wise 在上面的评论中提到了这两个。
方法 #1: 使用 grobs 将 2 个独立的图例组合在同一个图中。
我使用了这个 answer to grab the legend. Baptiste's arrangeGrob vignette 的代码是一个有用的参考。
library(grid); library(gridExtra)
#Function to extract legend grob
g_legend <- function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
legend
}
#Create plots
p1 <- ggplot()+ geom_point(aes(x,y,size=z),data=d1) + scale_size(name = "point")
p2 <- ggplot()+ geom_line(aes(x,y,size=z),data=d2) + scale_size(name = "line")
p3 <- ggplot()+ geom_line(aes(x,y,size=z),data=d2) +
geom_point(aes(x,y, size=z * 100),data=d1) # Combined plot
legend1 <- g_legend(p1)
legend2 <- g_legend(p2)
legend.width <- sum(legend2$width)
gplot <- grid.arrange(p3 +theme(legend.position = "none"), legend1, legend2,
ncol = 2, nrow = 2,
layout_matrix = rbind(c(1,2 ),
c(1,3 )),
widths = unit.c(unit(1, "npc") - legend.width, legend.width))
grid.draw(gplot)
printing 的注意事项:使用 arrangeGrob()
而不是 grid.arrange()
。我不得不使用 png; grid.draw; dev.off
来保存 (arrangeGrob) 图。
方法 #2: 破解另一个美学传奇。
MilanoR 在这方面有很好的 post,关注颜色而不是大小。 更多 SO 示例:1) discrete colour and 2) colour gradient.
#Create discrete levels for point sizes (because points will be mapped to fill)
d1$z.bin <- findInterval(d1$z, c(0,2,4,6,8,10), all.inside= TRUE) #Create bins
#Scale the points to the same size as the lines (points * 100).
#Map points to a dummy aesthetic (fill)
#Hack the fill properties.
ggplot()+ geom_line(aes(x,y,size=z),data=d2) +
geom_point(aes(x,y, size=z * 100, fill = as.character(z.bin)),data=d1) +
scale_size("line", range = c(1,5)) +
scale_fill_manual("points", values = rep(1, 10) ,
guide = guide_legend(override.aes =
list(colour = "black",
size = sort(unique(d1$z.bin)) )))
我是编程小白,不过你可以试试这个方法。如您所见,我的代码使用了点和路径。我定义了路径数量长度的向量。我的线的大小为 1。然后我在该向量的后面添加我的点的大小。
size_vec<-c(rep(1, length(unique(Data$Satellite))), 1.4, 4.6, 4.2, 5.5)
plot <- ggplot(data) +
geom_point(aes(x = x_cor, y = y_cor, shape=Type, size=Type)) +
geom_path(aes(x = x_cor, y = y_cor, group = Tour, size=factor(Satellite))) +
scale_size_manual(values = size_vec, guide ='none')