两种色标适用于不同的几何图形 [英] Two color scales for different geoms

查看:42
本文介绍了两种色标适用于不同的几何图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在不同方面创建差异geom_rect时,我得到一些奇怪的行为.在下面的示例中,geom_point的颜色应与geom_rect的填充相同,并且应与geom_rect的轮廓相同.但是,这些因素似乎在某个地方以一种奇怪的方式串联在一起.

I'm getting some strange behaviour when trying to create difference geom_rect in different facets. In the below example, the colour of the geom_point should be the same as the fill of the geom_rect, which should be the same as the outline of the geom_rect. However, the factors seem to be becoming concatenated in a weird way somewhere.

任何帮助将不胜感激.

我不希望这些点按序列着色,因为聚类和序列并不总是相同的(用于可视化排列统计信息).

I don't want the points to be coloured by sequence, as the cluster and sequence will not always be identical (it is for visualising a permutation statistic).

这些显然不是我想要绘制的实际数据!

THESE ARE OBVIOUSLY NOT THE ACTUAL DATA THAT I WANT TO PLOT!

library(data.table)
library(ggplot2)

scatter.dt = data.table(sequence = factor(paste('Sequence',c(1,2,3,4))),
                        cluster = factor(paste('Cluster',c(1,2,3,4))),
                        outcome = c(1,1,1,1),
                        transition.time = 1:4,
                        intervention.time = 1:4)
vline.dt = data.table(sequence = scatter.dt$sequence,
                      cluster = scatter.dt$cluster,
                      transition.time = 1:4,
                      intervention.time = 2:5)

plot1 = ggplot2::ggplot() +
  ggplot2::geom_rect(data = vline.dt,
                     aes(fill = sequence,
                         colour = sequence,
                         xmin = transition.time, 
                         xmax = intervention.time, 
                         ymin = -Inf, 
                         ymax = Inf),
                     alpha = .6,
                     size = 2) +
  ggplot2::geom_point(data=scatter.dt,
                      aes(x=transition.time ,
                          y=intervention.time,
                          colour = cluster),
                      alpha=1,
                      size = 4) +
  ggplot2::facet_grid(sequence + cluster ~ .)

plot(plot1)

这样生成数据时,其聚类颜色也应与顺序颜色顺序相同:

Should also have cluster colours in the same order as sequence colours when data are generated thus:

scatter.dt = data.table(sequence = factor(paste('Sequence',c(1,2,3,4))),
                        cluster = sample(factor(paste('Cluster',c(1,2,3,4)), levels = paste('Cluster',c(1,2,3,4)))),
                        outcome = c(1,1,1,1),
                        transition.time = 1:4,
                        intervention.time = 1:4)
vline.dt = data.table(sequence = scatter.dt$sequence,
                      cluster = scatter.dt$cluster,
                      transition.time = 1:4,
                      intervention.time = 2:5)

推荐答案

使用 ggnewscale :

library(data.table)
library(ggplot2)
library(ggnewscale)

scatter.dt = data.table(sequence = factor(paste('Sequence',c(1,2,3,4))),
                        cluster = factor(paste('Cluster',c(1,2,3,4))),
                        outcome = c(1,1,1,1),
                        transition.time = 1:4,
                        intervention.time = 1:4)
vline.dt = data.table(sequence = scatter.dt$sequence,
                      cluster = scatter.dt$cluster,
                      transition.time = 1:4,
                      intervention.time = 2:5)

plot1 = ggplot2::ggplot() +
  ggplot2::geom_rect(data = vline.dt,
                     aes(fill = sequence,
                         colour = sequence,
                         xmin = transition.time, 
                         xmax = intervention.time, 
                         ymin = -Inf, 
                         ymax = Inf),
                     alpha = .6,
                     size = 2) +
  ggnewscale::new_scale_color() +
  ggplot2::geom_point(data=scatter.dt,
                      aes(x=transition.time ,
                          y=intervention.time,
                          colour = cluster),
                      alpha=1,
                      size = 4) +
  ggplot2::facet_grid(sequence + cluster ~ .)

plot(plot1)

已生成数据:

scatter.dt = data.table(sequence = factor(paste('Sequence',c(1,2,3,4))),
                        cluster = sample(factor(paste('Cluster',c(1,2,3,4)), levels = paste('Cluster',c(1,2,3,4)))),
                        outcome = c(1,1,1,1),
                        transition.time = 1:4,
                        intervention.time = 1:4)
vline.dt = data.table(sequence = scatter.dt$sequence,
                      cluster = scatter.dt$cluster,
                      transition.time = 1:4,
                      intervention.time = 2:5)

这篇关于两种色标适用于不同的几何图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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