I am learning visualisation in R and have been facing some issues trying to make a categorical bar graph from data that I extracted from a .xlsx file in R.
The input had numerous columns and I created a dataframe with the two columns i needed for the graph. When i run the following in R, I get the error
"Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?"
This is my script:
df<-read_excel("C:\\..\\excel.xlsx", sheet="Sheet1")
unique_df<-unique(mis[c("Vegetables", "Fruits")])
ggplot(data = unique_df, mapping = aes(x = as.factor(unique_df["Vegetables"]),
y = unique_df["Fruits"])) + geom_bar(stat = "identity") +
labs(x = "Vegetables", y="Fruits", title="Number of Codes", subtitle="Vegetables wise number of fruits")
This is similar to the dataframe (unique_df) that I need to make the visualisation from:
x y
Red Mango
Red Apple
Red Banana
Blue Mango
Blue Banana
Blue Banana
Blue Apple
Yellow Pineapple
Green Mango
Yellow Pineapple
Yellow Pineapple
Green Apple
Output of str(unique_df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 219 obs. of 2 variables:
x: chr "Red" "Red" "Red" "Blue" "Blue"...
y: chr "Mango" "Apple" "Banana" "Mango"...