data <-airquality
str(data)
## 'data.frame': 153 obs. of 6 variables:
## $ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
## $ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
## $ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
## $ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
## $ Month : int 5 5 5 5 5 5 5 5 5 5 ...
## $ Day : int 1 2 3 4 5 6 7 8 9 10 ...
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:
The first thing what I did.
library(ggplot2)
library (tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# 샘플 데이터 생성
data <- airquality %>%
mutate(Category = factor(Month), # Make Month a categorical variable
Value = Wind)
# 혼란스러운 그래프 생성
ggplot(data, aes(x = Category, y = Value, color = Category)) +
geom_point(size = 5) +
geom_jitter(width = 0.2, height = 0) +
scale_color_manual(values = rainbow(5)) +
labs(title = "bad graph example",
x = "category",
y = "value") +
theme_minimal() +
theme(
legend.position = "bottom",
plot.title = element_text(size = 20, face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1)
)
library(ggplot2)
# 샘플 데이터 생성
data <- data.frame(
Category = rep(LETTERS[1:5], each = 20),
Value = rnorm(100, mean = 50, sd = 10)
)
# 좋은 그래프 생성
ggplot(data, aes(x = Category, y = Value, color = Category)) +
geom_point(alpha = 0.7, size = 3) +
geom_jitter(width = 0.2, height = 0, alpha = 0.7) +
scale_color_brewer(palette = "Set1") +
labs(title = "good graph example",
x = "category",
y = "value") +
theme_minimal() +
theme(
legend.position = "none",
plot.title = element_text(size = 16, face = "bold"),
axis.text.x = element_text(angle = 0, hjust = 0.5)
)
You can also embed plots, for example: Histogram
library(tidyverse)
library(dplyr)
clean_data <- airquality %>%
filter(!is.na(Wind)) %>% # Remove missing values in Wind
group_by(Month) %>% # Group by month (5 = May, etc.)
summarize(avg_wind = mean(Wind, na.rm = TRUE)) # Average wind
# 막대 그래프 그리기
ggplot(clean_data, aes(x = factor(Month), y = avg_wind, fill = factor(Month))) +
geom_bar(stat = "identity") +
labs(title = "month of average wind",
x = "month",
y = "average of wind") +
theme_minimal() +
theme(legend.position = "none")
Note that the
echo = FALSE parameter was added to the code
chunk to prevent printing of the R code that generated the plot.
#data for the plot
library(ggplot2)
# 히스토그램 생성
ggplot(data = airquality, aes(x = Wind)) +
geom_histogram(binwidth = 2, fill = "skyblue", color = "black") +
labs(title = "wind speed distribution",
x = "wind speed",
y = "frequency") +
theme_minimal()
library(ggplot2)
# 상자 그림 생성
ggplot(data = airquality, aes(x = factor(Month), y = Wind, fill = factor(Month))) +
geom_boxplot() +
labs(title = "wind speed by month",
x = "month",
y = "wind speed") +
theme_minimal() +
theme(legend.position = "none")
library(ggplot2)
# economics 데이터셋을 사용한 선 그래프 생성
ggplot(data = airquality, aes(x = factor(Month), y = Wind)) +
geom_line(color = "blue") +
labs(title = "wind speed by month",
x = "month",
y = "wind") +
theme_minimal()
library(ggplot2)
library(dplyr)
library(dplyr)
library(ggplot2)
# Summarize counts and proportions by Month
data <- airquality %>%
count(Month) %>%
mutate(prop = n / sum(n) * 100,
ypos = cumsum(prop) - 0.5 * prop)
# Pie chart with ggplot2
ggplot(data, aes(x = "", y = prop, fill = factor(Month))) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0) +
labs(title = "observation of each month",
fill = "month") +
theme_void() +
theme(legend.position = "right") +
geom_text(aes(y = ypos, label = paste0(round(prop, 1), "%")), color = "white")
library(ggplot2)
library(ggplot2)
ggplot(data = airquality, aes(x = Wind, y = Ozone)) +
geom_point() +
facet_wrap(~ factor(Month)) +
labs(title = "Every month of wind speed and Ozonoe relationship",
x = "wind speed",
y = "Ozone") +
theme_minimal()
## Warning: Removed 37 rows containing missing values or values outside the scale range
## (`geom_point()`).