ggplot2

时间:2017-09-05 14:12:09

标签: r ggplot2

我想使用ggplot2创建一个3D瀑布图,x- / y轴显示为普通(2D)瀑布图,z轴显示为第三维"治疗天数" (条形图)。本文的一个例子(来源:doi:10.1093/annonc/mdw656)如下所示(图1(D)),其中3D图只是2D图(图1(B))和水平条图的组合(图1(C))。

enter image description here

以下是一些可重现的R代码,用于创建 2D瀑布图

library(ggplot2)

# Mock Data Simulation
subjid = seq(1, 9)
tumor.type = c("CCR", "CCR", "Gastric", "Ovarian", "Ovarian", "CCR", "Breast", "CCR", "Gastric")
per.change = c(42, 26, 20, 10, 5, -5, -10, -20, -50)
days.on.trt = c(20, 40, 60, 45, 70, 80, 100, 90, 120)

dat = data.frame(subjid, tumor.type, per.change, days.on.trt)
dat

# Waterfall Plot
b = ggplot(dat, aes(x=reorder(subjid, -per.change, median), y=per.change, fill=tumor.type)) +
  scale_fill_manual(values = c("red3", "gray60", "green4", "dodgerblue3"),
                    name="Tumor Type") + 
  labs(x = "Subject ID", y = "Best % Change from Baseline in Tumor Size") +
  theme_bw() %+replace%
  theme(axis.text.x = element_text(angle=90, hjust = 1), 
        axis.ticks.x=element_blank(),
        axis.title.x = element_text(face="bold", angle=0, size=8),
        axis.title.y = element_text(face="bold",angle=90, size=8),
        panel.grid.major.x = element_blank(),
        legend.justification=c(0.95,0.99), legend.position=c(0.95,0.99),
        legend.key = element_rect(fill = "white", 0.1)) +
  scale_y_continuous(limits=c(-80, 60), breaks=seq(-80, 60, by=20)) +
  coord_cartesian(ylim = c(-80, 60)) +
  geom_hline(yintercept = 0) +
  geom_hline(yintercept = -30, linetype="dashed", size=1, color="gray30") +
  geom_hline(yintercept = 20, linetype="dashed", size=1, color="gray30")

b + geom_bar(stat="identity", width=0.7, position = position_dodge(width=0.4))

以下是横条图的一些代码(针对相同主题):

# Swimmer Plot
ggplot(dat, aes(x=reorder(subjid, per.change), y=days.on.trt)) +
  geom_bar(stat='identity') + 
  labs(x = "Days on Treatment", y = "Subject ID") +
  coord_flip()

如果有人可以就如何创建3D图表(如上图1(D)所示)分享一些想法,我感激不尽。非常感谢!

1 个答案:

答案 0 :(得分:1)

关于ggplot2不支持3D图形的第二个Axeman。如果仔细观察文章的3D图,颜色与2D瀑布图不同;粗略地绘制肿瘤大小变化,在条的底部和x轴之间有可见的间隙;治疗的日子似乎没有正确分开。

这不能满足您的要求,但我也模拟了数据集并创建了2D瀑布图。如果颜色主题尊重剂量的普遍性,我更喜欢。此外,这会添加正确的x轴刻度标签。

###
# Libraries
###

library(tidyverse)
library(ggplot2)

###
# Simulate data
###

dose.categories <- paste(c(1,5,10,15,20,25), "mg")
disease.categories <- c("CCR", "Gastric", "Ovarian", "Breast")

data <- tibble(
  id=sample(20,20),
  tchange=sort(runif(n=20,min=-100,max=60), decreasing=T),
  dose=dose.categories[c(1,4,2,3,4,1,5,1,5,5,2,3,2,4,3,5,6,5,5,5)],
  disease=disease.categories[c(1,1,2,3,3,1,4,4,4,1,4,1,3,2,2,2,4,1,2,2)],
  duration=jitter(sort(runif(n=20,min=30,max=120)), amount=30)
) %>%
  arrange(id) %>%
  mutate(dose=factor(dose, levels=paste(c(1,5,10,15,20,25), "mg")))

###
# 2D waterfall
###

ggplot(data, aes(x=reorder(id, -tchange), y=tchange)) + 
  geom_bar(aes(fill=dose), stat="identity") +
  scale_x_discrete(labels=data$disease[as.numeric(levels(reorder(data$id, -data$tchange)))]) +
  scale_fill_brewer(palette="YlGn") +
  labs(x="",
       y="Tumor size change (%)",
       fill="Dose") +
  theme(axis.text.x = element_text(angle=90, vjust=0.5, hjust=1),
        axis.ticks.x=element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.position="bottom")