R语言 从5而不是0开始设置y轴

tcomlyy6  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(198)

我遇到一个问题,显示值为零的条形图,所以我想将y轴的起点设置为5,因为值为负。
enter image description here
这是我的代码。

p4 <- ggplot(path_response, aes(y= resp,x = reorder(Patient, Path_response), fill = response)) +
  geom_col() +
  scale_fill_manual(values = c("pink", "brown1","brown4")) +
  ggnewscale::new_scale_fill()+
  
  geom_col(aes(y =15, fill = Regimen), width = 0.9) +
  geom_col(aes(y = 10), fill = "white", color = "white") +
  geom_text(aes(y = 7, label = Patient), size = 4, fontface = "bold") +
    scale_fill_brewer(palette = "Set1")+
  theme_classic()+
  theme(axis.line.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.y = element_text(size=16, "bold"),
        legend.text = element_text(size= 14, "bold"),
        legend.background = element_blank(),                        
        legend.box.background  = element_rect(color = "black")     # add frame to the legend
        ) +
  labs(y="Pathological Response (%)", x="")

这是我的数据尝试。

path_response <- tibble::tribble(
  ~Patient, ~Regimen,     ~response, ~Path_response, ~resp,
  "129",    "Nivo/Rela",  "pMR",     -100L,          -100,
  "113",    "Nivo/Ipi",   "pMR",     -90L,           -90,
  "131",    "Nivo/Rela",  "pMR",     -90L,           -90,
  "108",    "Nivo/Ipi",   "pMR",     -80L,           -80,
  "139",    "Nivo/Rela",  "pMR",     -80L,           -80,
  "104",    "Nivo/Ipi",   "pMR",     -70L,           -70,
  "138",    "Nivo/Rela",  "pMR",     -50L,           -50,
  "112",    "Nivo/Rela",  "pmR",     -40L,           -40,
  "111",    "Nivo/Rela",  "pmR",     -30L,           -30,
  "119",    "Nivo/Ipi",   "pmR",     -30L,           -30,
  "141",    "Nivo alone", "pmR",     -20L,           -20,
  "102",    "Nivo alone", "pmR",     -10L,           -10,
  "103",    "Nivo/Rela",  "pmR",     -10L,           -10,
  "106",    "Nivo/Rela",  "pmR",     -10L,           -10,
  "109",    "Nivo/Rela",  "pmR",     -10L,           -10,
  "114",    "Nivo/Rela",  "pmR",     -10L,           -10,
  "121",    "Nivo alone", "pmR",     -10L,           -10,
  "133",    "Nivo alone", "pmR",     -10L,           -10,
  "134",    "Nivo/Ipi",   "pmR",     -10L,           -10,
  "140",    "Nivo/Ipi",   "pmR",     -10L,           -10,
  "143",    "Nivo/Rela",  "pmR",     -10L,           -10,
  "101",    "Nivo/Ipi",   "NR",      0L,             0,
  "107",    "Nivo alone", "NR",      0L,             0,
  "116",    "Nivo alone", "NR",      0L,             0,
  "117",    "Nivo/Ipi",   "NR",      0L,             0,
  "123",    "Nivo/Ipi",   "NR",      0L,             0,
  "124",    "Nivo/Ipi",   "NR",      0L,             0,
  "125",    "Nivo/Rela",  "NR",      0L,             0,
  "128",    "Nivo alone", "NR",      0L,             0,
  "130",    "Nivo alone", "NR",      0L,             0,
  "136",    "Nivo alone", "NR",      0L,             0,
  "137",    "Nivo alone", "NR",      0L,             0,
)

然而,当我尝试不同的方法来改变y轴范围时,它会翻转条形图或改变条形图的格式。

uubf1zoe

uubf1zoe1#

可能不是完美的答案,但是给resp添加一个最小值,至少得到显示0的列,然后改变scale_y_continuous的break和标签,以考虑这个最小的偏移,怎么样?

minimal_value = -5
ggplot(path_response, aes(y= resp+ minimal_value,x = reorder(Patient, Path_response), fill = response)) +
  geom_col() +
  scale_fill_manual(values = c("pink", "brown1","brown4")) +
  ggnewscale::new_scale_fill()+
  
  geom_col(aes(y =15, fill = Regimen), width = 0.9) +
  geom_col(aes(y = 10), fill = "white", color = "white") +
  geom_text(aes(y = 7, label = Patient), size = 4, fontface = "bold") +
  scale_fill_brewer(palette = "Set1")+
  theme_classic()+
  theme(axis.line.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.y = element_text(size=16, "bold"),
        legend.text = element_text(size= 14, "bold"),
        legend.background = element_blank(),                        
        legend.box.background  = element_rect(color = "black")     # add frame to the legend
  ) +
  labs(y="Pathological Response (%)", x="")+
  scale_y_continuous(breaks = seq(0,min(path_response$resp), by = -25)+minimal_value, labels = function(x) x- minimal_value)

其给予以下图表:

它看起来像你正在努力实现的目标吗?
注意:对scale_y_continuouslabels的修改来自之前的回答:how to plot geom_col() to get y axis to center around 1 and not 0

相关问题