显示geom_text图层的单独图例?

显示geom_text图层的单独图例?,第1张

概述我有以下情节: library(ggplot2)ib<- data.frame( category = factor(c("Cat1","Cat2","Cat1", "Cat1", "Cat2","Cat1","Cat1", "Cat2","Cat2")), city = c("CITY1","CITY1","CITY2","CITY3", "CITY3","CITY4 我有以下情节:

library(ggplot2)ib<- data.frame(  category =   factor(c("Cat1","Cat2","Cat1","Cat2")),city =       c("CITY1","CITY1","CITY2","CITY3","CITY4","CITY5","CITY6","CITY7"),median =     c(1.3560,2.4830,0.7230,0.8100,3.1480,1.9640,0.6185,1.2205,2.4000),samplesize = c(851,1794,47,189,185,9,94,16,65)  )p<-ggplot(data=ib,aes(x=city,y=category,size=median,colour=category,label=samplesize)) +  geom_point(Alpha=.6) +  scale_area(range=c(1,15)) +  scale_colour_hue(guIDe="none") +  geom_text(aes(size = 1),colour="black")p

(我正在绘制与中值成比例的圆圈,并用表示样本大小的文本标签覆盖.图像在http://imgur.com/T82cF)

有没有办法分开两个传说?我想要一个传说(标记为“中位数”)给出圆圈的比例,另一个传奇带有单个字母“a”(或者甚至更好的数字),我可以标注“样本大小”.由于这两个属性之间没有任何关联,因此将它们捆绑在同一个图例中是没有意义的.

我尝试了各种各样的组合,但我能想到的最好的是完全放弃文本传说:)

谢谢你的回答!

解决方法 更新后的scale_area已被弃用;而是使用scale_size. gtable函数gtable_filter()用于提取图例.修改后的代码用于替换其中一个图例中的默认图例键.

如果您仍然在寻找问题的答案,那么这里似乎可以完成您想要的大部分内容,尽管这有点像黑客.图例中的符号可以使用kohske’s comment here进行更改

困难在于尝试应用两种不同大小的映射.所以,我已经在美学陈述中留下了点大小映射,但从审美陈述中删除了标签大小映射.这意味着必须根据samplesize的因子版本(fsamplesize)的离散值来设置标签大小.生成的图表几乎是正确的,除了未绘制标签大小的图例(即samplesize).为了解决这个问题,我绘制了一个图表,其中包含根据samplesize的因子版本(但忽略点大小映射)的标签大小映射,以便提取其图例,然后可以将其插入到第一个图表中.

## Your dataib<- data.frame(  category =   factor(c("Cat1",65)  )## Load packageslibrary(ggplot2)library(grIDExtra)library(gtable)library(grID)##  Obtain the factor version of samplesize.   ib$fsamplesize = cut(ib$samplesize,breaks = c(0,100,1000,Inf))## Obtain plot with dot size mapped to median,the label insIDe the dot set ## to samplesize,and the size of the label set to the discrete levels of the factor## version of samplesize. Here,I've selected three sizes for the labels (3,6 and 10)## corresponding to samplesizes of 0-100,100-1000,>1000. The sizes of the labels are## set using three call to geom_text - one for each size.p <- ggplot(data=ib,y=category)) +   geom_point(aes(size = median,colour = category),Alpha = .6) +   scale_size("Median",range=c(0,15)) +   scale_colour_hue(guIDe = "none") + theme_bw()p1 <- p +   geom_text(aes(label = ifelse(samplesize > 1000,samplesize,"")),size = 10,color = "black",Alpha = 0.6) +  geom_text(aes(label = ifelse(samplesize < 100,size = 3,Alpha = 0.6) +  geom_text(aes(label = ifelse(samplesize > 100 & samplesize < 1000,size = 6,Alpha = 0.6)## Extracxt the legend from p1 using functions from the grIDExtra packageg1 = ggplotGrob(p1) leg1 = gtable_filter(g1,"guIDe-Box")## Keep p1 but dump its legendp1 = p1 + theme(legend.position = "none")## Get second legend - size of the label.## Draw a dummy plot,using fsamplesize as a size aesthetic. Note that the label sizes are## set to 3,6,and 10,matching the sizes of the labels in p1. dummy.plot = ggplot(data = ib,aes(x = city,y = category,label = samplesize)) +  geom_point(aes(size = fsamplesize),colour = NA) +  geom_text(show.legend = FALSE) + theme_bw() +  guIDes(size = guIDe_legend(overrIDe.aes = List(colour = "black",shape = utf8ToInt("N")))) +scale_size_manual("Sample Size",values = c(3,10),breaks = levels(ib$fsamplesize),labels = c("< 100","100 - 1000","> 1000"))## Get the legend from dummy.plot using functions from the grIDExtra packageg2 = ggplotGrob(dummy.plot) leg2 = gtable_filter(g2,"guIDe-Box")## Arrange the three components (p1,leg1,leg2) using functions from the grIDExtra package## The two legends are arranged using the inner arrangeGrob function. The resulting## chart is then arranged with  p1 in the outer arrrangeGrob function.ib.plot = arrangeGrob(p1,arrangeGrob(leg1,leg2,nrow = 2),ncol = 2,wIDths = unit(c(9,2),c("null","null")))## Draw the graphgrID.newpage()grID.draw(ib.plot)
总结

以上是内存溢出为你收集整理的显示geom_text图层的单独图例?全部内容,希望文章能够帮你解决显示geom_text图层的单独图例?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063957.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存