同一刻度/标签有几种颜色 [英] Several colors for the same tick/label

查看:75
本文介绍了同一刻度/标签有几种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据:

dat <- data.frame(x = c(1,2,3,4,5,6), y = c(2,3,4,6,2,3))

我的情节的痕迹和标签:

Breaks and labels of my plot :

breaks <- c(3,5)
labels <- c(paste(3,"(0.3)"), paste(5,"(0.5)"))

还有我的情节:

library(ggplot2)
ggplot() + 
  geom_point(data = dat, aes(x = x, y = y)) + 
  scale_y_continuous(breaks = breaks, labels = labels)

我希望给相同的标签涂上不同的颜色.例如,我希望用不同于(0.3)"的颜色给"3"上色.

I wish to colour the same labels differently. For instance, I wish to colour the "3" with a different colour than the one of "(0.3)".

推荐答案

这是将2个地块与patchwork粘贴在一起的一种方法,该软件包与cowplot类似,但具有更大的灵活性.我将标签分为2个向量,一个带有整数,一个带有括号的小数.然后绘制2个图,一个用于没有其他标记的外部标签,另一个用于主图.

Here's a way to stick 2 plots together with patchwork, which is a package similar to cowplot but with a little more flexibility. I split the labels into 2 vectors, one with the integers and one with the decimals in parentheses. Then make 2 plots, one for the outer labels with no other markings, and one for the main plot.

经过一轮尝试构建后,我开始调整每个主题的边距,意识到我需要将顶部和底部边距设置为相同,但在左侧图的右侧和左侧均未设置边距右侧图的一侧,因此它们之间的空间很小.肯定还有调整的方法,但是我将从一些间距开始.

After doing one round of trying to build this, I started adjusting the margins in each theme, realizing I needed to set the top and bottom margins the same, but making no margin on the right side of the left plot and the left side of the right plot, so there's very little space between them. There's definitely still ways to tweak this, but I'd start with some of the spacing.

library(tidyverse)
library(patchwork)

lbl_int <- str_extract(labels, "^\\d+")
lbl_frac <- str_extract(labels, "\\(.+\\)")

主图非常简单,只需删除主题左侧的元素即可.

The main plot is fairly straightforward, just removing elements from the left side in the theme.

(main_plot <- ggplot(dat, aes(x = x, y = y)) + 
  geom_point() + 
  scale_y_continuous(breaks = breaks, labels = lbl_frac) +
  theme(axis.text.y.left = element_text(color = "gray"), 
        axis.title.y.left = element_blank(),
        plot.margin = margin(1, 1, 1, 0, "mm")))

外部标签的图删除了大多数主题元素,但具有y轴标题和标签.

The plot for the outer labels has most theme elements removed, but has the y-axis title and labels.

(int_plot <- ggplot(dat, aes(x = 0, y = y)) + 
  scale_y_continuous(breaks = breaks, labels = lbl_int) +
  theme(axis.text.y.left = element_text(color = "black"), 
        axis.title.y.left = element_text(color = "black"),
        axis.title.x = element_blank(),
        panel.grid = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_blank(),
        panel.background = element_blank(),
        plot.margin = margin(1, 0, 1, 1, "mm")))

然后patchwork可以轻松地将所有绘图(按+按字面意义)添加在一起,然后设置宽度.再说一遍,您可以根据需要进行调整,但是与右图相比,我使左图非常瘦.

Then patchwork makes it easy to just add plots together—literally with +—and then set the widths. Again, here's something you can adjust as you need, but I made the left plot very very skinny compared to the right one.

int_plot + main_plot +
  plot_layout(ncol = 2, widths = c(1e-3, 1))

reprex程序包(v0.2.1)于2018年12月21日创建

Created on 2018-12-21 by the reprex package (v0.2.1)

这篇关于同一刻度/标签有几种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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