Plot with ggplot2, interact, collaborate, and share online
来源:互联网 发布:淘宝插件看别人数据 编辑:程序博客网 时间:2024/05/17 08:08
Editor’s note: This is a guest post by Marianne Corvellec fromPlotly. This post is based on an interactive Notebook (click to view) she presented at the R User Conference on July 1st, 2014.
Plotly is a platform for making, editing, and sharing graphs. If you are used to making plots with ggplot2, you can callggplotly() to make your plots interactive, web-based, and collaborative. For example, seeplot.ly/~ggplot2examples/211, shown below and in this Notebook. Notice the hover text!
0. Get started
Visit http://plot.ly. Here, you’ll find a GUI that lets you create graphs from data you enter manually, or upload as a spreadsheet (or CSV file). From there you can edit graphs! Change between types (from bar charts to scatter charts), change colors and formatting, add fits and annotations, try other themes…
Our R API lets you usePlotly with R. Once you have your R visualization in Plotly, you can use the web interface to edit it, or to extract its data. Install and load package “plotly” in your favourite R environment. For a quick start, follow:https://plot.ly/ggplot2/getting-started/
Go social! Like, share, comment, fork and edit plots… Export them, embed them in your website. Collaboration has never been so sweet!
Not ready to publish? Set detailed permissions for who can view and who can edit your project.
1. Make a (static) plot with ggplot2
Baseball data is the best! Let’s plot a histogram of batting averages. I downloaded datahere.
Load the CSV file of interest, take a look at the data, subset at will:
library
(RCurl)
online_data <-
getURL
(
"https://raw.githubusercontent.com/mkcor/baseball-notebook/master/Batting.csv"
)
batting_table <-
read.csv
(
textConnection
(online_data))
head
(batting_table)
summary
(batting_table)
batting_table <-
subset
(batting_table, yearID >= 2004)
The batting average is defined by the number of hits divided by at bats:
batting_table$Avg <-
with
(batting_table, H / AB)
You may want to explore the distribution of your new variable as follows:
library
(ggplot2)
ggplot
(data=batting_table)
+
geom_histogram
(
aes
(Avg), binwidth=0.05)
# Let's filter out entries where players were at bat less than 10 times.
batting_table <-
subset
(batting_table, AB >= 10)
hist <-
ggplot
(data=batting_table) +
geom_histogram
(
aes
(Avg),
binwidth=0.05)
hist
We have created a basic histogram; let us share it, so we can get input from others!
2. Save your R plot to plot.ly
# Install the latest version
# of the “plotly” package and load it
library
(devtools)
install_github
(
"ropensci/plotly"
)
library
(plotly)
# Open a Plotly connection
py <-
plotly
(
"ggplot2examples"
,
"3gazttckd7"
)
Use your own credentials if you prefer. You can sign up for a Plotly accountonline.
Now call the `ggplotly()` method:
collab_hist <-
py$
ggplotly
(hist)
And boom!
You get a nice interactive version of your plot! Go ahead and hover…
Your plot lives at this URL (`collab_hist$response$url`) alongside the data. How great is that?!
If you wanted to keep your project private, you would use your own credentials and specify:
py <-
plotly
()
py$
ggplotly
(hist,
kwargs=
list
(filename=
"private_project"
,
world_readable=
FALSE
))
3. Edit your plot online
Now let us click “Fork and edit”. You (and whoever you’ve added as a collaborator) can make edits in the GUI. For instance, you can run a Gaussian fit on this distribution:
You can give a title, edit the legend, add notes, etc.
You can add annotations in a very flexible way, controlling what the arrow and text look like:
When you’re happy with the changes, click “Share” to get your plot’s URL.
If you append a supported extension to the URL, Plotly will translate your plot into that format. Use this to export static images, embed your graph as an iframe, or translate the code between languages. Supported file types include:
https://plot.ly/~mkcor/305.png
https://plot.ly/~mkcor/305.jpeg
https://plot.ly/~mkcor/305.svg
https://plot.ly/~mkcor/305.embed
https://plot.ly/~mkcor/305.json
https://plot.ly/~mkcor/305.r
https://plot.ly/~mkcor/305.py
https://plot.ly/~mkcor/305.m
Isn’t life wonderful?
4. Retrieve your plot.ly plot in R
The JSON file specifies your plot completely (it contains all the data and layout info). You can view it as your plot’s DNA. The R file (https://plot.ly/~mkcor/305.r) is a conversion of this JSON into a nested list in R. So we can interact with it by programming in R!
Access a plot which lives on plot.ly with the well-named method `get_figure()`:
enhanc_hist <-
py$
get_figure
(
"mkcor"
, 305)
Take a look:
str
(enhanc_hist)
# Data for second trace
enhanc_hist$data[[2]]
The second trace is a vertical line at 0.300 named “Good”. Say we get more ambitious and we want to show a vertical line at 0.350 named “Very Good”. We overwrite old values with our new values:
enhanc_hist$data[[2]]$name <-
"VeryGood"
enhanc_hist$data[[2]]$x[[1]] <- 0.35
enhanc_hist$data[[2]]$x[[2]] <- 0.35
Send this new plot back to plot.ly!
enhanc_hist2 <-
py$
plotly
(enhanc_hist$data,
kwargs=
list
(layout=enhanc_hist$layout))
enhanc_hist2$url
Visit the above URL (`enhanc_hist2$url`).
How do you like this workflow? Let us know!
Tutorials are at plot.ly/learn. You can see more examples and documentatation atplot.ly/ggplot2 andplot.ly/r. Our gallery has the following examples:
Acknowledgments
This presentation benefited tremendously from comments by Matt Sundquist and Xavier Saint-Mleux.
Plotly’s R API is part of rOpenSci. It is under active development; you can find it onGitHub. Your thoughts, issues, and pull requests are always welcome!
- Plot with ggplot2, interact, collaborate, and share online
- Collaborate and Connect with Subversion
- Use intent and Uri to interact with your anroid device,.
- When Programmers and Testers Collaborate
- When Programmers and Testers Collaborate
- Interact uploadify3.1 with jsp
- access windows share with smbclient and mount with cifs
- WebGain and HP Collaborate to Bridge Java and .NET
- Online Object Tracking, Learning and Parsing with And-Or Graphs
- Share-nothing and Share-everything.
- Building Online Communities With Drupal, phpBB, and WordPress
- How to Make Money Online with eBay, Yahoo!, and Google
- Line plot for two-way designs using ggplot2
- (转)Using cURL to interact with Google Data services
- how to interact with java object in cpp
- PowerShell: Interact with SharePoint in the fastest way possible
- Use expect command to interact with mysql in linux system
- HOWTO make ns-3 interact with the real world
- 用redis构建访问频率控制模块
- Ubuntu14.04安装AMD显卡驱动双屏显示器完全解决方案
- 联想台式机启天m4350 启用intel vt-x
- hdu.1003 Max Sum
- 串口开放root权限
- Plot with ggplot2, interact, collaborate, and share online
- [编译原理]垃圾收集
- 第四周项目二 分类数的雏形
- 当从后台唤醒activity时,getIntent() 无法获取到最新的值 或者为空
- 内存相关:栈和堆
- 乘积最大
- PHP环境配置
- 安装PHP开发环境及部署PHP项目
- git常用命令