简体   繁体   English

将图例颜色方块插入R ggplot轴标签

[英]insert legend color square into r ggplot axis label

I've found lots of great documentation for how to insert special characters and Greek letters into axis labels in ggplot in R, but nothing for pasting legend colors into the axis labels. 我已经找到了很多很棒的文档,关于如何在R的ggplot中将特殊字符和希腊字母插入到轴标签中,但是没有将图例颜色粘贴到轴标签中的内容。 I am creating a graph with a slightly complicated x-axis, and a collaborator suggested formatting the axis with a colored square (identical to the figure legend) in the axis label text so readers can reference which axis label refers to which data series. 我正在创建一个带有稍微复杂的x轴的图形,并且协作者建议在轴标签文本中使用彩色正方形(与图例相同)格式化轴,以便读者可以参考哪个轴标签引用了哪个数据系列。

Here's what the figure legend currently looks like: 这是当前图例的外观:

[ORANGE SQUARE] Series 1 [橙色广场]系列1

[BLUE SQUARE] Series 2 [BLUE SQUARE]系列2

And here's what I'd like the x-axis label to look like: 这就是我希望x轴标签看起来像的样子:

Extent in km [ORANGE SQUARE] or km/yr [BLUE SQUARE] 范围,以公里[ORANGE SQUARE]或km / yr [BLUE SQUARE]

Is it possible to do this kind of graphical manipulation in R, or do I need to create this kind of label in some other image processing software? 是否可以在R中进行这种图形操作,还是需要在其他图像处理软件中创建这种标签? Here's the plotting code I'm using, with some fake data: 这是我正在使用的绘图代码,其中包含一些虚假数据:

SERIES1 <- as.data.frame(sample(1:100, 100, replace=TRUE)) %>% mutate(source="SERIES1") 
SERIES2 <- as.data.frame(sample(1:1000, 100, replace=TRUE)) %>% mutate(source="SERIES2")
SERIES3 <- as.data.frame(sample(1:10000, 100, replace=TRUE)) %>% mutate(source="SERIES3")
colnames(SERIES1) <- c("value","source")
colnames(SERIES2) <- c("value","source")
colnames(SERIES3) <- c("value","source")
gg_df <- rbind(SERIES1, SERIES2, SERIES3)

fig1A <- ggplot(gg_df) + 
  geom_density(alpha=0.5, size=0.2, aes(x=value, y=..scaled.., fill=factor(source, labels=c('SERIES1','SERIES2','SERIES3')))) + 
  scale_x_continuous(limits=c(0,16000), breaks=c(seq(0, 16000, by=2000))) + 
  labs(x='Extent (km) or (km/dec)',y='Density') +
  theme_bw() +
  theme(legend.position=c(0.8, 0.85), legend.title=element_blank()) + 
  scale_fill_brewer(type='qual',palette='Dark2')

The cowplot package has nice functions for annotating on top of ggplots cowplot软件包具有出色的功能,可以在ggplots上进行注释

require(cowplot)

first, add spaces to your x label to make room for squares 首先,在您的x标签上添加空格以为正方形留出空间

fig1A <- ggplot(gg_df) + 
  geom_density(alpha=0.5, size=0.2, aes(x=value, y=..scaled..,
        fill=factor(source, labels=c('SERIES1','SERIES2','SERIES3')))) + 
  scale_x_continuous(limits=c(0,16000), breaks=c(seq(0, 16000, by=2000))) + 
  labs(x='Extent (km)     or (km/dec)     ',y='Density') + 
  theme_bw() +
  theme(legend.position=c(0.8, 0.85), legend.title=element_blank()) + 
  scale_fill_brewer(type='qual',palette='Dark2')

Make a dataframe containing x and y locations for your squares 制作一个包含正方形的x和y位置的数据框

squares <- data.frame(x = c(0.53, 0.75),  y = c(0.017,0.017))

use the ggdraw function in cowplot to draw your graph and then annotate squares on top of it. 使用ggdraw中的ggdraw函数绘制图形,然后在其顶部注释正方形。 Unlike annotate and geom_rect in ggplot, which allow annotation only inside the plot area, you can annotate anywhere on your figure using cowplot. geom_rect中的annotategeom_rect (仅允许在绘图区域内进行注释)不同,您可以使用Cowplot在图形上的任何位置进行注释。 The location of your squares is given using a 0 - 1 scale that goes from left to right and bottom to top on your plot - you will probably have to adjust the numbers depending on what size you are saving your graph as. 正方形的位置是使用0-1比例给出的,该比例从绘图的左到右,从下到上-您可能必须根据将图形另存为多大的大小来调整数字。

ggdraw(fig1A) + 
geom_rect(data = squares, aes(xmin = x, xmax = x + .02, 
                          ymin = y, ymax = y + .02),
        fill = c("orange", "blue"))

结果图

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

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