如何在R的ggplot中按日落/日出时段筛选数据,以moonlight_fraction为横轴绘制ACI分图
moonlight_fraction as X-axis Got it, let's get this sorted out exactly how you want it—breaking your data into the three time windows, plotting each as a scatter + smooth curve with moonlight_fraction on the X-axis, then combining them into a single figure. Here's a step-by-step implementation:
First, let's make sure we have the right packages loaded—you'll need dplyr for data filtering, ggplot2 for plotting, and ggpubr for combining the plots with ggarrange:
library(dplyr) library(ggplot2) library(ggpubr)
Step 1: Define & Filter Data for Each Time Period
The key fix here is filtering the dataset first based on minsincesunset (instead of restricting the X-axis later). Let's handle each period:
1. Sunset Window (Pre-sunset 105 mins to Post-sunset 120 mins)
This one's straightforward since you gave exact bounds:
data_sunset <- data.thesis %>% filter(minsincesunset >= -105 & minsincesunset <= 120)
2. Late Night Window (Post-sunset 120 mins to Pre-sunrise 120 mins)
For this, you'll need to know the minsincesunset value that corresponds to 120 minutes before sunrise. Let's call this value sunrise_minus_120—replace this with the actual number from your dataset (you can calculate it if you know the total minutes between sunset and sunrise, e.g., if sunset to sunrise is 720 mins, then sunrise_minus_120 = 720 - 120 = 600):
# Replace sunrise_minus_120 with your actual value sunrise_minus_120 <- 600 data_late_night <- data.thesis %>% filter(minsincesunset > 120 & minsincesunset < sunrise_minus_120)
3. Sunrise Window (Pre-sunrise 120 mins to Post-sunrise 120 mins)
Similarly, you'll need the minsincesunset value for sunrise itself (sunrise_mins). The window is 120 mins before to 120 mins after that:
# Replace sunrise_mins with your actual value sunrise_mins <- 720 data_sunrise <- data.thesis %>% filter(minsincesunset >= (sunrise_mins - 120) & minsincesunset <= (sunrise_mins + 120))
Step 2: Create Individual Plots
Now we'll make a plot for each filtered dataset, using moonlight_fraction as the X-axis (your original goal!). We'll keep the same colour=rank_light, scatter points, and smooth curve without standard error:
Sunset Plot
p_sunset <- ggplot(data = data_sunset, aes(x = moonlight_fraction, y = aci, colour = rank_light)) + geom_point(alpha = 0.6) + # Adding alpha helps with overlapping points geom_smooth(se = FALSE) + labs(title = "Sunset Window", x = "Moonlight Fraction", y = "ACI") + theme_minimal()
Late Night Plot
p_late_night <- ggplot(data = data_late_night, aes(x = moonlight_fraction, y = aci, colour = rank_light)) + geom_point(alpha = 0.6) + geom_smooth(se = FALSE) + labs(title = "Late Night Window", x = "Moonlight Fraction", y = "ACI") + theme_minimal()
Sunrise Plot
p_sunrise <- ggplot(data = data_sunrise, aes(x = moonlight_fraction, y = aci, colour = rank_light)) + geom_point(alpha = 0.6) + geom_smooth(se = FALSE) + labs(title = "Sunrise Window", x = "Moonlight Fraction", y = "ACI") + theme_minimal()
Step 3: Combine Plots with ggarrange
Finally, we'll stitch the three plots together into a single figure. You can adjust ncol/nrow to control layout, and add a shared legend if needed:
# Combine plots (adjust ncol/nrow for your preferred layout) combined_plot <- ggarrange(p_sunset, p_late_night, p_sunrise, ncol = 1, nrow = 3, common.legend = TRUE, legend = "bottom") # Add a main title to the combined figure annotate_figure(combined_plot, top = text_grob("ACI vs. Moonlight Fraction by Time Period", face = "bold", size = 14)) # To save the plot: # ggsave("combined_aci_moonlight.png", combined_plot, width = 10, height = 12)
Key Notes:
- The critical mistake you mentioned (using
scale_x_continuous(limits=...)) is fixed by filtering the data first—this ensures only the relevant observations are included in each plot, not just hiding points outside the X-axis range. - Adjust the
sunrise_minus_120andsunrise_minsvalues to match your actual dataset's timing. If you don't have these values, you can calculate them using the total duration between sunset and sunrise in your data. - Tweak
alpha,theme, and layout parameters to match your JMP chart's style exactly.
内容的提问来源于stack exchange,提问作者lanzlanz

