library(shiny) library(ggplot2) library(plotly)
planet_sky_colors <- list( Mercury = c(“#1B1B1B”, “#4A4A4A”), Venus = c(“#FFD27F”, “#FFB266”), Earth = c(“#87CEEB”, “#FFA07A”), Mars = c(“#E17055”, “#A04000”), Jupiter = c(“#D9A066”, “#8E5C3B”), Saturn = c(“#C7B299”, “#A68D7C”), Uranus = c(“#B0E0E6”, “#5F9EA0”), Neptune = c(“#4169E1”, “#1E3A5F”), Pluto = c(“#827B60”, “#4F4B3A”) )
planet_rotation_hours <- list( Mercury = 1407.6, Venus = 5832.5, Earth = 24, Mars = 24.6, Jupiter = 9.9, Saturn = 10.7, Uranus = 17.2, Neptune = 16.1, Pluto = 153.3 )
planet_moons <- list( Mercury = 0, Venus = 0, Earth = 1, Mars = 2, Jupiter = min(79, 5), Saturn = min(83, 5), Uranus = min(27, 5), Neptune = min(14, 5), Pluto = min(5, 5) ) moon_color <- “gray”
generate_sky_plot <- function(planets, time) { colors <- planet_sky_colors[[planets]] rotation_period <- planet_rotation_hours[[planets]] normalized_time <- (time / rotation_period) * 24 # Normalize time
# Adjust sky color if (normalized_time < 6) { colors <- c(“#000000”, “#1C1C1C”) # Night } else if (normalized_time < 12) { colors <- c(colors[1], “#FFFF99”) # Morning } else if (normalized_time < 18) { colors <- colors # Afternoon } else if(normalized_time<21){ colors <- c(colors[1], “#FF4500”) # Sunset } else{ colors <- c(“#000000”, “#1C1C1C”) # Night }
# Sun and Moon positions sun_y <- 1 - abs(normalized_time - 12) / 12 moon_count <- min(planet_moons[[planets]], 5) moons <- if (moon_count > 0) { data.frame( x = seq(0.3, 0.7, length.out = moon_count), y = seq(0.2, 0.6, length.out = moon_count), label = paste(planets, “Moon”, 1:moon_count), color = rep(moon_color, moon_count) ) } else { data.frame(x = numeric(0), y = numeric(0), label = character(0), color = character(0)) }
# Stars at night stars <- if (normalized_time < 6 | normalized_time > 18) { data.frame(x = runif(50, 0, 1), y = runif(50, 0.5, 1), label = “Star”) } else { data.frame(x = numeric(0), y = numeric(0), label = character(0)) }
# Create ggplot p <- ggplot() + theme_void() + annotate(“rect”, xmin = 0, xmax = 1, ymin = 0, ymax = 1, fill = colors[1]) + annotate(“rect”, xmin = 0, xmax = 1, ymin = 0.5, ymax = 1, fill = colors[2]) + geom_point(aes(x = 0.5, y = sun_y, text = paste(“Sun at”, round(time, 1), “planetary hours”)), size = 6, color = “yellow”) + geom_point(data = moons, aes(x, y, text = label, color = color), size = 4) + geom_point(data = stars, aes(x, y, text = label), color = “white”, size = 1) + scale_color_identity()
ggplotly(p, tooltip = “text”) # Convert to interactive plot }
ui <- fluidPage( titlePanel(“Planetary Sky Simulator (Hover for Details)”), sidebarLayout( sidebarPanel( selectInput(“planets”, “Select a Planet:”, choices = names(planet_sky_colors)), sliderInput(“time”, “Time of Day:”, min = 0, max = 24, value = 12) ), mainPanel( plotlyOutput(“skyPlot”) ) ) )
server <- function(input, output, session) { observeEvent(input\(planets, { planets <- input\)planets updateSliderInput(session, “time”, min = 0, max = planet_rotation_hours[[planets]], value = planet_rotation_hours[[planets]] / 2) })
output\(skyPlot <- renderPlotly({ generate_sky_plot(input\)planets, input$time) }) }
shinyApp(ui, server)
library(ggplot2)
ggplot(data=planets_tidy2, aes(x=reorder(planets, gravity_in_m_per_sec_sq), y=gravity_in_m_per_sec_sq, fill=planets)) + geom_bar(stat=“identity”) + coord_flip() + # Flip coordinates for better readability labs(title=“Gravity of Planets”, x=“planets”, y=“Gravity (m/s²)”) + theme_minimal() + theme(legend.position=“none”) # Hide legend since each bar is unique
#Scatter Plot: Distance from Sun vs. Temperature
library(ggplot2)
ggplot(planets_tidy2, aes(x=distance_from_sun_in_million_km, y=mean_temp_in_C, label=planets)) + geom_point(aes(color=planets, size=mass_in_yotta_kg), alpha=0.7) + # Points colored by planet, sized by mass geom_text(vjust=-1, size=3) + # Add planet labels slightly above points labs(title=“Distance from Sun vs. Mean Temperature”, x=“Distance from Sun (Million km)”, y=“Mean Temperature (°C)”) + theme_minimal()
library(ggplot2)
ggplot(planets_tidy2, aes(x=orbital_period_in_days, y=orbital_velocity_in_km_per_sec, size=mass_in_yotta_kg, color=planets, label=planets)) + geom_point(alpha=0.7) + # Points with transparency geom_text(vjust=-1, size=3) + # Labels above bubbles scale_size_continuous(range = c(3, 10)) + # Adjust bubble size range labs(title=“Orbital Period vs. Orbital Velocity”, x=“Orbital Period (Days)”, y=“Orbital Velocity (km/s)”, size=“Mass (Yotta-kg)”) + theme_minimal()
library(ggplot2)
ggplot(planets_tidy2, aes(x=reorder(planets, rotation_period_in_hrs), y=rotation_period_in_hrs, fill=planets)) + geom_bar(stat=“identity”) + coord_flip() + # Flip coordinates for better readability labs(title=“Rotation Period of Planets (Ordered)”, x=“planets”, y=“Rotation Period (Hours)”) + theme_minimal() + theme(legend.position=“none”) # Hide legend since each bar is unique
library(ggplot2)
ggplot(planets_tidy2, aes(x=planets, y=gravity_in_m_per_sec_sq, fill=mean_temp_in_C)) + geom_tile(color = “white”) + # White grid lines for clarity scale_fill_gradient(low=“blue”, high=“red”) + # Color scale from cool to hot labs(title=“Heatmap of Surface Temperature by Gravity and Planet”, x=“planets”, y=“Gravity (m/s²)”, fill=“Mean Temp (°C)”) + theme_minimal() + theme(axis.text.x = element_text(angle=45, hjust=1)) # Rotate planet names for readability
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.