My Website

小徐同学参考了谢益辉大神的blog,也尝试记录自己的生活

plotly包--交互可视化

Xwyturbo / 2022-11-15


library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
p=ggplot(iris, aes(x=Sepal.Length,
                   y=Sepal.Width, 
                   color=Species, 
                   shape=Species)) +
  geom_point(size=2, alpha=0.6)
ggplotly(p)
library(plotly)
a=rnorm(100)
b=sample( c(1:10) , 100 , replace=T)
my_graph=plot_ly(x=b , y=a , 
                 mode="markers",
                 size=abs(a)/2 , 
                 color=ifelse(a>0,"blue","red") ) %>%
  layout( hovermode="closest")
my_graph
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
library(plotly)
d <- diamonds[sample(nrow(diamonds), 1000), ]
p <- ggplot(data = d, aes(x = carat, y = price)) +
  geom_point(aes(text = paste("Clarity:", clarity)), size = 4) +
  geom_smooth(aes(colour = cut, fill = cut)) + 
  facet_wrap(~ cut)
## Warning: Ignoring unknown aesthetics: text
p=ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
p
library(plotly)
p<-plot_ly(midwest,x=~percollege,y=~state,color=~state,type="box")
p
library(plotly)
graph=plot_ly(x = rnorm(500), opacity = 0.6, type ="histogram") %>%
  add_trace(x = rnorm(500)+1) %>%
  layout(barmode="overlay")
graph
library(plotly)
var1=seq(1,8)
var2=c(0,1,4,1,8,7,5,4)
var3=c(7,8,4,2,1,2,0,1)
p <- plot_ly(x = var1, y = var2, 
             type="scatter", mode="markers", 
             fill ="tozeroy")
p
#install.packages("webshot")
library(networkD3)
## Warning: package 'networkD3' was built under R version 4.2.2
library(webshot)
## Warning: package 'webshot' was built under R version 4.2.2
set.seed(101)
links=data.frame(source=c("A","A","A","A","A","J",
                          "B","B","C","C","D","I"),
                 target=c("B","B","C","D","J","A",
                          "E","F","G","H","I","I"))
graph=simpleNetwork(links)
saveNetwork(graph,
            file = 'D:/wenyu/Rrojects/ggplotly/network_chart1.html',
            selfcontained = T)
graph
library(networkD3)
data(MisLinks)
data(MisNodes)
forceNetwork(Links = MisLinks, 
             Nodes = MisNodes,
             Source ="source", 
             Target ="target",
             Value ="value", 
             NodeID ="name",
             Group ="group", 
             opacity = 0.8,
             linkDistance = JS('function(){d3.select("body").style("background-color","#DAE3F9"); 
                               return 50;}'))