Combined R Markdown Example

This is a simple R Markdown document with code chunks and a plot included.

Use Ctrl+Alt+I to create new code chunk

R Code Chunk with Plot

# Generate some random data
set.seed(123)
x <- rnorm(150)  # changed from 100 to 150

# Summary statistics
summary(x)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -2.30917 -0.63751 -0.05874 -0.02436  0.57694  2.18733

Plot histogram

hist(x, main = "Histogram of Random Data", xlab = "Value")

Loading and Exploring Data

# Load a sample dataset
data(iris)

# View the first few rows of the dataset
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Data visualization

# Scatter plot of iris dataset
plot(iris$Sepal.Length, iris$Sepal.Width, 
     main = "Sepal Length vs. Sepal Width",
     xlab = "Sepal Length", ylab = "Sepal Width", 
     col = iris$Species)
legend("topright", legend = levels(iris$Species), col = 1:3, pch = 1)

Linear Regression

# Fit a linear regression model
lm_model <- lm(Petal.Width ~ Petal.Length, data = iris)

# Summary of the model
summary(lm_model)
## 
## Call:
## lm(formula = Petal.Width ~ Petal.Length, data = iris)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.56515 -0.12358 -0.01898  0.13288  0.64272 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -0.363076   0.039762  -9.131  4.7e-16 ***
## Petal.Length  0.415755   0.009582  43.387  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2065 on 148 degrees of freedom
## Multiple R-squared:  0.9271, Adjusted R-squared:  0.9266 
## F-statistic:  1882 on 1 and 148 DF,  p-value: < 2.2e-16
# Plot the regression line
plot(iris$Petal.Length, iris$Petal.Width, 
     main = "Petal Width vs. Petal Length with Regression Line",
     xlab = "Petal Length", ylab = "Petal Width")
abline(lm_model, col = "red")

Interactive Plot (Using Plotly)

# Load the plotly library
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# Scatter plot using plotly
plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width, color = ~Species,
        type = 'scatter', mode = 'markers', 
        marker = list(size = 10)) %>%
  layout(title = "Interactive Scatter Plot: Petal Width vs. Petal Length",
         xaxis = list(title = "Petal Length"),
         yaxis = list(title = "Petal Width"))

Session Info

sessionInfo()
## R version 4.3.3 (2024-02-29)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Sonoma 14.5
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: Europe/Warsaw
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] plotly_4.10.4 ggplot2_3.5.1
## 
## loaded via a namespace (and not attached):
##  [1] gtable_0.3.5       jsonlite_1.8.8     dplyr_1.1.4        compiler_4.3.3    
##  [5] highr_0.10         tidyselect_1.2.1   tidyr_1.3.1        jquerylib_0.1.4   
##  [9] scales_1.3.0       yaml_2.3.8         fastmap_1.1.1      R6_2.5.1          
## [13] generics_0.1.3     knitr_1.45         htmlwidgets_1.6.4  tibble_3.2.1      
## [17] munsell_0.5.1      RColorBrewer_1.1-3 bslib_0.7.0        pillar_1.9.0      
## [21] rlang_1.1.4        utf8_1.2.4         cachem_1.0.8       xfun_0.49         
## [25] sass_0.4.9         lazyeval_0.2.2     viridisLite_0.4.2  cli_3.6.2         
## [29] withr_3.0.2        magrittr_2.0.3     crosstalk_1.2.1    digest_0.6.35     
## [33] grid_4.3.3         rstudioapi_0.16.0  lifecycle_1.0.4    vctrs_0.6.5       
## [37] evaluate_0.23      glue_1.8.0         data.table_1.15.4  farver_2.1.2      
## [41] fansi_1.0.6        colorspace_2.1-0   rmarkdown_2.29     purrr_1.0.2       
## [45] httr_1.4.7         tools_4.3.3        pkgconfig_2.0.3    htmltools_0.5.8.1