Tired22
February 26, 2023, 5:25pm
1
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:
base (local) <fn>
(x)
ggplot2:::print.ggplot(x)
ggplot2:::ggplot_build.ggplot(x)
ggplot2 (local) by_layer(function(l, d) l$compute_aesthetics(d, plot))
ggplot2 (local) f(l = layers[[i]], d = data[[i]])
l$compute_aesthetics(d, plot)
ggplot2 (local) f(..., self = self)
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:
▆
├─base (local) <fn>
(x)
└─ggplot2:::print.ggplot(x)
├─ggplot2::ggplot_build(x)
└─ggplot2:::ggplot_build.ggplot(x)
└─ggplot2 (local) by_layer(function(l, d) l$compute_aesthetics(d, plot))
└─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
└─l$compute_aesthetics(d, plot)
└─ggplot2 (local) f(..., self = self)
└─ggplot2:::check_aesthetics(evaled, n)
└─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 .
Tired22
February 26, 2023, 6:55pm
4
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
Tired22
February 26, 2023, 6:57pm
5
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:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
# 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)
.
Tired22
February 27, 2023, 9:35am
8
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()
Tired22
February 27, 2023, 4:34pm
10
Works perfectly. Thank you very much.
system
Closed
March 6, 2023, 4:34pm
11
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.