本文介绍了以小平面密度向ggplot添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在尝试使用ggplot制作密度图时遇到问题。 这里的数据看起来有点像。 require(ggplot2) require(plyr ) mms type = sample(as.factor(c(peanut,regular)),100,replace = TRUE) , color = sample(as.factor(c(red,green,yellow,brown)),100,replace = TRUE)) mms.cor plot< - ggplot(data = mms,aes(x = deliciousness))+ geom_density()+ facet_grid(type_color)+ geom_text(data = mms.cor,aes(x = 1.8,y = 5,label = n) =black,inherit.aes = FALSE,parse = FALSE) 用标签标记每个面工作得很好,除非每个方面的尺度不同。有人知道我怎样才能将标签放在同一位置,当每个刻面尺寸不同时? Best, daniel 解决方案类似这样的事情? plot< ; - ggplot(data = mms,aes(x = deliciousness))+ geom_density(aes(y = .. scaled ..))+ facet_grid(type〜color)+ geom_text(data = mms .cor,aes(x = 1.2,y = 1.2,label = n),color =black) plot 有是一种获得由ggplot内部设置的 scales =free限制的方法,但它涉及到对 grob (图形对象)。由于您似乎希望密度图具有相同的高度(???),您可以使用 aes(y = .. scaled ...)来实现。然后设置标签的位置很简单。 编辑(回应OP的评论) 黑客攻击 grob 。请注意,这利用了 gglpot 使用的内部结构。问题在于,随着新版本的发布,这可能会随时发生变化(实际上它已经与旧版本不同)。因此,不能保证此代码将来会有效。 plot geom_density()+ facet_grid(type〜color,scales =free)面板< - ggplot_build(绘图)[[panel]] $ b (范围)c(范围$ x.range,范围$ y.range))) colnames(限制) < - c(x.lo,x.hi,y.lo,y.hi) mms.cor< - cbind(mms.cor,limits) plot + geom_text(data = mms.cor,aes(x = x.hi,y = y.hi,label = n),hjust = 1,color =black) 基本思想是在没有文本的情况下生成 plot ,然后生成图形对象使用 ggplot_build(绘图)。由此我们可以提取x和y限制,并将它们绑定到 mms.cor 数据框中的标签。现在使用这些限制来渲染文本。 请注意,由于您没有使用 set,所以这些图与我之前的回答有所不同。 seed(...)在你的代码中生成数据集(我忘了添加它...)。I'm encountering a problem when trying to make a density plot with ggplot.The data look a bit like in the example here.require(ggplot2)require(plyr)mms <- data.frame(deliciousness = rnorm(100), type=sample(as.factor(c("peanut", "regular")), 100, replace=TRUE), color=sample(as.factor(c("red", "green", "yellow", "brown")), 100, replace=TRUE))mms.cor <- ddply(.data=mms, .(type, color), summarize, n=paste("n =", length(deliciousness)))plot <- ggplot(data=mms, aes(x=deliciousness)) + geom_density() + facet_grid(type ~ color) + geom_text(data=mms.cor, aes(x=1.8, y=5, label=n), colour="black", inherit.aes=FALSE, parse=FALSE)Labelling each facet with the labels work quite well unless the scales for each facet vary. Does anyone have an idea how I could achieve putting the labels at the same location when the scales per facet differ?Best,daniel 解决方案 Something like this?plot <- ggplot(data=mms, aes(x=deliciousness)) + geom_density(aes(y=..scaled..)) + facet_grid(type ~ color) + geom_text(data=mms.cor, aes(x=1.2, y=1.2, label=n), colour="black")plotThere is a way to get the limits set internally by ggplot with scales="free", but it involves hacking the grob (graphics object). Since you seem to want the density plots to have equal height (???), you can do that with aes(y=..scaled...). Then setting the location for the labels is straightforward.EDIT (Response to OP's comment)This is what I meant by hacking the grob. Note that this takes advantage of the internal structure used by gglpot. The problem is that this could change at any time with a new version (and in fact it is already different from older versions). So there is no guarantee this code will work in the future.plot <- ggplot(data=mms, aes(x=deliciousness)) + geom_density() + facet_grid(type ~ color, scales="free")panels <- ggplot_build(plot)[["panel"]]limits <- do.call(rbind,lapply(panels$ranges, function(range)c(range$x.range,range$y.range)))colnames(limits) <- c("x.lo","x.hi","y.lo","y.hi")mms.cor <- cbind(mms.cor,limits)plot + geom_text(data=mms.cor, aes(x=x.hi, y=y.hi, label=n), hjust=1,colour="black")The basic idea is to generate plot without the text, then build the graphics object using ggplot_build(plot). From this we can extract the x- and y-limits, and bind those to the labels in your mms.cor data frame. Now render the plot with the text, using these limits.Note that the plots are different from my earlier answer because you did not use set.seed(...) in your code to generate the dataset (and I forgot to add it...). 这篇关于以小平面密度向ggplot添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 12:48