如何使用ggplot / R将geom_bar添加到此绘图中?

时间:2019-05-14 07:38:37

标签: r ggplot2

请在下面找到My data

我在w$WHO==1,2,3,4中存储了四个患者类别,分别对应于WHO-I,WHO-II,WHO-III和未知肿瘤。

w$response是肿瘤进展还是保持稳定。我已将事件时间计算为人年,如下所示:

library(survival)
a <- pyears(Surv(rfs,response)~WHO, data=w, scale=365.25,data.frame = TRUE)

哪个给了a$data

  WHO    pyears  n event
1   1 74.700000 32     4
2   2 22.816667 23    15
3   3  3.333333 13    12
4   4 13.650000  4     0

如您所见,w$WHO==4中没有事件。

我已经构建了一个包含x-axis = WHO grades和两个y-axis = number og progressions and progression rate的地块。

我的情节看起来像这样

enter image description here

我希望与x-axis相等的w$WHO==4保留下来,但是上面有一个“空白”来说明0个事件。

我有类似的图显示了x-axis的外观:

enter image description here

geom_point y的计算如下:a$data$event[a$data$WHO==1]/a$data$pyears[a$data$WHO==1]*100*0.05用于校准y轴。

我的脚本在下面列出。

j <-  ggplot() +
  geom_bar(mapping = aes(x = w$WHO[w$response==1]), stat = "count", alpha=0.2, colour="#1C73C2", fill="#51BFC4")  

yaks <- c(0,2,4,6,8,10,12,14,16,18)


j <- 
  j %+% scale_y_continuous(name = "Progression", breaks=yaks, sec.axis = sec_axis(~ . * 1 , name = "Progression rate per 100 person-years", breaks=yaks, labels=c("0","40","80","120","160","200","240","280","320","360"))) +
  coord_cartesian(ylim=c(0, 18)) +
  theme(axis.text.y.right = element_text(color = "red", size = 12), axis.title.y=element_text(size=14,face="bold",margin = margin(t = 0, r = 20, b = 0, l = 0))) + 
  theme(axis.text.y = element_text(color = "#1C73C2", size = 12), axis.title.y.right=element_text(size=14,face="bold",margin = margin(t = 0, r = 0, b = 0, l = 20)))

j <- 
  j + 
  geom_point(mapping = aes(x = 1, y = 0.2677376), size=3, shape=16, colour="red") +
  geom_point(mapping = aes(x = 2, y = 3.2870709), size=3, shape=16, colour="red") +
  geom_point(mapping = aes(x = 3, y = 18), size=3, shape=16, colour="red") +

  geom_segment(mapping = aes(x = 1, y = 0.2677376, xend = 2, yend = 3.2870709), colour="red", alpha=1) +
  geom_segment(mapping = aes(x = 2, y = 3.2870709, xend = 3, yend = 18), colour="red", alpha=1) +


  annotate("text", x = c(0.9,1.9,2.9) , y = c(0.2677376+0.08, 3.2870709+0.15, 18),  label = c("5.4","65.7","360.0"), col="red", fontface=2, cex=4)

j + scale_x_continuous(name="", breaks = c(1,2,3), labels =c("WHO-I\nn=32","WHO-II\nn=23","WHO-III\nn=13")) +
  theme(axis.text.x = element_text(color = "grey20", size = 14, face="bold"))

我的数据

w <- structure(list(WHO = c(1L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 2L, 
3L, 3L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 4L, 4L, 1L, 4L, 1L, 2L, 1L, 4L, 1L, 4L, 4L, 4L, 
4L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 2L, 1L, 2L, 2L, 4L, 4L, 
4L, 2L, 4L, 1L, 4L, 4L, 2L, 4L, 4L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L
), response = c(0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 
1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 
1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), rfs = c(547.875, 
60.875, 395.6875, 517.4375, 669.625, 60.875, 182.625, 152.1875, 
365.25, 243.5, 91.3125, 60.875, 30.4375, 639.1875, 60.875, 1308.8125, 
121.75, 60.875, 121.75, 152.1875, 3.04375, 4169.9375, 121.75, 
2648.0625, 517.4375, 730.5, 2191.5, 578.3125, 426.125, 2526.3125, 
2069.75, 1704.5, 1734.9375, 547.875, 426.125, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 474.825, 
270.89375, 456.5625, 499.175, 234.36875, 2298.03125, 91.3125, 
1667.975, 675.7125, 365.25, 435.25625, 182.625, 365.25, 663.5375, 
182.625, 91.3125, 365.25, 91.3125, 182.625, 91.3125, 91.3125, 
365.25, 295.24375, 91.3125, 91.3125, 365.25, 91.3125, 182.625, 
91.3125, 182.625, 121.75, 1521.875, 639.1875, 913.125, 152.1875, 
334.8125, 365.25)), .Names = c("WHO", "response", "rfs"), class = "data.frame", row.names = c(NA, 
-106L))

1 个答案:

答案 0 :(得分:1)

这是您想要的吗?


w <- na.omit(w)

w$WHO <- as.factor(w$WHO)

yaks <- c(0,2,4,6,8,10,12,14,16,18)

j <-  ggplot() +
  geom_bar(mapping = aes(x = w$WHO[w$response==1]), stat = "count", alpha=0.2, colour="#1C73C2", fill="#51BFC4")  +
  scale_x_discrete(name="", drop = FALSE, labels =c("WHO-I\nn=32","WHO-II\nn=23","WHO-III\nn=13", "Unknown\nn=4")) +
  theme(axis.text.x = element_text(color = "grey20", size = 14, face="bold"))


j <- 
  j %+% scale_y_continuous(name = "Progression", breaks=yaks, sec.axis = sec_axis(~ . * 1 , name = "Progression rate per 100 person-years", breaks=yaks, labels=c("0","40","80","120","160","200","240","280","320","360"))) +
  coord_cartesian(ylim=c(0, 18)) +
  theme(axis.text.y.right = element_text(color = "red", size = 12), axis.title.y=element_text(size=14,face="bold",margin = margin(t = 0, r = 20, b = 0, l = 0))) + 
  theme(axis.text.y = element_text(color = "#1C73C2", size = 12), axis.title.y.right=element_text(size=14,face="bold",margin = margin(t = 0, r = 0, b = 0, l = 20)))

j <- 
  j + 
  geom_point(mapping = aes(x = 1, y = 0.2677376), size=3, shape=16, colour="red") +
  geom_point(mapping = aes(x = 2, y = 3.2870709), size=3, shape=16, colour="red") +
  geom_point(mapping = aes(x = 3, y = 18), size=3, shape=16, colour="red") +
  geom_point(mapping = aes(x = 4, y = 0), size=3, shape=16, colour="red") +

  geom_segment(mapping = aes(x = 1, y = 0.2677376, xend = 2, yend = 3.2870709), colour="red", alpha=1) +
  geom_segment(mapping = aes(x = 2, y = 3.2870709, xend = 3, yend = 18), colour="red", alpha=1) +
  geom_segment(mapping = aes(x = 3, y = 18, xend = 4, yend = 0), colour="red", alpha=1) +

  annotate("text", x = c(0.9,1.9,2.9, 3.9) , y = c(0.2677376+0.08, 3.2870709+0.15, 18, 0),  label = c("5.4","65.7","360.0", "0"), col="red", fontface=2, cex=4)

enter image description here