首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用geom_col在x轴上添加0计数?

如何使用geom_col在x轴上添加0计数?
EN

Stack Overflow用户
提问于 2021-10-22 11:07:38
回答 2查看 136关注 0票数 0

让我说我有

如您所见,0在WHO-1中有完整的响应,但也有其他类别的情况。因此,从图形上看,在geom_col-category中有三个-bars,在另外两个中有四个geom_col-bars。

如何向WHO-1中添加第四个栏,指示对应于完整响应的空间

有点像

脚本

代码语言:javascript
复制
pp %>% 
  as_tibble() %>% 
  mutate(nyWHO = as.factor(WHO),
         best.resp = as.factor(case_when(best_rad == "CR" ~ 4,
                                         best_rad == "PR" ~ 3,
                                         best_rad == "SD" ~ 2,
                                         best_rad == "PD" ~ 1))) %>%
  count(nyWHO, best.resp) %>% 
  ggplot(aes(nyWHO, n, color = best.resp, fill= best.resp))  +
  scale_fill_manual(values = alpha(c("#6DBCC3", "#8B3A62", "grey40", "#E1B930"), 0.4), 
                    name="",
                    labels = c("Progressive disease", "Stable disease", "Partial response", "Complete response")) +
  scale_colour_manual(values = cols, 
                      name="",
                      labels = c("Progressive disease", "Stable disease", "Partial response", "Complete response"))  + 
  geom_col(width=1, position = position_dodge2(width = 1, preserve = "single")) +
  geom_text(aes(label = n), position = position_dodge2(width = 1, preserve = "single"), 
            vjust=-0.5, size = 10, show.legend = F) +
  scale_x_discrete(name = "", labels = c("WHO-1", "WHO-2", "WHO-3")) +
  scale_y_continuous(name="", breaks = seq(0, 80, by = 20)) + 
  coord_cartesian(ylim=c(0, 80)) + 
  theme(axis.title.x = element_text(size = 22),
                axis.title.y = element_text(size = 22),
                axis.text.x = ggtext::element_markdown(color = "grey20", size = 20),
                axis.text.y = element_text(color = "grey20", size = 18),
                panel.grid.major = element_line(colour = "gray90", size = 1.2),
                panel.grid.minor = element_line(colour = "gray90", size = 0.6),
                legend.text = element_text(size = 22),
                legend.position = "top")

数据

pp <-结构(WHO=结构(c( 3L、3L、3L、3L、3L、2L、2L、2L、2L、2L、2L、2L、2L、2L、1L、1L、3L、3L、2L、3L、1L、1L、2L、2L、1L、1L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、1L、2L、3L、2L、3L、2L、2L、1L、2L、3L、2L、2L、2L、1L、2L、3L、2L、2L、3L、3L、3L、2L、2L、1L、3L、3L、2L、2L、1L、3L、3L、2L、2L、1L、3L、3L、2L、2L、1L、1L、2L、2L、2L、2L、2L、1L、2L、3L、2L、2L、3L、2L、3L、2L、2L、1L、1L、2L、3L、2L、2L、1L、3L、3L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、2L、1L,1L,3L,2L,2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,1L,1L,2L,2L,1L,1L,3L,1L,1L,3L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,1L,1L,1L,3L,3L,3L,3L,2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,1L,1L,3L,3L,3L,2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,1L,1L,1L,2L,2L,1L,1L,3L,1L,1L,3L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,1L,1L,3L,2L,2L,1L,1L,3L,1L,1L,1L,1L,1L,1L,1L,1L3L,2L,2L,2L,3L,2L,3L,3L,.Label = c("1","2","3"),类=“因子”,best_rad = c("SD","SD","CR","CR","SD","PR","CR","PR","PR","PR“、"SD”、"PD“、"PR”、"PD“、"SD”、"SD“、"PD”、"SD“、"PD”、"PR“、"PD”、"SD“、"PD”、"SD“、"SD”、"PD“、"PD”、"PD““PD”、"SD“、”PD“、"SD”、“PD”、“PD”、"SD“、"SD”、“PD”、"PD",“SD”、"PD“、”SD“、"PD”、“SD”、“SD”、"PD“、”SD“、"PD”、“SD”、"SD","SD","PD","SD","CR","SD","PD","SD","SD","SD“、"PD”、"PD“、"SD”、"SD“、"PD”、"SD“、"PD”)、row.names = c(NA、-133 L)、class = "data.frame")

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-22 11:30:08

使用count(nyWHO, best.resp, .drop = FALSE)

代码语言:javascript
复制
d <- pp %>% 
  as_tibble() %>% 
  mutate(nyWHO = as.factor(WHO),
         best.resp = as.factor(case_when(best_rad == "CR" ~ 4,
                                         best_rad == "PR" ~ 3,
                                         best_rad == "SD" ~ 2,
                                         best_rad == "PD" ~ 1))) %>%
  count(nyWHO, best.resp, .drop = FALSE)
d
# A tibble: 12 x 3
   nyWHO best.resp     n
   <fct> <fct>     <int>
 1 1     1            11
 2 1     2            41
 3 1     3             3
 4 1     4             0
 5 2     1            22
 6 2     2            13
 7 2     3             5
 8 2     4             1
 9 3     1            23
10 3     2             9
11 3     3             1
12 3     4             4

ggplot(...)

票数 1
EN

Stack Overflow用户

发布于 2021-10-22 11:46:17

使用.drop = FALSE是解决方案,但是如果不可能的话,原始数据是不可用的,那么解决方案是通过使用联接来扩展所有级别的数据:

代码语言:javascript
复制
...
data <- pp %>% 
  as_tibble() %>% 
  mutate(nyWHO = as.factor(WHO),
         best.resp = as.factor(case_when(best_rad == "CR" ~ 4,
                                         best_rad == "PR" ~ 3,
                                         best_rad == "SD" ~ 2,
                                         best_rad == "PD" ~ 1))) %>%
  count(nyWHO, best.resp) 

data %>% 
  full_join(expand(data, nyWHO, best.resp), by = c("nyWHO", "best.resp")) %>% 
  replace_na(list(n = 0)) %>% 

  ggplot(aes(nyWHO, n, color = best.resp, fill= best.resp))  +
... # rest of ggplot statement

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69675802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档