2017-08-03 79 views
1

嗨,我正在使用pairs()函数。如何将散点图矩阵转换为R Plotly

png(file = matrixoutput, width=610, height=360) 
pairs(df.log[smp,],col=mdl.km$cluster[smp],pch=16) 
dev.off() 

它产生下面的图片... enter image description here

我怎么能转换成R plotly?

library(jsonlite) 
library(base64enc) 
library(plotly) 
dataset <- "C:\\Users\\qaise\\Desktop\\markDown\\Twittersmalldata.csv" 
samplesize <- 500 
clusters <-20 
plotlyoutput <- "C:\\Users\\qaise\\Desktop\\markDown\\index.html" 
matrixoutput <- "C:\\Users\\qaise\\Desktop\\markDown\\matric.png" 

set.seed(1776) 

df.oq<-read.csv(dataset) 
df.oq$rratio<-(1+df.oq$followers_count)/(1+df.oq$friends_count) 
names<c("followers_count","friends_count","statuses_count", 
"favourites_count","list_count","rratio") 
df.simp<-df.oq[,names] 
df.simp<-df.simp[complete.cases(df.simp),] 
df.log<-log(df.simp[,c(names)]+1) 
df.log$rratio<-log(df.simp$rratio) 

### 
df.prec<-df.simp 
for (nm in c("followers_count","friends_count","statuses_count","rratio")) { 
F<-ecdf(df.simp[,nm]) 
df.prec[,nm]<-F(df.simp[,nm]) 
} 

### UI NOTE: Allow the user to select clisters from 3 - 20 
### UI NOTE: put the value of that in variable num.clusters 
num.culsters <- clusters 
mdl.km<-kmeans(df.log, num.culsters) 
### UI NOTE: Allow the user to set sample size of values 100, 250, 500, 
1000, 2000 
### UI NOTE: put the value of that in variable sample.size 
sample.size <- samplesize 
sample.size <- min(nrow(df.log),sample.size) 
smp<-sample(nrow(df.log),size=1000) 


### Matrixplot of the data 
png(file = matrixoutput, width=610, height=360) 
pairs(df.log[smp,],col=mdl.km$cluster[smp],pch=16) 
dev.off() 
+0

提供可再现的例子。我在答案 – RUser

+0

中选择了其他数据集参见上面的完整代码! –

+0

可重现的示例需要示例数据。 – RUser

回答

2
require(GGally) 
require(plotly) 
data(tips, package="reshape") 

p <- ggpairs(data=tips, # data.frame with variables 
      columns=1:3, # columns to plot, default to all. 
      title="tips data", # title of the plot 
      colour = "sex") # aesthetics, ggplot2 style 

ggplotly(p) 
+0

对不起,我错了。你说得对。这是官方的方法。 –