Last active
December 27, 2015 05:09
-
-
Save natj/7272043 to your computer and use it in GitHub Desktop.
DataFrames integration to Winston by @jverzani
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## DataFrames | |
# using Winston, RDatasets | |
# iris = data("datasets", "iris") | |
# clean_colnames!(iris) | |
# ## simple line graph | |
# plot(:Sepal_Length, :Sepal_Width, iris) | |
# ## scatterplot | |
# plot((:Sepal_Length, :Sepal_Width), iris, symboltype=".") | |
# ## plot through formula, scatterplot | |
# plot(:(Sepal_Width ~ Sepal_Length), iris, symboltype=".") | |
# ## can group | |
# plot(:(Sepal_Width ~ Sepal_Length), groupby(iris, :Species)) | |
using DataFrames | |
using Color | |
## mirror regular usage of plot(x, args...; kwargs...) | |
function _plot(p::FramedPlot, y::Symbol, data::AbstractDataFrame, args...;kwargs...) | |
_plot(p, with(data,y), args...; kwargs...) | |
end | |
## mirror regular usage of plot(x, y, args...; kwargs...) | |
function _plot(p::FramedPlot, x::Symbol, y::Symbol, data::AbstractDataFrame, args...;kwargs...) | |
_plot(p, with(data, x), with(data, y), args...; kwargs...) | |
end | |
## mirror regular usage of plot((x, y), args...; kwargs...) | |
typealias SymbolScatterPlotPoints (Symbol, Symbol) | |
_plot(p::FramedPlot, x::SymbolScatterPlotPoints, data::AbstractDataFrame, args...; kwargs...) = | |
_plot(p, (with(data, x[1]).data, with(data, x[2]).data), args...; kwargs...) | |
## Formula -> (x,y) | |
function _plot(p::FramedPlot, x::Formula, data::AbstractDataFrame, args...;kwargs...) | |
m = ModelFrame(x, data) | |
df = m.df | |
terms = m.terms.eterms | |
_plot(p, (terms[1], terms[2]), df, args...; kwargs...) | |
end | |
## Expr -> Formula | |
function _plot(p::FramedPlot, x::Expr, data::AbstractDataFrame, args...;kwargs...) | |
_plot(p, Formula(x), data, args...; kwargs...) | |
end | |
## Grouped data frame | |
## recycles symboltypes | |
function _plot(p::FramedPlot, x::Formula, g::GroupedDataFrame, args...; | |
symboltypes=["+", "o", "*", ".", "x", "s", "d", "^", "v", ">", "<"], | |
colors = linspace(color("red"), color("blue"), length(g)), | |
kwargs...) | |
for i in 1:length(g) | |
_plot(p, x, g[i], symboltype=symboltypes[1 + rem(i-1, length(symboltypes))], color=colors[i]) | |
end | |
p | |
end | |
function _plot(p::FramedPlot, x::Expr, g::GroupedDataFrame, args...; kwargs...) | |
_plot(p, Formula(x), g, args...; kwargs...) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment