将调色板分配给ggplot2中的元素

时间:2017-01-07 06:10:11

标签: r plot ggplot2 colors

我正在使用代码生成Swimmer的情节,但遇到了定制难度。有3个分期类别,颜色太相似了。如何在aes中设置调色板,但元素长度是否一致?

swimmer's plot - teal on teal

试过这个而不是color = variable:colorRampPalette(c(“blue”,“red”))((length(dat $ Subject)))

set.seed(33)
dat = data.frame(Subject = 1:10, 
                 Months = sample(4:20, 10, replace=TRUE),
                 Treated=sample(0:1, 10, replace=TRUE),
                 Stage = sample(1:3, 10, replace=TRUE),
                 Continued=sample(0:1, 10, replace=TRUE))

dat = dat %>%
  group_by(Subject) %>%
  mutate(Complete=sample(c(4:(max(Months)-1),NA), 1, 
                         prob=c(rep(1, length(4:(max(Months)-1))),5), replace=TRUE),
         Partial=sample(c(4:(max(Months)-1),NA), 1, 
                        prob=c(rep(1, length(4:(max(Months)-1))),5), replace=TRUE),
         Durable=sample(c(-0.5,NA), 1, replace=TRUE))

# Order Subjects by Months
dat$Subject = factor(dat$Subject, levels=dat$Subject[order(dat$Months)])

# Melt part of data frame for adding points to bars
dat.m = melt(dat %>% select(Subject, Months, Complete, Partial),
             id.var=c("Subject","Months"))

ggplot(dat, aes(Subject, Months)) +
  geom_bar(stat="identity", aes(fill=factor(Stage)), width=0.7) +
  geom_point(data=dat.m, 
             aes(Subject, value, colour=variable, shape=variable), size=4) +
  geom_segment(data=dat %>% filter(Continued==1), 
               aes(x=Subject, xend=Subject, y=Months + 0.1, yend=Months + 1), 
               pch=15, size=0.8, arrow=arrow(type="closed", length=unit(0.1,"in"))) +
  coord_flip() +
  scale_fill_manual(values=hcl(seq(15,375,length.out=5)[1:4],100,70)) +
  scale_colour_manual(values=c(hcl(seq(15,375,length.out=3)[1:2],100,40),"black")) +
  scale_y_continuous(limits=c(0,20), breaks=0:20) +
  labs(fill="Disease Stage", colour="", shape="", 
       x="Subject Recevied Study Drug") +
  theme_bw() +
  theme(panel.grid.minor=element_blank(),
        panel.grid.major=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

1 个答案:

答案 0 :(得分:0)

我做了以下修改:

  • 删除pch=调用中的geom_segment参数,因为它不是geom_segment参数。
  • 更改了set_colour_manual调用,将两种颜色明确设置为darkbluedarkred。这些控制符号的颜色
  • na.rm=T调用中添加了geom_point参数,以抑制正在发出的缺失值警告..

结果代码如下所示:

library(ggplot2)
library(dplyr)
library(reshape2)
set.seed(33)
dat = data.frame(Subject = 1:10, 
                 Months = sample(4:20, 10, replace=TRUE),
                 Treated=sample(0:1, 10, replace=TRUE),
                 Stage = sample(1:3, 10, replace=TRUE),
                 Continued=sample(0:1, 10, replace=TRUE))

dat = dat %>%
  group_by(Subject) %>%
  mutate(Complete=sample(c(4:(max(Months)-1),NA), 1, 
                         prob=c(rep(1, length(4:(max(Months)-1))),5), replace=TRUE),
         Partial=sample(c(4:(max(Months)-1),NA), 1, 
                        prob=c(rep(1, length(4:(max(Months)-1))),5), replace=TRUE),
         Durable=sample(c(-0.5,NA), 1, replace=TRUE))

# Order Subjects by Months
dat$Subject = factor(dat$Subject, levels=dat$Subject[order(dat$Months)])

# Melt part of data frame for adding points to bars
dat.m = melt(dat %>% select(Subject, Months, Complete, Partial),
             id.var=c("Subject","Months"))

ggplot(dat, aes(Subject, Months)) +
  geom_bar(stat="identity", aes(fill=factor(Stage)), width=0.7) +
  geom_point(data=dat.m, na.rm=T,
             aes(Subject, value, colour=variable, shape=variable), size=4) +
  geom_segment(data=dat %>% filter(Continued==1), 
               aes(x=Subject, xend=Subject, y=Months + 0.1, yend=Months + 1), 
               size=0.8, arrow=arrow(type="closed", length=unit(0.1,"in"))) +
  coord_flip() +
  scale_fill_manual(values=hcl(seq(15,375,length.out=5)[1:4],100,70)) +
  scale_colour_manual(values=c("darkblue","darkred")) +
  scale_y_continuous(limits=c(0,20), breaks=0:20) +
  labs(fill="Disease Stage", colour="", shape="", 
       x="Subject Recevied Study Drug") +
  theme_bw() +
  theme(panel.grid.minor=element_blank(),
        panel.grid.major=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

这会在ggplot 2.2.1上创建以下图表而没有任何警告。

enter image description here

当然我没有看到你提到的所有警告,所以也许查看版本。这是我的sessionInfo()

enter image description here