-1

I want to plot 3D temperature distribution to demonstrate the trend of temperature according to years and months in the same graph. The x and y axes denote the month and year. The z-axis shows the hourly temperature. How to show multiple distribution to show the trend of temperature.

This is the temperature data. https://1drv.ms/x/s!AndXEcE6b4oxeaSk0sBJrcJuJ0c?e=6DvrFG

Jimin Eom
  • 9
  • 3
  • Could you please share some data using `dput`? – Quinten Jun 12 '22 at 08:46
  • I'm so sorry that I don't know how to share data using dput. I just share the data using dropbox link. https://www.dropbox.com/scl/fi/0q2wr1gm1z2iosn97mgds/Temp2.xls?dl=0&rlkey=twxucimacrlilrmxuzkq50t7o – Jimin Eom Jun 12 '22 at 12:32

1 Answers1

1

If you want a rotatable 3D surface plot, then plotly is your best bet. You will need to first get the monthly average of temperatures and create a matrix from them.

One option for doing this is using the tidyverse to pivot to long format, summarize, then pivot to wide format. Here's how to do that, assuming your data frame as loaded from the csv is called temp

library(tidyverse)
library(plotly)

temp %>% 
  pivot_longer(starts_with('Hour'), 
               names_to = 'Hour',
               values_to = 'Temperature') %>%
  group_by(Year, Month) %>%
  summarise(Temperature = mean(Temperature, na.rm = TRUE)) %>%
  pivot_wider(names_from = Month, values_from = Temperature) %>%
  ungroup() %>%
  select(-1) %>%
  as.matrix() %>%
  plot_ly(x = month.name, y = unique(temp$Year), z = .) %>%
  add_surface()

enter image description here

A nice alternative 2D way to show this kind of data would be with a heatmap:

temp %>% 
  pivot_longer(starts_with('Hour'), 
               names_to = 'Hour',
               values_to = 'Temperature') %>%
  group_by(Year, Month) %>%
  summarise(Temperature = mean(Temperature, na.rm = TRUE)) %>%
  ggplot(aes(Year, Month, fill = Temperature)) +
  geom_tile() +
  scale_fill_viridis_c(option = 1) +
  scale_y_continuous(breaks = 1:12,
                     labels = month.name) +
  coord_equal()

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87