R 为geom_文本层显示单独的图例?
我有以下情节:R 为geom_文本层显示单独的图例?,r,ggplot2,R,Ggplot2,我有以下情节: 库(ggplot2) ib这实际上并没有直接解决您的问题,但我可以用您描述的一般特征创建一个图表: ib$ss <- paste("n = ",ib$samplesize,sep = "") ggplot(data=ib, aes(x=city, y=category, size=median, colour=category, label=ss)) + geom_point(alpha=.6) + geom_text(size = 2, vjust = -1.2
库(ggplot2)
ib这实际上并没有直接解决您的问题,但我可以用您描述的一般特征创建一个图表:
ib$ss <- paste("n = ",ib$samplesize,sep = "")
ggplot(data=ib, aes(x=city, y=category, size=median, colour=category, label=ss)) +
geom_point(alpha=.6) +
geom_text(size = 2, vjust = -1.2,colour="black") +
scale_colour_hue(legend = FALSE)
ib$ss这也不能回答你的问题。我把samplesize
放在圆圈内。而且,samplesize
对我来说更像是一个注释,而不是一个图例。
但我认为您使用的是旧版本的ggplot2
。ggplot2
0.9.0版中有一些更改。我已经做了以下更改
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)) + # range instead of to
scale_colour_hue(guide = "none") + # guide instead of legend
geom_text(size = 2.5, colour="black")
p
p更新的scale\u区域
已被弃用<代码>比例大小
改用。gtable
函数gtable_filter()
用于提取图例。以及用于替换其中一个图例中的默认图例键的修改代码
如果你仍然在寻找你的问题的答案,这里有一个似乎能满足你大部分需求的答案,尽管有些地方有点不对劲。图例中的符号可以使用更改
困难在于尝试应用两种不同大小的映射。因此,我将点大小映射保留在美学声明中,但从美学声明中删除了标签大小映射。这意味着必须根据samplesize(fsSampleSize)的因子版本的离散值设置标签大小。生成的图表几乎正确,只是没有绘制标签大小(即samplesize)的图例。为了解决这个问题,我根据samplesize的factor版本绘制了一个包含标签大小映射的图表(但忽略了点大小映射),以便提取其图例,然后将其插入到第一个图表中
## Your data
ib<- data.frame(
category = factor(c("Cat1","Cat2","Cat1", "Cat1", "Cat2","Cat1","Cat1", "Cat2","Cat2")),
city = c("CITY1","CITY1","CITY2","CITY3", "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)
)
## Load packages
library(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, aes(x=city, 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, samplesize, "")),
size = 3, color = "black", alpha = 0.6) +
geom_text(aes(label = ifelse(samplesize > 100 & samplesize < 1000, samplesize, "")),
size = 6, color = "black", alpha = 0.6)
## Extracxt the legend from p1 using functions from the gridExtra package
g1 = ggplotGrob(p1)
leg1 = gtable_filter(g1, "guide-box")
## Keep p1 but dump its legend
p1 = 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, 6, 10),
breaks = levels(ib$fsamplesize), labels = c("< 100", "100 - 1000", "> 1000"))
## Get the legend from dummy.plot using functions from the gridExtra package
g2 = 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 graph
grid.newpage()
grid.draw(ib.plot)
##您的数据
IbThank@joran,这是可行的,但是,我的最后一个地块(大约30个城市和~100个类别)的空间非常有限,所以我更愿意在地块之外描述这些数字的含义。我同意这不是最好的解决方案,但我负担不起情节本身的那么多文字…@Krizbi我想。尽管如此,绘图>(30 x 100)上的单个数字是否比在每个数字前面加上“n=”更具可读性?您确实有一点-我肯定已经在这个绘图的“信息过载”区域(即使没有数字):)。因此,我希望在额外的文本中尽可能做到斯巴达。A“n=”在平均为2-3位的数字上添加4个字符空格,因此我认为影响和视觉混乱将是相当大的。想想我想要实现的元素周期表中的数据(例如:),每个元素都有几条信息,我们有一个关于如何读取它们的视觉指南(第一行的第二个框)。谢谢@Sandy,事实上,我似乎在使用旧版本-我确实升级了,但不知何故忘记重新启动Rstudio:)我认为挑战将是尝试应用两个不同大小的映射。参见@joran的评论和解决方案(参考不同的颜色映射)。但是我不明白这种解决方案如何能轻易地应用到你的问题上。鉴于,我改变了绘图方式,使大写字母“N”而不是小写字母“a”。哇@Sandy,谢谢你的努力!这太棒了,非常感谢!有关更可靠地处理图例宽度的方法,请参见。