I have data frame with 500,000 rows and 16 columns, I have temprature data for all 12 months (500,000) observations for each month(columns), against Latitude and Longitude values( 2 columns). I need to plot animated plot for temperature observations against each month taking long and latitude column. In short want 3d plot (Latitude vs Longitude and Plot temperatures for all 12 months).Please help if you can.
1 Answers
Your Question:
Welcome to SO! please make sure that you read https://stackoverflow.com/help/how-to-ask. I know it's hard when you need to solve a problem and you don't get an answer when needed. This is why I tried to answer your question, although it isn't perfect. However, try to be more specific the next time.
My Try to Solve Your Problem:
As you didn't give us some data we can reproduce and your question isn't very clear (animated and 3D or just 3D?), it's pretty hard to tell what you want to achieve. However, I suppose you wanted to build an interactive 3D plot.
I don't have your data, so what I came up with has to be adjusted so that you can build the plot you want to generate, but it may be easy to go from here. The example below shows you which data format you should have at the beginning and how to transform it into the right format for plotting with plotly
to generate a surface plot. I built a quite detailed example regarding the plotting command itself, so that it becomes easier to adjust the layout as layouting surface plots can be quite confusing from time to time.
Here's what you need:
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")

- 1,092
- 1
- 14
- 27
-
year latitude longitude jan feb mar apr may jun jul aug sep oct nov dec 2010 22.875 70.125 17.8907 19.364 24.776 29.2925 32.1756 31.6154 30.0141 29.5415 29.1655 26.3846 21.868 21.2286 2010 22.875 70.375 18.1399 19.538 24.9371 29.5763 32.368 31.6504 29.9832 29.512 29.2318 26.6148 22.1961 21.466 2010 22.875 70.625 18.3127 19.6462 25.069 29.8204 32.4819 31.5776 29.8456 29.3998 29.233 26.7767 22.4403 21.6326 2010 22.875 70.875 18.4187 19.6924 25.1408 29.9707 32.4877 31.4036 29.6173 29.2125 29.1524 26.8444 22.5904 21.7172 – Asma Nov 19 '18 at 12:29
-
The data appears something like this Ist column is years from 2010-2099, 2nd and 3rd are latitude and longitude and from 4-15 temperatures, for each month. I need 3d animated plot for this. – Asma Nov 19 '18 at 12:30
-
Ok, then you might have a look at: https://www.r-bloggers.com/r-animating-2d-and-3d-plots/ as well as the gganimate package (e.g. https://stackoverflow.com/questions/53153410/animated-line-plot-with-parallel-processing). Hope this helps. – alex_555 Nov 19 '18 at 13:09