Error: Aesthetics must be either length 1 or the same as the data (68): y

Can someone help me here. I am trying to create a circular barplot. but the code is throwing an error. Pls help.

Dataset:
Name Case
A 100
B 50 ......
Total rows 58 + 10 blank rows

library(tidyverse)

# Create dataset
data <-Dataset
value <- Dataset$Cases
# Set a number of 'empty bar'
empty_bar <- 10

# Add lines to the initial dataset
to_add <- matrix(NA, empty_bar, ncol(data))
colnames(to_add) <- colnames(data)
data <- rbind(data, to_add)
data$id <- seq(1, nrow(data))

# Get the name and the y position of each label
label_data <- data
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) /number_of_bar   
  # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
label_data$hjust <- ifelse( angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)

# Make the plot
PChart <- ggplot(data, aes(x=as.factor(id), y=value)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar
  geom_bar(stat="identity", fill=alpha("green", 0.3)) +
  ylim(-100,120) +
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-1,4), "cm") 
  ) +
  coord_polar(start = 0) + 
  geom_text(data=label_data, 
            aes(x=id, y=value, label=Name,
            hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, 
            angle= label_data$angle, inherit.aes = FALSE ) 
PChart

Without a reproducible example, it's hard to say what is wrong, but just from a quick glance, I think it might be in the geom_text(... angle= label_data$angle ...). Have you tried putting angle = angle in the aes?

Also, I am curious why you separated label_data and data when all of the information is in label_data?

Tried changing to "angle", but I am still getting the same error.

The label_data has all data from the dataset (rows 58) + an additional 10 blank rows. Thats why I used label_data instead of data.

Well according to your question, Dataset has 68 rows (58 + 10 blank rows). Can you clarify your question with a reproducible example?

Also, did you change it to

geom_text(data=label_data,
aes(x=id, y=value, label=Name,
hjust=hjust, angle = angle), color="black", fontface="bold",alpha=0.6, size=2.5, inherit.aes = FALSE )

and not

geom_text(data=label_data,
aes(x=id, y=value, label=Name,
hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5,
angle= angle, inherit.aes = FALSE )
1 Like

As per your suggestion I moved the ')' after angle, but is throwing the same error:
geom_text(data=label_data,
aes(x=id, y=value, label=State_Names,
hjust=hjust, angle = angle),color="black", fontface="bold",alpha=0.6, size=2.5,
inherit.aes = FALSE )
2. I tried to reproduce using my dataset, which seems not working, but when I tried using the example which I took from a webpage, it worked. the sample code is given below:

data <- data.frame(
  individual=paste( "Mister ", seq(1,60), sep=""),
  value=sample( seq(10,100), 60, replace=T)
)
 
# Set a number of 'empty bar'
empty_bar <- 10
 
# Add lines to the initial dataset
to_add <- matrix(NA, empty_bar, ncol(data))
colnames(to_add) <- colnames(data)
data <- rbind(data, to_add)
data$id <- seq(1, nrow(data))
 
# Get the name and the y position of each label
label_data <- data
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) /number_of_bar     # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
label_data$hjust <- ifelse( angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)
 
# Make the plot
pChart <- ggplot(data, aes(x=as.factor(id), y=value)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar
  geom_bar(stat="identity", fill=alpha("green", 0.3)) +
  ylim(-100,120) +
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-1,4), "cm") 
  ) +
  coord_polar(start = 0) + 
  geom_text(data=label_data, aes(x=id, y=value+10, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) 
 
pChart

What are the column names of data and label_data? Somewhere I think you have a mismatch between the variable names in the aes and the names in the datasets.

data: Column names: Names, Cases, id
label_data: Column names: Names , Cases, id, hjust, angle

Ok then I think the problem is you are setting y=value, but you do not have a value column in either of those datasets. I think y should be Cases

Edited to add: Also in geom_text label should be Names with an s to match the column

Thanks, when I changed as per your instructions, I can finally see the chart, but now I am getting the below error:
1: Removed 63 rows containing missing values (position_stack).
2: Removed 63 rows containing missing values (geom_text).

Those are generally warnings when you have NAs in your data or the data is outside the plotting window. Since you only have 68 rows in your data frames, ggplot is saying 63 of those rows are missing in some way. You should be able to tell from the plot which cases are causing an issue and go from there. Again, it's difficult to answer without having a copy of your data in the question.

Thank you for your help, I figured out what the issue was and sorted the same.

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