1
votes

I need help to make the ggplot2 plot for my data. This is my script in R.

> library(reshape2)
> library(ggplot2)
mdat = read.csv(file = "~/Desktop/final_data/low_group.csv", header = TRUE, sep="\t") 
> head(mdat)
         day..variable..value
1 Day 0, Actinobacteria,0.187
2 Day 3, Actinobacteria,0.117
3 Day 6, Actinobacteria,0.153
4 Day 9, Actinobacteria,0.147
5  Day 0, Bacteroidetes,0.154
6  Day 3, Bacteroidetes,0.364
> ggplot(mdat, aes(variable, value, fill=day)) + 
+     geom_bar(stat="identity", position="dodge") +
+     scale_alpha_discrete(range = c(0.5,1)) +
+     theme_minimal()
Error in eval(expr, envir, enclos) : object 'variable' not found

Any suggestion please!! Thank you very much.

2
That error means you don't have a column named variable in your dataframe. Look at what names(mdat) prints. It looks like you told read.csv that your file was tab-delimited when in fact it was probably comma-delimited - camille
+1 what Camille said above; it looks like day..variable..value is a single column name, and your data has one column. Try also dim(mdat) to check this, and replace sep="\t" with sep="" or nothing to split the data with commas - Scransom

2 Answers

2
votes

As was mentioned in comments, the problem is that you have a comma-separated file (csv) but you're reading it as if it where tab-separated. I can reproduce your problem and then rectify it by reading the file correctly.

First, I create a simple csv file:

df <- data.frame(day = c("Day 0", "Day 3", "Day 6"),
                 variable = c("Actinobacteria", "Actinobacteria", "Actinobacteria"),
                 value = c(0.187, 0.117, 0.153))
write.table(df, "low_group.csv", row.names = FALSE, sep = ", ")

Now, I read it in the way you do, with sep="\t" (tab-separated). The tell-tale sign that something is wrong is in the column header, day..variable..value, which is a single string, hence the data frame mdat contains only a single column:

mdat <- read.csv(file = "low_group.csv", header = TRUE, sep="\t") 
head(mdat)
#           day..variable..value
# 1 Day 0, Actinobacteria, 0.187
# 2 Day 3, Actinobacteria, 0.117
# 3 Day 6, Actinobacteria, 0.153

If we try to plot this we get an error. (My error message is slightly different because I run the development version of ggplot2.)

ggplot(mdat, aes(variable, value, fill=day)) + geom_col()
# Error in FUN(X[[i]], ...) : object 'variable' not found

Now let's do it correctly, without sep="\t". Now we get three separate columns in the data frame, and we can plot them.

mdat <- read.csv(file = "low_group.csv", header = TRUE) 
head(mdat)
#     day        variable value
# 1 Day 0  Actinobacteria 0.187
# 2 Day 3  Actinobacteria 0.117
# 3 Day 6  Actinobacteria 0.153
ggplot(mdat, aes(variable, value, fill=day)) + geom_col()

enter image description here

1
votes

Object variable is not found. You passed variable as the data argument to ggplot, but it doesn't exist. Try ggplot(mdat) instead, once you have split your data into three columns instead of a single one.