我有一个具有500,000行和16列的数据框,我有针对每个月(列)的所有12个月(500,000个)观测值的温度数据,而不是纬度和经度值(2列)。我需要绘制对应于每个月的经度和纬度列的温度观测动画图。简而言之,想要3d图(所有12个月的经度,纬度和图温度)。如果可以,请提供帮助。
答案 0 :(得分:3)
欢迎您!请确保您阅读https://stackoverflow.com/help/how-to-ask。我知道当您需要解决问题时很难,而在需要时却没有答案。这就是为什么我试图回答您的问题,尽管它并不完美。但是,下次尝试更加具体。
由于您没有提供给我们一些可以重现的数据,而且您的问题也不清楚(动画和3D还是3D?),很难说出要实现的目标。但是,我想您想构建一个交互式3D图。
我没有您的数据,因此必须对我想出的东西进行调整,以便您可以构建要生成的图,但是从这里开始可能很容易。下面的示例向您展示了开始时应该具有哪种数据格式,以及如何将其转换为正确的格式以使用plotly
进行绘制以生成表面图。我针对绘图命令本身构建了一个非常详细的示例,因此,由于布局曲面图有时会造成混乱,因此调整布局变得更加容易。
这是您需要的:
library(plotly)
library(htmlwidgets)
library(tidyr)
#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# sample data
x <- structure(list(year = c(2008, 2008, 2009, 2009, 2009, 2009, 2009,
2009, 2009, 2009, 2009, 2009, 2009, 2009, 2010, 2010, 2010, 2010,
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2011, 2011, 2011,
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2012, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2013,
2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018), month = structure(c(2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L), .Label = c("January",
"February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"), class = "factor"),
count = c(14, 88, 122, 44, 95, 85, 115, 126, 22, 0, 20, 81,
100, 99, 121, 37, 97, 100, 112, 124, 32, 0, 20, 71, 99, 95,
119, 35, 107, 84, 128, 141, 44, 14, 24, 75, 110, 99, 123,
54, 106, 83, 122, 144, 35, 0, 30, 95, 107, 119, 121, 44,
88, 95, 126, 136, 44, 0, 40, 91, 111, 120, 135, 40, 107,
90, 133, 137, 33, 0, 30, 87, 120, 116, 137, 48, 103, 111,
129, 155, 46, 0, 36, 99, 114, 119, 126, 50, 102, 109, 132,
162, 48, 0, 40, 93, 129, 107, 146, 66, 123, 91, 153, 158,
45, 10, 41, 103, 123, 122, 147, 64, 104, 127, 141, 160, 41,
0, 35, 105)), row.names = c(NA, -120L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = "year", drop = TRUE, indices = list(
0:1, 2:13, 14:25, 26:37, 38:49, 50:61, 62:73, 74:85, 86:97,
98:109, 110:119), group_sizes = c(2L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 10L), biggest_group_size = 12L, labels = structure(list(
year = c(2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
2016, 2017, 2018)), row.names = c(NA, -11L), class = "data.frame", vars = "year", drop = TRUE))
# make sure your data-structure looks like this
x
# year month count
# 1 2008 February 14
# 2 2008 March 88
# 3 2009 April 122
# 4 2009 May 44
# 5 2009 June 95
# 6 2009 July 85
# 7 2009 August 115
# 8 2009 September 126
# 9 2009 October 22
# 10 2009 November 0
# ...
#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# adjust data-structure for plotting
# make sure not to overwrite the original dataframe!
y <- spread(x, year, count)
y <- data.matrix(y[-1])
rownames(y) <- levels(x$month)
# remove NA's
y[is.na(y[,1]),1] <- 0
# plot
plot <- plot_ly(type = 'surface' , z = ~y, colors=c("red", "green")
) %>%
# layout-options
layout(
title = "your plot title",
scene = list(
# format x-axis
xaxis = list(title = "year", range=c(0,length(unique(x$year))-1),
color = "black",
schowticklabels=TRUE,
zeroline = FALSE,
showline = TRUE,
showgrid = FALSE,
autotick = FALSE,
tickmode = 'array',
ticktext = colnames(y),
tickvals = 0:(ncol(y)-1),
linecolor = toRGB("black"),
linewidth = 6,
type="category",
range=c(0,length(unique(x$year))-1)),
# format y-axis
yaxis = list(title = "months", range=c(0,length(levels(x$month))-1),
color = "black",
schowticklabels=TRUE,
zeroline = FALSE,
showline = TRUE,
showgrid = FALSE,
autotick = FALSE,
tickmode = "array",
ticktext = rownames(y),
tickvals = 0:(nrow(y)-1),
linecolor = toRGB("black"),
linewidth = 6,
type="category",
range=c(0:(nrow(y)-1))),
# format z-axis
zaxis = list(title = "count",
color = "black",
schowticklabels=TRUE,
zeroline = FALSE,
showline = TRUE,
showgrid = FALSE,
autotick = FALSE,
tick0 = 0,
dtick = 50,
linecolor = toRGB("black"),
linewidth = 6),
titlefont = list(family="Arial", size=12, color="black"),
tickfont = list(family="Arial", size=12, color="black"),
showgrid = FALSE
)) %>%
# hide legend
hide_colorbar()
# save plot
saveWidget(plot, "test.html")