-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathggplot2_examples.py
More file actions
151 lines (129 loc) · 5.24 KB
/
Copy pathggplot2_examples.py
File metadata and controls
151 lines (129 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
##
## Examples using ggplot2 from Rpy2
##
import os
import sys
import time
import scipy
import numpy as np
from r_plot import *
# Data directory
data_dir = "./data"
# Load up the iris dataset
iris_df = pandas.read_table(os.path.join(data_dir, "iris.csv"),
sep=",")
print iris_df
# Make Species column to make it more similar to R iris dataset
iris_df["Species"] = iris_df["Name"]
# Make plots dir
plots_dir = "./plots"
if not os.path.isdir(plots_dir):
os.makedirs(plots_dir)
# Make non-melted dataframe example
def get_nonmelted_df():
df = pandas.DataFrame({"gene": np.arange(200),
"sample1_expr": scipy.random.uniform(size=200),
"sample2_expr": scipy.random.uniform(size=200),
"binding": np.array(["no"]*50+["yes"]*50+["maybe"]*100),
"peak_height": scipy.random.uniform(size=200)*5})
return df
def iris_plots():
"""
Make some plots with iris
"""
# Convert iris to R dataframe before plotting
r_df = conversion_pydataframe(iris_df)
plot_fname = os.path.join(plots_dir, "iris1.pdf")
r.pdf(plot_fname)
# Simple scatter plot
p = ggplot2.ggplot(r_df) + \
ggplot2.geom_point(aes_string(x="SepalWidth", y="SepalLength",
colour="Species"))
p.plot()
plot_fname = os.path.join(plots_dir, "iris2.pdf")
r.pdf(plot_fname, width=9, height=5)
# Separate panel for each species
p = ggplot2.ggplot(r_df) + \
ggplot2.geom_point(aes_string(x="SepalWidth", y="SepalLength",
colour="Species")) + \
ggplot2.facet_grid(Formula("~ Species"))
p.plot()
# Horizontal boxplots
plot_fname = os.path.join(plots_dir, "iris3.pdf")
r.pdf(plot_fname, width=9, height=5)
p = ggplot2.ggplot(r_df) + \
ggplot2.geom_boxplot(aes_string(x="Species", y="SepalWidth", fill="Species")) + \
ggplot2.coord_flip()
p.plot()
def melting_dfs():
"""
Take unmelted df and melt it.
"""
df = get_nonmelted_df()
plot_fname = os.path.join(plots_dir, "melt1.pdf")
r.pdf(plot_fname, width=5, height=5)
# First, no melting required for many things
r_df = conversion_pydataframe(df)
p = ggplot2.ggplot(r_df) + \
ggplot2.geom_point(aes_string(x="sample1_expr", y="sample2_expr",
size="peak_height",
colour="factor(binding)")) + \
ggplot2.facet_grid(Formula("binding ~ ."))
p.plot()
# Histogram sample1's expression for each binding state
plot_fname = os.path.join(plots_dir, "melt2.pdf")
r.pdf(plot_fname, width=9, height=5)
p = ggplot2.ggplot(r_df) + \
ggplot2.geom_histogram(aes_string(x="sample1_expr", fill="binding"),
color="white") + \
ggplot2.facet_grid(Formula("~ binding"))
p.plot()
# In the original dataframe, we have a column for each
# sample's expression values: sample1_expr, sample2_expr, etc.
# If we want to carve up the data for plotting according to
# which sample's gene expression is being used (i.e. we want to
# treat the sample name as a variable) then we need to melt
# the dataframe. This can be easily done with pandas like this:
## Melt the dataframe so that the only variable is whether we're looking
## at sample1 or sample2
melted_df = pandas.melt(df, id_vars=["gene", "peak_height", "binding"])
# The melted dataframe is now twice as big, since two distinct columns got merged
# into long form ("melted form") as rows
print "Melted df has %d elements." %(len(melted_df))
# Make it an R dataframe
r_melted = conversion_pydataframe(melted_df)
# Plot the expression for each sample across different bindings
plot_fname = os.path.join(plots_dir, "melt3.pdf")
r.pdf(plot_fname)
p = ggplot2.ggplot(r_melted) + \
ggplot2.geom_histogram(aes_string(x="value", colour="binding"), fill="white") + \
ggplot2.facet_grid(Formula("variable ~ binding"))
p.plot()
print r_melted
# plot_fname = os.path.join(plots_dir, "melt4.pdf")
# r.pdf(plot_fname)
# p = ggplot2.ggplot(r_melted) + \
# ggplot2.geom_area(aes_string(x="gene", y="value", group="binding", fill="binding"), position="fill")# + \
# #ggplot2.facet_grid(Formula("variable ~ binding"))
# p.plot()
def noahtest():
df = pandas.DataFrame({"idx":range(5),
"het":[0.2, 0.4, 0.4, 0.3, 0.3],
"hom":[0.6, 0.4, 0.4, 0.7, 0.6],
"unk":[0.2, 0.2, 0.2, 0.0, 0.1]})
melted_df = pandas.melt(df, id_vars="idx")
r_melted = conversion_pydataframe(melted_df)
r.pdf("plots/noahtest.pdf")
p = ggplot2.ggplot(r_melted) + \
ggplot2.geom_area(aes_string(x="idx", y="value", group="variable", fill="variable"), position="fill")
p.plot()
# p = ggplot2.ggplot(r_melted) + \
# ggplot2.geom_histogram(aes_string(x="idx", colour="variable"))#, fill="variable")# + \
# #ggplot2.facet_grid(Formula("variable ~ binding"))
# p.plot()
def main():
#iris_plots()
#melting_dfs()
noahtest()
if __name__ == "__main__":
main()