2

I encountered a problem showing the bars with a value of zero, so I would like to set the starting point of the y-axis as 5, since the values are negative.

enter image description here

Here is my code.

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="")

Here is my data for trying.

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,
)

However, when I tried different ways to change the y-axis ranges, it would flip the bars or change the bar formats. I appreciate any help.

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • you can change the kimits of the yaxis with coord_cartesian(ylim=c(-100,5)) but this wont help you since the values for the bars are still 0 – user12256545 Apr 25 '23 at 13:34
  • @user12256545 Thanks for your comment. I tried your way to change the y-axis scale, but also expand the bar from 0 to 5. This is so hard for me. – tufeiyinjie Apr 25 '23 at 14:54

1 Answers1

1

Probably not the perfect answer but what about adding a minimal value to resp to get at least the column for 0 being displayed and then change breaks and labels of scale_y_continuous to take account of this minimal shift ?

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)

Which give the following graph: enter image description here Does it look like what you are trying to achieve ?

NB: Credit for the modification to labels of scale_y_continuous are from this previous answer: how to plot geom_col() to get y axis to center around 1 and not 0

dc37
  • 15,840
  • 4
  • 15
  • 32