Reputation: 1877
I am trying to plot a specific column of a data frame with a name containing a space in Julia Plots.jl (GR backend).
Here is my data frame structure:
17-element Array{Symbol,1}:
Symbol("DATE/TIME")
[...]
:TEMPERATURE
Symbol("EXT TEMPERATURE")
:PIM
[...]
Here's my code:
using CSV, DataFrames
me=CSV.File(joinpath(dir,"myfile.csv"),delim=';')
medf=me|>DataFrame
using Plots,StatPlots,GR
@df medf GR.plot(Symbol("EXT TEMPERATURE"),title="Temperature")
This fails with:
MethodError: no method matching plot(::Symbol)
Closest candidates are:
plot(!Matched::Union{AbstractString, Function, AbstractArray{T,2} where T, AbstractArray{T,1} where T}...; kv...) at C:\Users\condo\.julia\packages\GR\joQgG\src\jlgr.jl:1118
I can:
@df medf GR.plot(:TEMPERATURE)
But I can't:
@df medf GR.plot(:EXT TEMPERATURE)
How should I go about referencing this column in this particular situation?
Upvotes: 3
Views: 1342
Reputation: 3162
I've just hit this issue while importing an SPSS file with a .sav
extension. One may also reference the symbol with a space in the name by doing the following:
Symbol("SOME NAME WITH SPACE")
I noticed you had done that in some code of yours but not everywhere you use the name with spaces. You should be able to do that in subsequent calls too. For example you could have:
@df medf GR.plot(Symbol("EXT TEMPERATURE"))
In my case I have a lot of columns in my SPSS file with spaces in the name so I wrote a macro to do this for me:
macro s_str(s)
Expr(:quote, Symbol(s))
end
Then subsequent calls can be done like this:
@df medf GR.plot(s"EXT TEMPERATURE")
I'm using this with good results on DataFrames, YMMV on other packages.
Upvotes: 2
Reputation: 11942
Macro @df
in StatPlots
seems to have a few limitations. Fortunately, the StatPlots
package provides cols()
as a workaround for your situation.
Try:
using CSV, DataFrames
me=CSV.File(joinpath(dir,"myfile.csv"),delim=';')
medf=me|>DataFrame
using StatPlots
gr() # N.B.: You can include things like size=(400,300), or dpi=400 here as params
@df medf plot(cols(Symbol("EXT TEMPERATURE")),title="Temperature")
Upvotes: 3
Reputation: 6086
Julia has a tendency to treat the Symbol constant :symb as if it were a variable name in some contexts, and so it becomes awkward if you try to use Symbols containing spaces as if they are variable names, since variable names in Julia cannot contain spaces.
You can however also refer to the DataFrame column by its position this way:
using CSV, DataFrames, Plots
iob = IOBuffer("""DATE/TIME,EXT TEMPERATURE\n2018-10-10 12:30, 22.4\n2018-10-11 08:10,26.1\n""")
df = convert(DataFrame, CSV.read(iob))
plot(df[2], ylabel=names(df)[2])
Upvotes: 1