local({r <- getOption("repos") # You'll want to select the CRAN mirror you like.
r["CRAN"] <- "http://cran.r-project.org"
options(repos=r)
})
install.packages("devtools") # windows users install Rtools
library("devtools")
install_github("ropensci/plotly") # Plotly is part of the super cool rOpenSci
library(plotly) # install libraries we’ll be using
library(ggplot2)
library(reshape)
py <- plotly("ggplot2examples", "3gazttckd7") # initiate plot object
library(WDI) # now we’ll make the plot from the blog post
library(ggplot2)
dat = WDI(indicator='NY.GNP.PCAP.CD', country=c('CL','HU','UY'), start=1960, end=2012) # Grab GNI per
# capita data for Chile, Hungary and Uruguay
wb <- ggplot(dat, aes(year, NY.GNP.PCAP.CD, color=country)) + geom_line()
+ xlab('Year') + ylab('GDI per capita (Atlas Method USD)')
+ labs(title <- "GNI Per Capita ($USD Atlas Method)")
py$ggplotly(wb) # call Plotly
library(IRdisplay)
plotly_iframe <- function(url) {
# set width and height from options or default square
w <- "750"
h <- "600"
html <- paste("
", sep="")
return(html)
}
display_html(plotly_iframe("https://plot.ly/~ggplot2examples/98"))
display_html(plotly_iframe("https://plot.ly/~MattSundquist/1339"))
str(dat) # Once published, you can list all objects and their attributes
py <- plotly("ggplot2examples", "3gazttckd7") # Or you can use Plotly to call any figure
figure <- py$get_figure("MattSundquist", 1339)
str(figure)
figure$data[] # or call the data so you can analyze it or bring data from any plot into your R environment
p <- ggplot(diamonds, aes(x=carat, y=price))
p <- p + geom_point(aes(colour=clarity)) + facet_grid(cut ~ color, margins=TRUE)
py$ggplotly(p)
display_html(plotly_iframe("https://plot.ly/~MattSundquist/1630")) # edited in GUI
require(reshape2)
require(plyr)
require(spdep)
require(igraph)
require(ggplot2)
require(RColorBrewer)
wealth.dist <- structure(
list(
CA = c(0.054, 0.442, 0.349, 0.155), # 2005, family
FR = c(0.040, 0.440, 0.278, 0.242), # 2010, adult
IE = c(0.050, 0.390, 0.330, 0.230), # 2001, household
IT = c(0.115, 0.462, 0.301, 0.122), # 2008, household
GB = c(0.092, 0.465, 0.318, 0.125), # 2008, household
US = c(0.025, 0.265, 0.372, 0.338), # 2007, family
BE = c(0.135, 0.373, 0.300, 0.197)), # 1994, family
.Names = c("CA", "FR", "IE", "IT", "GB", "US", "BE"),
row.names = c("Bottom 50%", "Mid 40%", "Top 9%", "Elite 1%"),
class = "data.frame")
wealth.dist <- wealth.dist[,order(wealth.dist[4,])] # order by share 1%
t(wealth.dist*100) # print table
wealth.dist$EQ <- c(0.5, 0.4, 0.09, 0.01) # add perfectly equal country
# restack data (one rown per combination)
d <- melt(t(wealth.dist))
names(d) <- c("country", "group", "value")
d$group <- factor(d$group, levels = row.names(wealth.dist))
d$country <- factor(d$country, levels = names(wealth.dist))
# plot distributions in stacked barchart
q <- ggplot(d, aes(country, value * 100, fill = group)) +
geom_bar(stat = "identity")
q <- q + scale_fill_brewer(
palette = "RdYlBu",
guide = guide_legend(reverse = TRUE, title = "Groups"))
q <- q + opts(title = "National wealth ownership")
q + scale_x_discrete(name = "Country") +
scale_y_continuous(name = "Percentage of wealth owned")
py$ggplotly()
display_html(plotly_iframe("https://plot.ly/~ggplot2examples/220"))
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
d <- qplot(carat, price, data=dsamp, colour=clarity)
py$ggplotly()
display_html(plotly_iframe("https://plot.ly/~ggplot2examples/42"))
display_html(plotly_iframe("https://plot.ly/~MattSundquist/1511")) # then hide a few series and add a pair of fits.
# CSS styling within IPython notebook
display_html(getURL("https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css"))