Erro: Aesthetics must be either length 1 with geom_encircle

I have this database, see a small part below:

COD. CLIENTE NOME CLIENTE Estado MAR_20_REVENUE_MAX MAR_20_RETURN MAR_20_REVENUE
85 CLIENTE 85 PE 160.911 10.765 29.758
246 CLIENTE 246 PB 48.094 9.897 11.148
298 CLIENTE 298 PB 362.643 10.050 122.052
433 CLIENTE 433 PB 31.856 10.564 3.187
1050 CLIENTE 1050 BA 47.909 14.266 26.647
1209 CLIENTE 1209 BA 156.628 9.731 7.136
1598 CLIENTE 1598 SE 52.380 9.232 36.152

I'm using this code:

library(readxl)
library(ggplot2)
library(ggalt)

dataset<- read_excel("~/dataset.xlsx")
View(dataset)

ggplot(dataset, aes(x = dataset$MAR_20_REVENUE, y = dataset$MAR_20_RETURN))+
         geom_point(aes(col = dataset$Estado, size = dataset$MAR_20_REVENUE_MAX))+
              xlim(c(0,100000))+
              ylim(c(0,15000))+
         geom_smooth(method = "auto", se = TRUE)+
         geom_encircle(data = subconjunto,
                       color = "red",
                       size = 2,
                       expand = 0.08)
str(dataset)
       
subconjunto = dataset[dataset$MAR_20_REVENUE > 0 &  dataset$MAR_20_REVENUE <= 50000 &
                        dataset$MAR_20_RETURN >= 5000 & dataset$MAR_20_RETURN < 15000,]

And i want this result:

but when I try to make a red circle my code doesn't work. " Erro: Aesthetics must be either length 1 or the same as the data (14): x, y"

I need help!!

I have never used the ggalt package and I do not have your data but try this.

ggplot(dataset, aes(x = MAR_20_REVENUE, y = MAR_20_RETURN))+
         geom_point(aes(col = Estado, size = MAR_20_REVENUE_MAX))+
              xlim(c(0,100000))+
              ylim(c(0,15000))+
         geom_smooth(method = "auto", se = TRUE)+
         geom_encircle(data = subconjunto,
                       color = "red",
                       size = 2,
                       expand = 0.08)

You can use the bare column names in ggplot2; you do not have to repeat the data frame name after declaring it with the data = argument.

1 Like

Thanks for the solution!!

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