@@ -2,42 +2,51 @@ import type { RExample } from '@/types'
22
33export const examples : RExample [ ] = [
44 {
5- id : 'getting-started' ,
6- title : 'Getting started' ,
7- description : 'Simple scatter plot to get you started with ggplot2' ,
8- code : `# WebR ggplot2 & dplyr Demo
9- # Select an example or write your own code
10-
11- library(ggplot2)
12- library(dplyr)
5+ id : 'car-weight-mpg' ,
6+ title : 'Car weight vs fuel efficiency' ,
7+ description : 'Heavier cars consume more fuel - classic automotive relationship' ,
8+ code : `library(ggplot2)
139
14- # Create your first visualization
10+ # Load built-in R dataset about car characteristics
11+ # Contains data on 32 car models from 1973-74
12+ data(mtcars)
1513ggplot(mtcars, aes(x = wt, y = mpg)) +
1614 geom_point() +
15+ labs(title = "Car Weight vs Fuel Efficiency",
16+ x = "Weight (1000 lbs)",
17+ y = "Miles per Gallon") +
1718 theme_minimal()` ,
1819 } ,
1920 {
20- id : 'basic-plot ' ,
21- title : 'Basic example ' ,
22- description : 'Simple scatter plot with built-in mtcars dataset ' ,
21+ id : 'cylinders-matter ' ,
22+ title : 'Why cylinder count matters ' ,
23+ description : 'Engine cylinders affect both weight and fuel efficiency ' ,
2324 code : `library(ggplot2)
2425
25- # Basic scatter plot
26- ggplot(mtcars, aes(x = wt, y = mpg)) +
27- geom_point() +
28- labs(title = "Car Weight vs MPG",
26+ # Load built-in dataset
27+ # Visualize multiple variables: weight, mpg, and cylinder count
28+ data(mtcars)
29+ ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
30+ geom_point(size = 3, alpha = 0.7) +
31+ geom_smooth(method = "lm", se = FALSE) +
32+ scale_color_manual(values = c("4" = "#E31A1C", "6" = "#1F78B4", "8" = "#33A02C")) +
33+ labs(title = "Car Weight vs MPG by Cylinder Count",
2934 x = "Weight (1000 lbs)",
30- y = "Miles per Gallon") +
31- theme_minimal()` ,
35+ y = "Miles per Gallon",
36+ color = "Cylinders") +
37+ theme_minimal() +
38+ theme(legend.position = "bottom")` ,
3239 } ,
3340 {
34- id : 'dplyr-filter ' ,
35- title : 'Data filtering with dplyr ' ,
36- description : 'Filter and summarize data using dplyr ' ,
41+ id : 'efficient-cars-only ' ,
42+ title : 'Focus on efficient cars ' ,
43+ description : 'What makes fuel-efficient cars special? ' ,
3744 code : `library(dplyr)
3845library(ggplot2)
3946
40- # Filter and summarize data
47+ # Load built-in dataset
48+ # Filter cars with good fuel efficiency and summarize by cylinders
49+ data(mtcars)
4150mtcars_summary <- mtcars %>%
4251 filter(mpg > 20) %>%
4352 group_by(cyl) %>%
@@ -51,37 +60,21 @@ mtcars_summary <- mtcars %>%
5160ggplot(mtcars_summary, aes(x = factor(cyl), y = avg_mpg)) +
5261 geom_col(fill = "steelblue") +
5362 geom_text(aes(label = paste("n =", count)), vjust = -0.5) +
54- labs(title = "Average MPG by Cylinder Count (MPG > 20 )",
63+ labs(title = "Average MPG by Cylinder Count (Efficient Cars Only )",
5564 x = "Number of Cylinders",
5665 y = "Average MPG") +
5766 theme_minimal()` ,
5867 } ,
5968 {
60- id : 'scatter-plot' ,
61- title : 'Enhanced scatter plot' ,
62- description : 'Scatter plot with color mapping and smooth line' ,
63- code : `library(ggplot2)
64-
65- # Enhanced scatter plot with color and smooth line
66- ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
67- geom_point(size = 3, alpha = 0.7) +
68- geom_smooth(method = "lm", se = FALSE) +
69- scale_color_manual(values = c("4" = "#E31A1C", "6" = "#1F78B4", "8" = "#33A02C")) +
70- labs(title = "Car Weight vs MPG by Cylinder Count",
71- x = "Weight (1000 lbs)",
72- y = "Miles per Gallon",
73- color = "Cylinders") +
74- theme_minimal() +
75- theme(legend.position = "bottom")` ,
76- } ,
77- {
78- id : 'bar-chart' ,
79- title : 'Bar chart with dplyr' ,
80- description : 'Create a bar chart after data manipulation' ,
69+ id : 'manual-vs-automatic' ,
70+ title : 'Manual vs automatic transmission' ,
71+ description : 'Do manual transmissions really save fuel?' ,
8172 code : `library(dplyr)
8273library(ggplot2)
8374
84- # Create bar chart with data manipulation
75+ # Load built-in dataset
76+ # Create grouped bar chart showing gears vs transmission type
77+ data(mtcars)
8578gear_summary <- mtcars %>%
8679 group_by(gear, am) %>%
8780 summarize(count = n(), .groups = 'drop') %>%
@@ -90,30 +83,75 @@ gear_summary <- mtcars %>%
9083ggplot(gear_summary, aes(x = factor(gear), y = count, fill = transmission)) +
9184 geom_col(position = "dodge") +
9285 scale_fill_manual(values = c("Automatic" = "#FF7F00", "Manual" = "#1F78B4")) +
93- labs(title = "Number of Cars by Gear and Transmission Type",
86+ labs(title = "Cars by Gear Count and Transmission Type",
9487 x = "Number of Gears",
9588 y = "Count",
9689 fill = "Transmission") +
9790 theme_minimal() +
9891 theme(legend.position = "bottom")` ,
9992 } ,
10093 {
101- id : 'startup-analysis' ,
102- title : 'Startup funding analysis' ,
103- description : 'Analyze startup funding data by sector and region' ,
94+ id : 'metal-bands-happiness' ,
95+ title : 'Metal bands vs happiness' ,
96+ description : 'Surprising correlation: more metal bands = happier countries' ,
97+ csvUrl : '/metal_bands_happiness.csv' ,
98+ code : `library(ggplot2)
99+ library(ggrepel)
100+
101+ # Load CSV data - correlation between metal bands and happiness by country
102+ data <- read.csv("/tmp/metal_bands_happiness.csv", stringsAsFactors = FALSE)
103+
104+ # Create labeled scatter plot with logarithmic scale
105+ ggplot(data, aes(x = Metal.bands.per.capita, y = Score, label = Country.or.region)) +
106+ scale_x_log10() +
107+ stat_smooth(method = "lm", linewidth = 0.5, alpha = 0.2) +
108+ geom_point(color = "red", size = 0.8) +
109+ geom_text_repel(size = 2.5, point.size = 0.5, segment.alpha = 0.5, segment.color = "red") +
110+ xlab("Metal bands per 1M people") +
111+ ylab("Average happiness score") +
112+ labs(title = "Metal Bands per Capita vs Happiness Score",
113+ caption = "Read more: https://p.migdal.pl/blog/2023/01/metal-bands-happiness-correlation") +
114+ theme_minimal()` ,
115+ } ,
116+ {
117+ id : 'global-warming' ,
118+ title : 'Global warming evidence' ,
119+ description : 'Temperature rise over the past decades' ,
120+ csvUrl : '/global_temperature.csv' ,
121+ code : `library(ggplot2)
122+
123+ # Load global temperature time series data
124+ data <- read.csv("/tmp/global_temperature.csv", stringsAsFactors = FALSE)
125+
126+ # Create time series visualization with trend line
127+ ggplot(data, aes(x = year)) +
128+ geom_line(aes(y = temperature_celsius), color = "#e74c3c", size = 1.5) +
129+ geom_point(aes(y = temperature_celsius), color = "#c0392b", size = 2) +
130+ geom_smooth(aes(y = temperature_celsius), method = "loess", se = TRUE,
131+ color = "#e74c3c", alpha = 0.2) +
132+ labs(title = "Global Temperature Trend (2000-2020)",
133+ x = "Year",
134+ y = "Temperature (°C)",
135+ caption = "Data: Global temperature measurements") +
136+ theme_minimal()` ,
137+ } ,
138+ {
139+ id : 'startup-gold-rush' ,
140+ title : 'The startup gold rush' ,
141+ description : 'Which sectors attract the most venture capital?' ,
104142 csvUrl : '/startup_funding.csv' ,
105143 code : `library(dplyr)
106144library(ggplot2)
107145library(scales)
108146
109- # Load the startup funding data
147+ # Load startup funding data
110148data <- read.csv("/tmp/startup_funding.csv", stringsAsFactors = FALSE)
111149
112- # Check the data
150+ # Explore the data structure
113151head(data)
114152
115- # Analyze funding by sector
116- data %>%
153+ # Summarize funding by sector
154+ sector_summary <- data %>%
117155 group_by(sector) %>%
118156 summarise(
119157 total_funding = sum(funding_amount_usd),
@@ -122,90 +160,47 @@ data %>%
122160 ) %>%
123161 arrange(desc(total_funding))
124162
125- # Create visualization
163+ print(sector_summary)
164+
165+ # Create horizontal bar chart with formatted currency
126166ggplot(data, aes(x = reorder(sector, funding_amount_usd),
127167 y = funding_amount_usd, fill = sector)) +
128168 geom_col(show.legend = FALSE) +
129169 coord_flip() +
130170 scale_y_continuous(labels = label_dollar(scale = 1e-6, suffix = "M")) +
131171 labs(title = "Startup Funding by Sector",
132172 x = "Sector",
133- y = "Funding Amount (USD)",
134- caption = "Data: Sample startup funding by sector and valuation") +
173+ y = "Funding Amount (USD)") +
135174 theme_minimal()` ,
136175 } ,
137176 {
138- id : 'csv-example ' ,
139- title : 'Work with uploaded CSV' ,
140- description : 'Generic example for any uploaded CSV file ' ,
177+ id : 'custom- csv-template ' ,
178+ title : 'Your CSV template ' ,
179+ description : 'Starting point for analyzing your own data ' ,
141180 code : `library(dplyr)
142181library(ggplot2)
143182
144- # After uploading your CSV file, load it like this:
183+ # TO USE YOUR OWN DATA:
184+ # 1. Upload your CSV file using the interface above
185+ # 2. Replace the filename below with your actual filename
145186# data <- read.csv("/tmp/your_filename.csv", stringsAsFactors = FALSE)
146187
147- # For now, using built-in iris dataset as example
188+ # For demonstration, load iris - built-in dataset with flower measurements
189+ # iris contains 150 observations of 3 species with 4 measurements each
190+ data(iris)
148191data <- iris
149192
150193# Explore your data structure
151- str(data)
152- head(data)
153- # summary(data) # Uncomment for basic statistics
194+ str(data) # Show data types and structure
195+ head(data) # Show first 6 rows
196+ summary(data) # Show statistical summary
154197
155- # Create a scatter plot
198+ # Example scatter plot - modify based on your columns
156199ggplot(data, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
157200 geom_point(size = 3, alpha = 0.7) +
158- labs(title = "Sepal Dimensions by Species",
159- x = "Sepal Length (cm)",
160- y = "Sepal Width (cm)",
161- color = "Species") +
162- theme_minimal() +
163- theme(legend.position = "bottom")` ,
164- } ,
165- {
166- id : 'metal-bands-happiness' ,
167- title : 'Metal bands vs happiness' ,
168- description : 'Correlation between metal bands per capita and happiness score by country' ,
169- csvUrl : '/metal_bands_happiness.csv' ,
170- code : `library(ggplot2)
171- library(ggrepel)
172-
173- # Load the data from the uploaded CSV file
174- data <- read.csv("/tmp/metal_bands_happiness.csv", stringsAsFactors = FALSE)
175-
176- # Create the visualization
177- ggplot(data, aes(x = Metal.bands.per.capita, y = Score, label = Country.or.region)) +
178- scale_x_log10() +
179- stat_smooth(method = "lm", linewidth = 0.5, alpha = 0.2) +
180- geom_point(color = "red", size = 0.8) +
181- geom_text_repel(size = 2.5, point.size = 0.5, segment.alpha = 0.5, segment.color = "red") +
182- xlab("Metal bands per 1M people") +
183- ylab("Average happiness score") +
184- labs(title = "Metal Bands per Capita vs Happiness Score",
185- caption = "Data sources: Enc. Metallum (2016), after Jakub Marian; World Happiness Report (2022). Chart by Piotr Migdał, p.migdal.pl, CC-BY.") +
201+ labs(title = "Your Data Visualization",
202+ x = "X Variable",
203+ y = "Y Variable") +
186204 theme_minimal()` ,
187205 } ,
188- {
189- id : 'global-temperature' ,
190- title : 'Global temperature trends' ,
191- description : 'Analyze global temperature and CO2 trends over time' ,
192- csvUrl : '/global_temperature.csv' ,
193- code : `library(ggplot2)
194- library(dplyr)
195-
196- # Load the global temperature data
197- data <- read.csv("/tmp/global_temperature.csv", stringsAsFactors = FALSE)
198-
199- # Create temperature trend visualization
200- ggplot(data, aes(x = year)) +
201- geom_line(aes(y = temperature_celsius), color = "#e74c3c", size = 1.5) +
202- geom_point(aes(y = temperature_celsius), color = "#c0392b", size = 2) +
203- geom_smooth(aes(y = temperature_celsius), method = "loess", se = TRUE, color = "#e74c3c", alpha = 0.2) +
204- labs(title = "Global Temperature Trend (2000-2020)",
205- x = "Year",
206- y = "Temperature (°C)",
207- caption = "Data: Global temperature measurements") +
208- theme_minimal() +
209- theme(plot.title = element_text(hjust = 0.5, size = 16, face = "bold"))` ,
210- } ,
211206]
0 commit comments