Rグラフィックスクックブック ―ggplot2によるグラフ作成のレシピ集 にでている例題をpython版ggplotで試し、ggplotでサポートしていない部分はRのggplot2をSageから操作してプロットしてみました。
Sageでデータをプロットするときに参考にしてください。
|
Pandasを使って連続値の区間で区切って処理する方法の紹介です。知っていると便利です。
age sex 0 20 F 1 22 M 2 25 M 3 27 M 4 21 F 5 23 M 6 37 F 7 31 M 8 61 F 9 45 M 10 41 F 11 32 M age sex 0 20 F 1 22 M 2 25 M 3 27 M 4 21 F 5 23 M 6 37 F 7 31 M 8 61 F 9 45 M 10 41 F 11 32 M |
age sex bins 0 20 F youth 1 22 M youth 2 25 M youth 3 27 M YoungAdult 4 21 F youth age sex bins 0 20 F youth 1 22 M youth 2 25 M youth 3 27 M YoungAdult 4 21 F youth |
ageMonth ageYear heightIn sex weightLb 0 143 11.92 56.3 f 85.0 1 155 12.92 62.3 f 105.0 2 153 12.75 63.3 f 108.0 3 161 13.42 59.0 f 92.0 4 191 15.92 62.5 f 112.5 ageMonth ageYear heightIn sex weightLb 0 143 11.92 56.3 f 85.0 1 155 12.92 62.3 f 105.0 2 153 12.75 63.3 f 108.0 3 161 13.42 59.0 f 92.0 4 191 15.92 62.5 f 112.5 |
ageMonth ageYear heightIn sex weightLb 231 164 13.67 66.5 m 112.0 232 189 15.75 65.0 m 114.0 233 164 13.67 61.5 m 140.0 234 167 13.92 62.0 m 107.5 235 151 12.58 59.3 m 87.0 ageMonth ageYear heightIn sex weightLb 231 164 13.67 66.5 m 112.0 232 189 15.75 65.0 m 114.0 233 164 13.67 61.5 m 140.0 234 167 13.92 62.0 m 107.5 235 151 12.58 59.3 m 87.0 |
<ggplot: (8769175188637)> <ggplot: (8769175188637)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
pressure temperature 0 0.0002 0 1 0.0012 20 2 0.0060 40 3 0.0300 60 4 0.0900 80 pressure temperature 0 0.0002 0 1 0.0012 20 2 0.0060 40 3 0.0300 60 4 0.0900 80 |
<ggplot: (8769175112865)> <ggplot: (8769175112865)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
Traceback (click to the left of this block for traceback) ... AttributeError: 'EvalEnvironment' object has no attribute 'add_outer_namespace' Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_25.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("IyBSZWMuMi4zIOajkuOCsOODqeODleOCkuS9nOaIkOOBmeOCiwojIGN5bOOBr+mAo+e2muWApOOBquOBruOBp+OAgWZhY3RvcuOBp+mbouaVo+OBqOOBl+OBpuaJseOBhgojIGZhY3RvcuOBjOS4iuaJi+OBj+apn+iDveOBl+OBpuOBhOOBquOBhApnZ3Bsb3QobXRjYXJzLCBhZXMoeD0nZmFjdG9yKGN5bCknKSkgK2dlb21fYmFyKCk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpVEH1Fr/___code___.py", line 5, in <module> exec compile(u"ggplot(mtcars, aes(x='factor(cyl)')) +geom_bar()" + '\n', '', 'single') File "", line 1, in <module> File "/usr/local/sage-6.7/local/lib/python2.7/site-packages/ggplot-0.6.5-py2.7.egg/ggplot/ggplot.py", line 66, in __init__ self.data = _apply_transforms(data, self.aesthetics) File "/usr/local/sage-6.7/local/lib/python2.7/site-packages/ggplot-0.6.5-py2.7.egg/ggplot/ggplot.py", line 548, in _apply_transforms env.add_outer_namespace({"factor":factor}) AttributeError: 'EvalEnvironment' object has no attribute 'add_outer_namespace' |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769174542105)> <ggplot: (8769174542105)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
dose len supp 0 0.5 4.2 VC 1 0.5 11.5 VC 2 0.5 7.3 VC 3 0.5 5.8 VC 4 0.5 6.4 VC dose len supp 0 0.5 4.2 VC 1 0.5 11.5 VC 2 0.5 7.3 VC 3 0.5 5.8 VC 4 0.5 6.4 VC |
|
|
|
|
group weight 0 ctrl 5.03200000000000 1 trt1 4.66100000000000 2 trt2 5.52600000000000 group weight 0 ctrl 5.03200000000000 1 trt1 4.66100000000000 2 trt2 5.52600000000000 |
<ggplot: (8769174493181)> <ggplot: (8769174493181)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
Time demand 0 1 8.3 1 2 10.3 2 3 19.0 3 4 16.0 4 5 15.6 Time demand 0 1 8.3 1 2 10.3 2 3 19.0 3 4 16.0 4 5 15.6 |
<ggplot: (8769174385521)> <ggplot: (8769174385521)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
Traceback (click to the left of this block for traceback) ... AttributeError: 'EvalEnvironment' object has no attribute 'add_outer_namespace' Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_51.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("IyBhZXPjga5mYWN0b3IoVGltZSksIGdlb21fYmFy44Guc3RhdD0iaWRlbnRpdHki44GM5Yq544GL44Gq44GECmdncGxvdChCT0QsIGFlcyh4PSdmYWN0b3IoVGltZSknLCB3ZWlnaHQ9J2RlbWFuZCcpKSArIGdlb21fYmFyKHN0YXQ9ImlkZW50aXR5Iik="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpR_yR_4/___code___.py", line 3, in <module> exec compile(u'ggplot(BOD, aes(x=\'factor(Time)\', weight=\'demand\')) + geom_bar(stat="identity") File "", line 1, in <module> File "/usr/local/sage-6.7/local/lib/python2.7/site-packages/ggplot-0.6.5-py2.7.egg/ggplot/ggplot.py", line 66, in __init__ self.data = _apply_transforms(data, self.aesthetics) File "/usr/local/sage-6.7/local/lib/python2.7/site-packages/ggplot-0.6.5-py2.7.egg/ggplot/ggplot.py", line 548, in _apply_transforms env.add_outer_namespace({"factor":factor}) AttributeError: 'EvalEnvironment' object has no attribute 'add_outer_namespace' |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769173888185)> <ggplot: (8769173888185)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
Cultivar Date Weight 0 c39 d16 3.18000000000000 1 c39 d20 2.80000000000000 2 c39 d21 2.74000000000000 3 c52 d16 2.26000000000000 4 c52 d20 3.11000000000000 5 c52 d21 1.47000000000000 Cultivar Date Weight 0 c39 d16 3.18000000000000 1 c39 d20 2.80000000000000 2 c39 d21 2.74000000000000 3 c52 d16 2.26000000000000 4 c52 d20 3.11000000000000 5 c52 d21 1.47000000000000 |
<ggplot: (8769173716461)> <ggplot: (8769173716461)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
<ggplot: (8769173640493)> <ggplot: (8769173640493)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
Source Year Anomaly1y Anomaly5y Anomaly10y Unc10y pos 101 Berkeley 1900 NA NA -0.171 0.108 FALSE 102 Berkeley 1901 NA NA -0.162 0.109 FALSE 103 Berkeley 1902 NA NA -0.177 0.108 FALSE 104 Berkeley 1903 NA NA -0.199 0.104 FALSE 105 Berkeley 1904 NA NA -0.223 0.105 FALSE 106 Berkeley 1905 NA NA -0.241 0.107 FALSE Source Year Anomaly1y Anomaly5y Anomaly10y Unc10y pos 101 Berkeley 1900 NA NA -0.171 0.108 FALSE 102 Berkeley 1901 NA NA -0.162 0.109 FALSE 103 Berkeley 1902 NA NA -0.177 0.108 FALSE 104 Berkeley 1903 NA NA -0.199 0.104 FALSE 105 Berkeley 1904 NA NA -0.223 0.105 FALSE 106 Berkeley 1905 NA NA -0.241 0.107 FALSE |
|
<ggplot: (8769173601385)> <ggplot: (8769173601385)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
Cultivar Date Weight sd n se 1 c39 d16 3.18 0.9566144 10 0.30250803 2 c39 d20 2.80 0.2788867 10 0.08819171 3 c39 d21 2.74 0.9834181 10 0.31098410 4 c52 d16 2.26 0.4452215 10 0.14079141 5 c52 d20 3.11 0.7908505 10 0.25008887 6 c52 d21 1.47 0.2110819 10 0.06674995 Cultivar Date Weight sd n se 1 c39 d16 3.18 0.9566144 10 0.30250803 2 c39 d20 2.80 0.2788867 10 0.08819171 3 c39 d21 2.74 0.9834181 10 0.31098410 4 c52 d16 2.26 0.4452215 10 0.14079141 5 c52 d20 3.11 0.7908505 10 0.25008887 6 c52 d21 1.47 0.2110819 10 0.06674995 |
|
|
|
|
<ggplot: (8769173601473)> <ggplot: (8769173601473)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
|
<ggplot: (8769173371653)> <ggplot: (8769173371653)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769173323185)> <ggplot: (8769173323185)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
<ggplot: (8769173255973)> <ggplot: (8769173255973)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
supp dose length 1 OJ 0.5 13.23 2 OJ 1.0 22.70 3 OJ 2.0 26.06 4 VC 0.5 7.98 5 VC 1.0 16.77 6 VC 2.0 26.14 supp dose length 1 OJ 0.5 13.23 2 OJ 1.0 22.70 3 OJ 2.0 26.06 4 VC 0.5 7.98 5 VC 1.0 16.77 6 VC 2.0 26.14 |
|
<ggplot: (8769173233133)> <ggplot: (8769173233133)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
Traceback (click to the left of this block for traceback) ... ggplot.utils.exceptions.GgplotError: u'Cannot recognize argument: linestyle' Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_94.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("IyBSZWMuNC40IOe3muOBruS9k+ijgeOCkuWkieabtOOBmeOCiyhQeXRob27niYgp44CAUuOBp+OBr2xpbmV0eXBl44Gn57ea56iu44KS5oyH5a6aCmdncGxvdChCT0QsIGFlcyh4PSdUaW1lJywgeT0nZGVtYW5kJykpICsgXAogICAgZ2VvbV9saW5lKGxpbmVzdHlsZT0iZGFzaGVkIiwgY29sb3I9ImJsdWUiKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpcLBMeL/___code___.py", line 3, in <module> exec compile(u'ggplot(BOD, aes(x=\'Time\', y=\'demand\')) + \\\n geom_line(linestyle="dashed", color="blue") File "", line 2, in <module> File "/usr/local/sage-6.7/local/lib/python2.7/site-packages/ggplot-0.6.5-py2.7.egg/ggplot/geoms/geom_line.py", line 16, in __init__ super(geom_line, self).__init__(*args, **kwargs) File "/usr/local/sage-6.7/local/lib/python2.7/site-packages/ggplot-0.6.5-py2.7.egg/ggplot/geoms/geom.py", line 102, in __init__ raise GgplotError('Cannot recognize argument: %s' % k) ggplot.utils.exceptions.GgplotError: u'Cannot recognize argument: linestyle' |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769173008937)> <ggplot: (8769173008937)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
Sunspots Year 0 5 1700 1 11 1701 2 16 1702 3 23 1703 4 36 1704 Sunspots Year 0 5 1700 1 11 1701 2 16 1702 3 23 1703 4 36 1704 |
<ggplot: (8769172999013)> <ggplot: (8769172999013)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
AgeGroup Thousands Year 0 <5 9181 1900 1 5-14 16966 1900 2 15-24 14951 1900 3 25-34 12161 1900 4 35-44 9273 1900 <repr(<ggplot.ggplot.ggplot at 0x7f9bb6f5d4d0>) failed: GgplotError: u'geom_area requires the following missing aesthetics: ymin, ymax'> AgeGroup Thousands Year 0 <5 9181 1900 1 5-14 16966 1900 2 15-24 14951 1900 3 25-34 12161 1900 4 35-44 9273 1900 <repr(<ggplot.ggplot.ggplot at 0x7f9bb6f5d4d0>) failed: GgplotError: u'geom_area requires the following missing aesthetics: ymin, ymax'> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
|
|
<ggplot: (8769172840921)> <ggplot: (8769172840921)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
<ggplot: (8769172800873)> <ggplot: (8769172800873)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769172752329)> <ggplot: (8769172752329)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769172790025)> <ggplot: (8769172790025)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769172609373)> <ggplot: (8769172609373)> |
|
<ggplot: (8769172585881)> <ggplot: (8769172585881)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769172538865)> <ggplot: (8769172538865)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769172410877)> <ggplot: (8769172410877)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769172388325)> <ggplot: (8769172388325)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769172362685)> <ggplot: (8769172362685)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
|
|
<ggplot: (8769172254553)> <ggplot: (8769172254553)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769171613869)> <ggplot: (8769171613869)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769171329417)> <ggplot: (8769171329417)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769171106017)> <ggplot: (8769171106017)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769171036313)> <ggplot: (8769171036313)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
|
<ggplot: (8769171118737)> <ggplot: (8769171118737)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
<ggplot: (8769170935665)> <ggplot: (8769170935665)> |
Saving 11.0 x 8.0 in image. Saving 11.0 x 8.0 in image. |
|
[1] "maps" "gcookbook" "ggplot2" "stats" "graphics" "grDevices" "utils" "datasets" [9] "methods" "base" [1] "maps" "gcookbook" "ggplot2" "stats" "graphics" "grDevices" "utils" "datasets" [9] "methods" "base" |
|
|
|
|
|
|
|
|
**** Warning: Invalid Page count. **** Warning: Invalid Page count. **** This file had errors that were repaired or ignored. **** The file was produced by: **** >>>> R 3.2.0 <<<< **** Please notify the author of the software that produced this **** file that it does not conform to Adobe's published PDF **** specification. convert: Postscript delegate failed `fig-13.22.pdf': そのようなファイルやディレクトリはありません @ error/pdf.c/ReadPDFImage/664. convert: missing an image filename `fig-13.22.png' @ error/convert.c/ConvertImageCommand/3015. **** Warning: Invalid Page count. **** Warning: Invalid Page count. **** This file had errors that were repaired or ignored. **** The file was produced by: **** >>>> R 3.2.0 <<<< **** Please notify the author of the software that produced this **** file that it does not conform to Adobe's published PDF **** specification. convert: Postscript delegate failed `fig-13.22.pdf': そのようなファイルやディレクトリはありません @ error/pdf.c/ReadPDFImage/664. convert: missing an image filename `fig-13.22.png' @ error/convert.c/ConvertImageCommand/3015. |
|