将自定义数字添加到ggplot图例 [英] add custom numbers to ggplot legend

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

问题描述

我绘制了一个 geom_tile 图,其中每个点都具有特定的 p值。 p>

这个图很好地用图例中的 p值限制绘制。



我想添加到这个图例中的是具有这个 p-value 的数据点的数量。



示例:

传说就像那样(每个值都有一个颜色):

  0.05 
0.04
0.03
0.02
0.01

我想要的是这样的:

  0.05 58 
0.04 34
0.03 85
0.02 54
0.01 10

如何将此添加到 ggplot2 图的传说?



我尝试使用geom_text并更改文字位置到图例,但它没有奏效。
$ b

更新

  pl < -  ggplot(df.new)+ geom_tile(ae s(fill = z,x,y))+ 
scale_fill_gradient(low =yellow,high =red,name =Legend1)+
stat_contour(breaks = c(0.001,0.01 ,0.05),aes(x = x,y = y,z = npvalue,color = .. level ..))+
scale_colour_gradient2(low =black,mid =black,high =green ,labels = paste(levels(c(0.001,0.01,0.05))))

我有这个图,我正在从 z 变量中正确绘制 geom_tile ,然后即时绘制一个 stat_contour 基于p值。我想编辑基于 p-value stat_contour 颜色的标签,就像我最初的问题一样,但我有这个错误:

  scale_labels.continuous(scale,breaks)中的错误:
Breaks and标签的长度不同

我的绘图问题在哪里?


fill 您可以指定图例的标签 c $ c> colors。



一个数据集示例:

  set .seed(1)
dat < - data.frame(x = rep(1:5),y = rep(1:10),
z = factor(sample(seq(0.01,0.05, 0.01),100,TRUE)))

函数 table 计算不同值 dat $ z

 表(dat $ z)

#0.01 0.02 0.03 0.04 0.05
#13 25 19 26 17

剧情:

  library(ggplot2)
ggplot(dat)+
geom_tile(aes(x = x,y = y,fill = z) )+
scale_fill_discrete(labels = paste(levels(dat $ z),table(dat $ z)))

在最后一行中,创建了图例的标签




I'm drawing a geom_tile plot where i have each point with a specific p-value.

The plot is drawn in a nice way with the p-value limits on the legend.

What i want to add to this legend is the number of data points which have this p-value.

Example:

the legend is something like that (with a color for each value):

0.05
0.04
0.03
0.02
0.01

what i want to have is something like this:

0.05 58
0.04 34
0.03 85
0.02 54
0.01 10

how can i add this to the legend of ggplot2 plots?

I tried using geom_text and change the text position to the legend, but it did not worked.

UPDATE

pl <- ggplot(df.new) + geom_tile(aes(fill=z, x, y)) + 
      scale_fill_gradient(low="yellow", high="red", name="Legend1") + 
      stat_contour(breaks=c(0.001, 0.01, 0.05), aes(x=x, y=y, z=npvalue, colour=..level..)) +
      scale_colour_gradient2(low="black", mid="black", high="green", labels=paste(levels(c(0.001, 0.01, 0.05))))

I have this plot, I'm drawing the geom_tile correctly from the z variable then im drawing a stat_contour based on the p-value. And i want to edit the labels for the stat_contour colors which are based on the p-value as in my initial question, but i'm having this error :

Error in scale_labels.continuous(scale, breaks) : 
  Breaks and labels are different lengths

Where is the problem in my plotting??

解决方案

You can specify the labels of the legend for the fill colours.

An example dataset:

set.seed(1)
dat <- data.frame(x = rep(1:5), y = rep(1:10), 
                  z = factor(sample(seq(0.01, 0.05, 0.01), 100, TRUE)))

The function table counts the different values of dat$z:

table(dat$z)

# 0.01 0.02 0.03 0.04 0.05 
#   13   25   19   26   17

The plot:

library(ggplot2)
ggplot(dat) +
  geom_tile(aes(x = x, y = y, fill = z)) + 
  scale_fill_discrete(labels = paste(levels(dat$z), table(dat$z)))

In the last line, the labels of the legend are created.

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

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