Spaces:
Build error

File size: 5,480 Bytes
4b8aeb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be7a6f1
4b8aeb1
 
 
 
 
 
 
 
 
 
 
 
 
be7a6f1
fc7d767
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import streamlit as st
import pandas as pd
import altair as alt
import plotly.express as px

st.set_page_config(page_title="IL Crop Rotation Explorer", layout="wide")

st.title("๐ŸŒพ Illinois Crop Rotation and Land Use Exploration")
st.markdown("Explore dominant crops and land use characteristics across Illinois fields from 2010โ€“2020. This dashboard is for exploration and pattern recognition. Dashboard created by Sara Kiel.")

# Load data
@st.cache_data
def load_data():
    return pd.read_csv("crop_final_df.csv")

df = load_data()

# ---- Sidebar filters ----
crop_options = sorted(df['crop_name'].dropna().unique())
genlu_options = sorted(df['GenLU'].dropna().unique())

selected_crops = st.sidebar.multiselect("Filter by Crop", options=crop_options, default=crop_options[:5])
selected_genlu = st.sidebar.multiselect(
    "Filter by General Land Use",
    options=genlu_options,
    default=genlu_options[:5]
)

filtered = df[df['crop_name'].isin(selected_crops) &df['GenLU'].isin(selected_genlu)]

# ---- Area chart: Crop coverage over time ----
coverage = filtered.groupby(['year', 'crop_name'])['pct_crop'].sum().reset_index()

area_chart = alt.Chart(coverage).mark_area(opacity=0.7).encode(
    x=alt.X('year:O', title='Year'),
    y=alt.Y('pct_crop:Q', title='Total Crop Coverage (%)'),
    color=alt.Color('crop_name:N', title='Crop'),
    tooltip=['year', 'crop_name', 'pct_crop']
).properties(
    width=800,
    height=400,
    title='Crop Coverage Over Time'
)

# ---- Scatter Plot: Crop Coverage vs Rotation Diversity ----
scatter = alt.Chart(filtered).mark_circle(size=60).encode(
    x=alt.X('year:O', title='Year'),
    y=alt.Y('rotation_diversity:Q', title='Rotation Diversity'),
    color=alt.Color('crop_name:N', title='Crop'),
    size=alt.Size('pct_crop:Q', title='% Crop Coverage'),
    tooltip=['crop_name', 'year', 'pct_crop', 'rotation_diversity', 'GenLU']
).properties(
    width=800,
    height=400,
    title='Crop Coverage vs. Rotation Diversity Over Time'
)

# -- Prepare continuous corn data --
df['cc_years'] = df['CCCount'].str.extract(r'(\d+):').astype(float)

# -- Bar chart: Frequency of Continuous Corn Planting --
cc_bar = alt.Chart(df).mark_bar().encode(
    x=alt.X('cc_years:O', title='Years of Continuous Corn (2015โ€“2020)'),
    y=alt.Y('count():Q', title='Number of Fields'),
    tooltip=['cc_years', 'count()']
).properties(
    width=350,
    height=400,
    title='Continuous Corn Frequency'
)

# -- Boxplot: Rotation Diversity by Land Use --
rotation_box = alt.Chart(filtered).mark_boxplot().encode(
    x=alt.X('GenLU:N', title='General Land Use'),
    y=alt.Y('rotation_diversity:Q', title='Rotation Diversity'),
    color='GenLU:N',
    tooltip=['GenLU', 'rotation_diversity']
).properties(
    title='Rotation Diversity by Land Use',
    width=800,
    height=400
)

# ---- Layout ----
st.altair_chart(area_chart, use_container_width=True)
st.altair_chart(scatter, use_container_width=True)
st.altair_chart(cc_bar, use_container_width=True)
st.altair_chart(rotation_box, use_container_width=True)

# ---- Discussion Section ----
st.subheader("๐Ÿงพ How to Use This Dashboard")
st.markdown("""
This dashboard enables exploration of Illinois crop coverage, land use patterns, and crop rotation practices from 2010 to 2020.
Due to the large size of the initial 2 datasets that were combined for this analysis, only a random sample (n=2000) of
each dataset was collected to ensure dashboard performance. The author acknowledges that while this approach preserves
general trends and relationships, some patterns, like rare crop or land use combinations, may not be fully represented in the visualizations.
Finally, this dashboard supports the identification of mono-cropping trends, crop diversity gaps, and sustainability challenges, especially in relation to water conservation.
Use the sidebar to filter by crop types and general land use categories.

* The area chart displays how total crop coverage changes over time.

* The scatter plot relates crop coverage to rotation diversity by year, highlighting diversity trends.

* The bar chart shows how frequently fields were planted with continuous corn, offering insight into back-to-back planting patterns.

* The boxplot compares rotation diversity across different land use types, helping identify where more diverse rotations occur.

Hover over chart elements for detailed values. 
""")

# ---- Dataset Link ----
st.subheader("๐Ÿ”— Dataset Source")
st.markdown("""
The data is sourced from **Data.gov** and was collected by the **USDA**. [Agricultural land use by field: Illinois 2010-2020](https://catalog.data.gov/dataset/agricultural-land-use-by-field-illinois-2010-2020-114f7)
""")

# ---- Contextual Dataset Ideas ----
st.subheader("๐ŸŒ Contextual Datasets")
st.markdown("""
- **Iowa Crop Rotation Data (CDL-based)** โ€“ To compare rotation practices in another high-production state. [USDA NASS CDL](https://www.nass.usda.gov/Research_and_Science/Cropland/Release/index.php)
- **Illinois Soil Types and Drainage Classes** โ€“ To relate land use decisions to soil characteristics. [USDA NRCS Web Soil Survey](https://websoilsurvey.sc.egov.usda.gov/)
""")

# ---- Large File Note ----
st.subheader("๐Ÿ—‚๏ธ Hosting Considerations")
st.markdown("""
This dataset does **not exceed GitHubโ€™s file size limit**, and can be hosted directly within this HuggingFace Space.
Future hosting considerations may need to be considered when including contextual datasets.
""")