问题描述
我想知道如何在大小和颜色来自相同数据的绘图中为 scale_size() {ggplot2}
中的 size_scale 着色.
I would like to know how to colorize the size_scale in scale_size() {ggplot2}
in a plot where the size and color are from the same data.
示例:
library(ggplot2)
df<-as.data.frame(cbind(rep(1:10,10),
rep(1:10,each=10),
rnorm(100)))
ggplot(df,aes(V1,V2))+
geom_point(aes(colour=V3,size=V3))+
scale_colour_gradient(low="grey", high="black")+
scale_size(range=c(1,10))
如您所见,V3
的颜色和数据点大小是相同的.如何将颜色渐变合并到大小比例(除非在 Illustrator 等程序中手动执行此操作...)?谢谢!
As you can see the V3
is the same for the color and the size of the data points. How can I merge the color gradient to the size scale (except do this manually in program such as Illustrator...)? Thanks!
推荐答案
使用ggplot2
的guides()
函数.在这种情况下:
Use the guides()
function of ggplot2
. In this case:
ggplot(df,aes(V1,V2))+
geom_point(aes(colour=V3,size=V3))+
scale_colour_gradient(low="grey", high="black")+
scale_size(range=c(1,10)) +
guides(color=guide_legend(), size = guide_legend())
ggplot2 会尝试为您整合尺度.在这种情况下不是,因为色阶的默认指南是颜色条,而尺寸的默认指南是普通图例.一旦您将它们都设置为图例,ggplot 2 就会接管并将它们组合起来.
ggplot2 will try to integrate the scales for you. It doesn't in this case because the default guide for a color scale is a colorbar and the default guide for size is a normal legend. Once you set them both to be legends, ggplot 2 takes over and combines them.
这篇关于如何将颜色和大小的比例组合成一个图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!