Y-axis starting at 1

Hi,

I've got a code such as:

data_long <- structure(list(percentil = c(5, 5, 5, 15, 15, 15, 25, 25, 25, 
                                          35, 35, 35, 45, 45, 45, 50, 50, 50, 55, 55, 55, 65, 65, 65, 75, 
                                          75, 75, 85, 85, 85, 95, 95, 95), 
                            type = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
                                                2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), 
                            levels = c("pivo", "víno", "lihoviny"), 
                            class = "factor"), 
                            value = c(0.824, 0.556, 0.774, 0.838, 0.631, 0.839, 0.846, 0.666, 0.866, 0.859, 0.697, 
                                      0.889, 0.873, 0.721, 0.908, 0.88, 0.732, 0.917, 0.886, 0.741, 
                                      0.925, 0.903, 0.767, 0.947, 0.929, 0.814, 0.98, 0.977, 0.901, 
                                      1.043, 1.164, 1.137, 1.281)), 
                       row.names = c(NA, -33L), 
                       class = c("tbl_df", "tbl", "data.frame")) 

library(ggplot2)

ggplot(data_long, aes(x = percentil, y = value, fill = type)) +
  geom_bar(position="dodge", stat="identity") +
  scale_fill_manual(values = c("#00254B", "#ECB925", "#A6A6A6")) +
  theme_minimal() + 
  theme(legend.position = 'bottom',
        legend.margin=margin(0,0,0,0),
        legend.title = element_blank(),
        text = element_text(size = 15),
        axis.text.x = element_text(size = 13),
        axis.text.y = element_text(size = 13,
                                   angle = 0,
                                   hjust = 0.7),
        axis.title.x = element_text(size = 11.5),
        plot.margin = margin(20, 20, 20, 20),
        axis.title.y = element_blank(),
        panel.spacing = unit(2, "lines"),
        panel.grid.major.y = element_blank()) +
  xlab("") +
  scale_y_continuous(name = "",
                     label = scales::percent_format(accuracy = 1, scale = 100, prefix = "", suffix = " %",
                                                    big.mark = " ", decimal.mark = ","))

Yet I want my y-axis to start at 1 (100 %) (see picture below). I've tried Google but I guess I don't know the right keyword.

I will apreaciate your help!

ylim() is what you're looking for.

How would you use it in my case? I don't want just ylim(1,2). I want values smaller than 1 to be displayed but downward-facing.

Not sure, but here's an idea. Make the values smaller than 1 negative. Then use scale_y_continuous(label=) to manually put in the labels you want.

I have already used 'label' argument in my code and I don't know how to use it for the second time.

What I was thinking was doing the plot, allowing for negative values, setting ylim(), and seeing if this gets the bars where you want. If that part looks good but the tick labels are "wrong," then change the label argument to manually force them to what you want.

It looks like you got what you needed, but just in case it helps anyone to see a working solution I'm providing one. I believe you can get what you want with just 2 minor adjustments to your code.

  1. in the ggplot(aes(..)) call, replace y = value with y = value - 1. This will get the bars starting at 100%.

  2. in scale_y_continuous() replace labels = scales::percent_label(...) with labels = ~scales::percent(1 + .x, ...) this will convert the y values back into the original values for the labelling.

So the whole code would be:

ggplot(data_long, 
       aes(x = percentil, 
           # reduce the y-values by 1 (100%) to set that as "zero"
           y = value - 1, 
           fill = type)) +
  geom_bar(position="dodge", stat="identity") +
  scale_fill_manual(values = c("#00254B", "#ECB925", "#A6A6A6")) +
  theme_minimal() + 
  theme(legend.position = 'bottom',
        legend.margin=margin(0,0,0,0),
        legend.title = element_blank(),
        text = element_text(size = 15),
        axis.text.x = element_text(size = 13),
        axis.text.y = element_text(size = 13,
                                   angle = 0,
                                   hjust = 0.7),
        axis.title.x = element_text(size = 11.5),
        plot.margin = margin(20, 20, 20, 20),
        axis.title.y = element_blank(),
        panel.spacing = unit(2, "lines"),
        panel.grid.major.y = element_blank()) +
  xlab("") +
  scale_y_continuous(name = "",
# use percent() instead of label_percent() so you can edit the value for the label 
# (add the 1 back in that we removed in ggplot(aes()).
                     label = ~scales::percent(1 + .x, 
                                                    accuracy = 1, scale = 100, prefix = "", suffix = " %",
                                                    big.mark = " ", decimal.mark = ",")))

image

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.