Error when establishing an axis limit in a barplot

Hi!
I have been trying to create a barplot with a variety of features, including a limit on the y axis. However, I keep getting an error in aesthetics. This is the code and the error:

ggplot(CrownVolume1, aes(x=Art., y=Crown_volume_May, fill=Art., ylim = c(0, 30)))+
geom_bar(stat="identity") +
labs(title = "May CVP",
x = "Species",
y = "CVP (m³)")

Error in check_aesthetics():
! Aesthetics must be either length 1 or the same as the data (9): ylim
Run rlang::last_error() to see where the error occurred.


Backtrace:

  1. base (local) <fn>(x)
  2. ggplot2:::print.ggplot(x)
  3. ggplot2:::ggplot_build.ggplot(x)
  4. ggplot2 (local) by_layer(function(l, d) l$compute_aesthetics(d, plot))
  5. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
  6. l$compute_aesthetics(d, plot)
  7. ggplot2 (local) f(..., self = self)
  8. ggplot2:::check_aesthetics(evaled, n)
    Run rlang::last_trace() to see the full context.

rlang::last_trace()
<error/rlang_error>
Error in check_aesthetics():
! Aesthetics must be either length 1 or the same as the data (9): ylim


Backtrace:

  1. ├─base (local) <fn>(x)
  2. └─ggplot2:::print.ggplot(x)
  3. ├─ggplot2::ggplot_build(x)
  4. └─ggplot2:::ggplot_build.ggplot(x)
  5. └─ggplot2 (local) by_layer(function(l, d) l$compute_aesthetics(d, plot))
    
  6.   └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
    
  7.     └─l$compute_aesthetics(d, plot)
    
  8.       └─ggplot2 (local) f(..., self = self)
    
  9.         └─ggplot2:::check_aesthetics(evaled, n)
    
  10.           └─rlang::abort(...)
    

ylim should never be inside aes()
Its either its own standalone function that is + plussed on, or you are plussing on a scale_x_* function and setting a limit on that.

First check the correct name of columns.

Maybe is Art
what happen if you use geom_col() inside geom_bar() ?

Like said @nirgrahamuk , in the aes() not put ylim attributes .

I have taken it out of aes() and now I get a different error:

ggplot(CrownVolume1, aes(x=Art., y=Crown_volume_May, fill=Art.))+
geom_bar(stat="identity") +
ylim = c(0, 30) +
labs(title = "May CVP",
x = "Species",
y = "CVP (m³)")
Error in c(0, 30) + labs(title = "May CVP", x = "Species", y = "CVP (m³)") :
non-numeric argument to binary operator

Nothing changes if I do

ggplot(CrownVolume1, aes(x=Art., y=Crown_volume_May, fill=Art.))+
geom_bar(geom_col(stat="identity")) +
ylim = c(0, 30) +
labs(title = "May CVP",
x = "Species",
y = "CVP (m³)")

I still get the error

Error in c(0, 30) + labs(title = "May CVP", x = "Species", y = "CVP (m³)") :
non-numeric argument to binary operator

I have checked column names and they are ok, it is indeed "Art."

Try to put a reproducible example of data, like this:

# with a toy data, this script run well.

CrownVolume <- data.frame(
  Art = c("Species 1", "Species 2", "Species 3", "Species 4", "Species 5"),
  Crown_volume_May = c(15.2, 20.3, 10.5, 7.8, 25.1))

ggplot(CrownVolume, aes(x = Art, y = Crown_volume_May, fill = Art)) +
  geom_bar(stat = "identity") +
  ylim(0, 30) +
  labs(title = "May CVP",
       x = "Species",
       y = "CVP (m³)")
2 Likes

I think @M_AcostaCH's example solves it. Change ylim = c(0, 30) to ylim(0, 30).

It has indeed fixed it. Thank you for pointing it out, I didn't see it at first glance.
Now I have obtained the desired plot. However, I wanted to eliminate the species names in the X axis, since they are redundant. I have tried with different commands, but I have only been able to eliminate the axis title. Any idea on how I could achieve this? Thank you.

You should be able to add on the following to the end of your plot code:

theme(axis.text.x = element_blank()

Works perfectly. Thank you very much.

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.