user153532
user153532

Reputation: 1

plot command isn't recognizing column names

Recently when I tried to plot in R I keep getting this error. Can anyone tell me why I can't seem to do a scatter plot? I've pasted the terminal screen below.

tcmg2o4 <-read.table("~/Documents/research/metal.oxides/TcMg2O4.inverse/energydata.txt")

tcmg2o4
                 V1            V2
1  Lattice_constant  Total_energy
2               8.0 -371.63306746
3               8.1   -375.035492
4               8.2  -378.8669067
5               8.3 -380.34136459
6               8.4  -382.3921237
7               8.5 -383.60394736
8               8.6 -384.09517631
9               8.7 -383.77668067
10              8.8 -382.43806866
11              8.9 -381.42213458
12              9.0 -379.63327976

attach(tcmg2o4)
plot(Lattice_constant, Total_energy)

Error in plot(Lattice_constant, Total_energy) : 
object 'Lattice_constant' not found

plot(V1,V2)

Upvotes: 0

Views: 3927

Answers (1)

mnel
mnel

Reputation: 115390

Your problem is that you are not reading the column names as column names. to do this use header = T

 tcmg2o4 <-read.table("~/Documents/research/metal.oxides/TcMg2O4.inverse/energydata.txt", header = T)

In your case, the read.table call has created column names V1 and V2 and these columns will both be factor variables.

You can check the structure of your read in object by

str(tcmg2o4)
## 'data.frame':    11 obs. of  2 variables:
##  $ Lattice_constant: num  8 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 ...
##  $ Total_energy    : num  -372 -375 -379 -380 -382 ...

I would also avoid using attach

instead use with or

with(tcmg2o4, plot(Lattice_constant, Total_energy))

or the fact that it is a 2 column data.frame

plot(tcmg2o4)

or use a formula to specify your x and y axis (y~x)

plot(Total_energy ~ Lattice_constant, data = tcmg2o4)

which will all give the same result and be much clearer as to where the data is stored

enter image description here

Upvotes: 6

Related Questions