简体   繁体   中英

Bar plot ggplot2 - Error: Aesthetics must be either length 1 or the same as the data (150): fill, x, y

Hey I know there are lots of questions about this particular error but i still cant find what is wrong, pretty new to R and coding in general. here is a link to may data

https://www.dropbox.com/s/qfo5rp7ywgsgxhy/CRERDATA.csv?dl=0

and here is my code to make the graph

not all used for graph obviously

library(car)
library(ggplot2)
library(Rmisc)
library(dunn.test)
library(FSA)

summarizes my data so i can get standard error bars

index_sum <- summarySE(CRERDATA, measurevar = "index", groupvars = c("site", "scenario"), na.rm = TRUE)

graph code

index_graph <- ggplot(CRERDATA, aes(x = index_sum$site, y = index_sum$index, fill = index_sum$scenario)) +
  geom_bar(aes(fill = index_sum$scenario), position = position_dodge(), stat="identity") + ylab("Bleaching index") + xlab("Sites") + 
  labs(fill = "scenario") + scale_fill_grey() + theme_minimal() + 
  geom_errorbar(aes(ymin = index_sum$index-se, ymax = index_sum$index+se), width = .2, position = position_dodge(.9), color = "red")

You have specified CRERDATA in the ggplot as data , when you are actually using index_sum .

Load necessary packages:

library(ggplot2)
library(Rmisc)

Read data and summarise:

CRERDATA <- read.csv('CRERDATA.csv')
index_sum <- summarySE(CRERDATA, measurevar = "index", groupvars = c("site", "scenario"), na.rm = TRUE)

Instead of CRERDATA , use index_sum . You then don't need to use the dollar sign $ to access columns:

ggplot(index_sum, aes(x = site, y = index, fill = scenario)) + 
  geom_bar(aes(fill = scenario), position = position_dodge(), stat="identity") + 
  ylab("Bleaching index") + xlab("Sites") + labs(fill = "scenario") + scale_fill_grey() + 
  theme_minimal() +
  geom_errorbar(aes(ymin = index-se, ymax = index+se), width = .2, position = position_dodge(.9), color = "red")

The result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM