简体   繁体   English

在ggplot2中添加手动渐变图例

[英]Add manual gradient legend in ggplot2

I am making a "bullseye" or "dart board" plot with ggplot2 . 我正在用ggplot2绘制“靶心”或“飞镖板”图。 I'd like to ask if there is a way to manually add a gradient legend to the figure I am making, or if there is a way to modify my procedure to add that legend. 我想问一下是否有一种方法可以手动在我制作的图形上添加渐变图例,或者是否可以修改我的过程以添加该图例。

Here is a reproducible dataset to work with: 以下是可重复使用的数据集:

step <- 0.1
frac <- seq(0, 1, step)
df <- data.frame(l=levels(cut(0, breaks=frac)),  o=frac[1:length(frac)-1], f=c(0.000, 0.028, 0.165, 0.151, 0.149, 0.129, 0.138, 0.060, 0.090, 0.088), cols=c('#0000FF','#4343FF','#FF4343','#FF6969','#FF7070','#FFA4A4','#FF8E8E','#9D9DFF','#F0F0FF','#E8E8FF'))

Here is what it looks like: 看起来像这样:

> df
df
           l   o     f    cols
1    (0,0.1] 0.0 0.000 #0000FF
2  (0.1,0.2] 0.1 0.028 #4343FF
3  (0.2,0.3] 0.2 0.165 #FF4343
4  (0.3,0.4] 0.3 0.151 #FF6969
5  (0.4,0.5] 0.4 0.149 #FF7070
6  (0.5,0.6] 0.5 0.129 #FFA4A4
7  (0.6,0.7] 0.6 0.138 #FF8E8E
8  (0.7,0.8] 0.7 0.060 #9D9DFF
9  (0.8,0.9] 0.8 0.090 #F0F0FF
10   (0.9,1] 0.9 0.088 #E8E8FF

Here is reproducible code to generate the plot: 这是生成图的可复制代码:

library(ggplot2)
#pdf("test.pdf", width=8, height=8)
p <- ggplot() + ylim(0, 1) + geom_rect(aes(xmin=-1, ymin=df$o, xmax=1, ymax=df$o+step), fill=df$cols) + coord_polar() + theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank())
print(p)
#dev.off()

And here is the plot that this code generates — basically, a set of concentric rings with a desired color fill: 这是此代码生成的图-基本上是一组具有所需颜色填充的同心环:

靶心

I'd like to add a legend bar to this plot that displays a (linear) gradient from a start color to an end color, with labels using the min/max of df$f . 我想在此绘图中添加图例栏,以显示从开始颜色到结束颜色的(线性)渐变,并使用df$f的最小值/最大值进行标注。

I can provide the start and end color manually from df — or from the first and last entries of a separate colorRampPalette object (not shown) — or provide the color array from the colorRampPalette object to provide the interpolated colors. 我可以从手动提供的开始和结束颜色df -或从单独的第一和最后一个条目colorRampPalette对象(未示出) -或从提供颜色阵列colorRampPalette目的是提供经内插的色彩。 I just don't know how to generate a legend object separate from the main plot. 我只是不知道如何生成与主图分开的图例对象。

Is there a way to manually construct and append such an object onto this plot? 有没有一种方法可以手动构造此对象并将其附加到该图上?

A couple things I tried were to explore the guide_colourbar() object and the scale_colour_gradientn() object, but I was not able to get anything into my plot using these entities. 我尝试了几件事情,目的是探索guide_colourbar()对象scale_colour_gradientn()对象,但是我无法使用这些实体将任何东西放入绘图中。 Is there a way to use them here? 有没有办法在这里使用它们?

The easiest way to get a nice legend, you'll need to actually map things to your data, like the design intent behind ggplot . 获得精美图例的最简单方法是,您实际上需要将事物映射到数据,例如ggplot背后的设计意图。

Method 1, map fill to your color variable and use scale_identity to use the exact colors as provided: 方法1,将fill映射到您的颜色变量,然后使用scale_identity使用提供的确切颜色:

ggplot(df) + 
  ylim(0, 1) + 
  geom_rect(aes(xmin = -1, ymin = o, xmax = 1, ymax = o + step, fill = cols)) + 
  coord_polar() + 
  scale_fill_identity(guide = guide_legend())

在此处输入图片说明

The legend leaves to be desired however, since it isn't continuous, it is ordered the wrong way around and it shows color codes instead of values for the labels. 但是,图例仍然不理想,因为它不是连续的,所以顺序错误,并且显示颜色代码而不是标签的值。 Additionally, the different colors aren't equidistant in their mapped values. 此外,不同的颜色在其映射值中不是等距的。

Method 2, you can use an actual continuous scale (which you haven't provided) in order to get an appropriate colorbar. 方法2,您可以使用实际的连续刻度(未提供)以获取合适的颜色条。 Here is an attempt that you can work from, where I let ggplot create a continuous scale from your extreme values through white. 这是您可以尝试的一种尝试,我让ggplot从您的极值到白色创建一个连续的比例。

ggplot(df) + 
  ylim(0, 1) + 
  geom_rect(aes(xmin = -1, ymin = o, xmax = 1, ymax = o + step, fill = f)) + 
  coord_polar() + 
  scale_fill_gradient2(midpoint = mean(range(df$f)), low = '#0000FF', high = '#FF4343')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM