添加文字到ggplot [英] Add text to ggplot

查看:1972
本文介绍了添加文字到ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(更新)
我有这样的ggplot,但接下来x轴日期缩放:

  g1< ;  -  ggplot(diamonds,aes(clarity,fill = cut))+ geom_bar()

酒吧(可以说VS2和IF,但在我的图中它是一个日期)我想把一个文本标签上方的酒吧在海格13.000。



我尝试了很多事情,但这是最接近的:
这是我在日期轴
g1 + geom_text(aes(as.Date(2014-10-05),13000) ,label =boat)



但这只会在图表中添加一个,只要我试图扩展它,例如用 $ b $ (c)(as.Date(2014-10-05),as.Date(2014-10-20)b

  g1 + geom_text ),13000),label = c(boat,train))

错误:美学必须是长度为1,或者长度与
dataProblems相同:c(as。)错误:日期(2014-10-05),as.Date(2014-10-20))


我也尝试从数据框(oefen)读取文本和标签,其中我使用了与原始图相同的名称

  g1 + geom_text(data = oefen,aes(x = newdat,y = Number,label = oefen $ labs,fill = 1))

我得到错误


错误:提供给离散量表的连续值

我尝试了很多其他解决方案,但无法找到答案。我错过了什么?

解决方案

考虑使用 annotate()无论您想要在剧情的某个特定位置放置什么文字。因子变量(如x轴上的透明度因子)对每个级别都有一个数字,因此您可以使用该数字来查找文本。我认为日期变量具有相同的用法:

  ggplot(diamonds,aes(clarity,fill = cut))+ geom_bar( )+ 
注释(text,x = 8,y = 13000,label =boat)+
注释(text,x = 4,y = 13000,label =ship )


评论后编辑



为了提高效率,您可以结合注释,例如:

  ggplot(diamonds,aes(clarity,fill = cut)) + geom_bar()+ 
annotate(text,x = c(2,4,6,8),y = 13000,label = c(two,ship,six,boat ))


(updated) I have ggplot like this, but then the x axis Date scaled:

g1 <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()

Above two bars (lets sayVS2 and IF, but in my graph it is a date) I want to put a text label above the bars at heigt 13.000.

I tried a lot of things, but this is what came closest: This is what I tried in my graph with the Date axis g1 + geom_text(aes(as.Date("2014-10-05"), 13000), label="boat")

but this adds only one to the graph and as soon as I try to extend it, for example with

g1 + geom_text(aes(c(as.Date("2014-10-05"),as.Date("2014-10-20")) , 13000), label=c("boat", "train"))

then I get the error:

Error: Aesthetics must either be length one, or the same length as the dataProblems:c(as.Date("2014-10-05"), as.Date("2014-10-20"))

I also tried to read the text and labels from a dataframe (oefen), where I used the same names as the original plot

g1 + geom_text(data=oefen, aes(x=newdat, y=Number, label=oefen$labs, fill=1))

I get the error

Error: Continuous value supplied to discrete scale

I tried many other solutions, but cannot find the answer. What am I missing?

解决方案

Consider using annotate() to place whatever text where you want at a given location on the plot. Factor variables, as in the clarity factor on the x-axis, have a number for each level, so you can use that number to locate the text. I assume date variables have the same usage.:

ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar() +
  annotate("text", x=8, y=13000, label= "boat") + 
  annotate("text", x = 4, y=13000, label = "ship")

EDIT after COMMENT

For efficiency, you can combine the annotations, such as this:

ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar() +
  annotate("text", x = c(2,4,6,8), y=13000, label = c("two", "ship", "six", "boat"))

这篇关于添加文字到ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆