问题描述
我使用 ggplot2 (版本0.9.3.1)在R(版本R版本3.0.1(2013-05-16))中生成了一个简单的图表,一组数据的相关系数。目前,绘图右侧的图例颜色条是整个绘图尺寸的一小部分。
我希望图例颜色条与图的高度相同。我认为我可以使用 legend.key.height 来做到这一点,但我发现事实并非如此。我调查了 grid package unit 函数,发现里面有一些标准化单元,但是当我尝试它们时( unit(1,npc)),颜色条显得太高而离开页面。
如何让图例与图表本身的高度相同?
完整的自包含示例是下面:
$ $ p $ #加载所需的库
库(ggplot2)
库(网格)$ b $生成一个样本数据集合
variables = c(Var1,Var2,Var3)
$ data = matrix(runif(9,-1,1),3,3)
diag(data)= 1
colnames(data)=变量
rownames(data)=变量
#生成绘图
corrs = data
ggplot(melt(corrs),aes(x = Var1,y = Var2,fill = value))+
geom_tile()+
geom_text(parse = TRUE,aes(label = sprintf(%。2f,value)),size = 3,color =white)+
theme_bw()+
主题(panel.border = element_blank(),
axis.text.x = element_text(angle = 90,vjust = 0.5,hjust = 1),
aspect.ratio = 1,
legend.position =right,
legend.key.height = unit(1,inch))+
labs(x =,y =,fill =,title =Correlation Coefficients)+
scale_fill_gradient2(limits = c(-1,1),expand = c(0,0),
low =静音(红色),
mid =黑色,
高=静音(蓝色))
看起来相当棘手,最接近的是这个,
## panel height是1null,所以我们通过(1,npc) - sum(ggplotGrob(plot)[[heights])从1npc
##和1line减去其他高度的默认绘图边距
panel_height = unit ] - - 单位(1,line)
绘图+指南(fill = guide_colorbar(barheight = panel_height))
不幸的是,垂直理由是a关闭。
I have generated a simple plot in R (version R version 3.0.1 (2013-05-16)) using ggplot2 (version 0.9.3.1) that shows the correlation coefficients for a set of data. Currently, the legend colorbar on the right side of the plot is a fraction of the entire plot size.
I would like the legend colorbar to be same height as the plot. I thought that I could use the legend.key.height to do this, but I have found that is not the case. I investigated the grid package unit function and found that there were some normalized units in there but when I tried them (unit(1, "npc")), the colorbar was way too tall and went off the page.
How can I make the legend the same height as the plot itself?
A full self contained example is below:
# Load the needed libraries library(ggplot2) library(grid) library(scales) library(reshape2) # Generate a collection of sample data variables = c("Var1", "Var2", "Var3") data = matrix(runif(9, -1, 1), 3, 3) diag(data) = 1 colnames(data) = variables rownames(data) = variables # Generate the plot corrs = data ggplot(melt(corrs), aes(x = Var1, y = Var2, fill = value)) + geom_tile() + geom_text(parse = TRUE, aes(label = sprintf("%.2f", value)), size = 3, color = "white") + theme_bw() + theme(panel.border = element_blank(), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1), aspect.ratio = 1, legend.position = "right", legend.key.height = unit(1, "inch")) + labs(x = "", y = "", fill = "", title = "Correlation Coefficients") + scale_fill_gradient2(limits = c(-1, 1), expand = c(0, 0), low = muted("red"), mid = "black", high = muted("blue"))
It seems quite tricky, the closest I got was this,
## panel height is 1null, so we work it out by subtracting the other heights from 1npc ## and 1line for the default plot margins panel_height = unit(1,"npc") - sum(ggplotGrob(plot)[["heights"]][-3]) - unit(1,"line") plot + guides(fill= guide_colorbar(barheight=panel_height))
unfortunately the vertical justification is a bit off.
这篇关于我怎样才能让ggplot2的图例和我的情节一样高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!