使用here中的示例代码:
library(plotly)
data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")
data_2007 <- data[which(data$year == 2007),]
data_2007 <- data_2007[order(data_2007$continent, data_2007$country),]
data_2007$size <- data_2007$pop
colors <- c('#4AC6B7', '#1972A4', '#965F8A', '#FF7070', '#C61951')
p <- plot_ly(data_2007, x = ~gdpPercap, y = ~lifeExp, z = ~pop, color = ~continent, size = ~size, colors = colors,
marker = list(symbol = 'circle', sizemode = 'diameter'), sizes = c(5, 150),
text = ~paste('Country:', country, '<br>Life Expectancy:', lifeExp, '<br>GDP:', gdpPercap,
'<br>Pop.:', pop)) %>%
layout(title = 'Life Expectancy v. Per Capita GDP, 2007',
scene = list(xaxis = list(title = 'GDP per capita (2000 dollars)',
gridcolor = 'rgb(255, 255, 255)',
range = c(2.003297660701705, 5.191505530708712),
type = 'log',
zerolinewidth = 1,
ticklen = 5,
gridwidth = 2),
yaxis = list(title = 'Life Expectancy (years)',
gridcolor = 'rgb(255, 255, 255)',
range = c(36.12621671352166, 91.72921793264332),
zerolinewidth = 1,
ticklen = 5,
gridwith = 2),
zaxis = list(title = 'Population',
gridcolor = 'rgb(255, 255, 255)',
type = 'log',
zerolinewidth = 1,
ticklen = 5,
gridwith = 2)),
paper_bgcolor = 'rgb(243, 243, 243)',
plot_bgcolor = 'rgb(243, 243, 243)')
使用此功能可以生成3个不同尺寸的3D散点图。
我有以下数据集:
grid <- structure(list(stock = structure(1:6, .Label = c("stock1", "stock2",
"stock3", "stock4", "stock5", "stock6"), class = "factor"), company = structure(1:6, .Label = c("company1",
"company2", "company3", "company4", "company5", "company6"), class = "factor"),
predirctor = structure(1:6, .Label = c("predictor1", "predictor2",
"predictor3", "predictor4", "predictor5", "predictor6"), class = "factor"),
count = c(6L, 12L, 4L, 9L, 15L, 23L)), .Names = c("stock",
"company", "predirctor", "count"), class = "data.frame", row.names = c(NA,
-6L))
我想使用之前的3D散点图来虚拟化我的数据。三个维度是:股票,公司和预测者。而count列是数字。使用代码:
p <- plot_ly(df, x = df$stock, y = df$company, z = df$predirctor,
marker = list(color = df$count, colorscale = c('#FFE1A1', '#683531'), showscale = TRUE)) %>%
add_markers() %>%
layout(scene = list(xaxis = list(title = 'Weight'),
yaxis = list(title = 'Gross horsepower'),
zaxis = list(title = '1/4 mile time')),
annotations = list(
x = 1.13,
y = 1.05,
text = 'Miles/(US) gallon',
xref = 'paper',
yref = 'paper',
showarrow = FALSE
))
我只采用情节线。我可以理解为什么会发生这种情况,因为此图所需的数据必须与三维不同。只有计数列就像我有一个维度。使用这个图我想当有人看到这可以看到示例stock1-company1-predirctor1有6个。所以不是每行中的数字都有这个标签。 有没有办法融化这些数据才能生成图表?