品质标签分几种颜色_同一刻度/标签有几种颜色

My data :

dat

Breaks and labels of my plot :

breaks

labels

And my plot :

library(ggplot2)

ggplot() +

geom_point(data = dat, aes(x = x, y = y)) +

scale_y_continuous(breaks = breaks, labels = labels)

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)".

解决方案

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

lbl_frac

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

(main_plot

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")))

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

(int_plot

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")))

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))

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值