Shalen
Shalen

Reputation: 95

Trying to solve the error: Error: `data` must be a data frame, or other object coercible by `fortify()`, not a character vector

I have tried to make some of my plots transparent but when I added the "alpha" I have got the following error. Does anybody have any idea how I should correct it?

Error: `data` must be a data frame, or other object coercible by `fortify()`, not a character vector

Here is my code:

graphics.off()
rm(list=ls())

library(ggplot2)
library(dplyr)
library(tidyr)
library(tidyverse)

setwd("F:/Data/")

file1<-read.csv("F:/Data/diam.csv")

cl<-rainbow(20)

names(file1)


ggplot(data=file1, aes(x = No.)) + 
  geom_line(aes(y = X1), colour="red")+
  geom_line(aes(y = X2), colour="coral4",alpha(.4))+
  geom_line(aes(y = X3), colour=cl[8],alpha(.4))+
  geom_line(aes(y = X4), colour="magenta")+
  xlab("Image ")+ylab("n pores<0.13")+
  theme(legend.position="bottom")

Upvotes: 1

Views: 486

Answers (3)

Carlos
Carlos

Reputation: 1

I previously had this error also. I solved it myself after a little tinkering. For me the this problem came about because I used Kaggle. Kaggle gets one to upload their CSV file, then gives the path to access that CSV file. This was the path it gave me:

"/kaggle/input/housing-register-project-1/D.REQ.BDR.csv"

Instead, I used only:

"/input/housing-register-project-1/D.REQ.BDR.csv"

That alone fixed the "data must be a data frame, or other object coercible by fortify(), not a character vector" error for me.

Upvotes: 0

SKyJim
SKyJim

Reputation: 109

There might be something weird in your csv file that is making the output read.csv be a different data type than a data frame. I would str(file1) and/or head(file1) to make sure the object looks like you intend it to. If it does then you can try coercing it to a data frame by doing file1 <- as.data.frame(file1), So then it would fail again if you don't fix those too, but I believe this error is previous to that.

Upvotes: 1

akrun
akrun

Reputation: 887501

Perhaps, it would be alpha = 0.4

library(ggplot2)
ggplot(data=file1, aes(x = No.)) + 
  geom_line(aes(y = X1), colour="red")+
  geom_line(aes(y = X2), colour="coral4",alpha = 0.4)+
  geom_line(aes(y = X3), colour=cl[8],alpha = 0.4)+
  geom_line(aes(y = X4), colour="magenta")+
  xlab("Image ")+ylab("n pores<0.13")+
  theme(legend.position="bottom")

Upvotes: 3

Related Questions