Module 8 Assignment


Correlation Analysis and ggplot2


 Download the mtcars data set from the R datasets package using the following code:


```R

data(mtcars)

```

 Once you have the data set loaded, you can generate correlation analysis using the `cor()` function to calculate the correlation matrix:


```R

correlation_matrix <- cor(mtcars)

```

Then, you can visualize the correlation matrix using ggplot2 and the `geom_tile()` function to create a heatmap:


```R

library(ggplot2)


# Convert correlation matrix to long format

correlation_long <- reshape2::melt(correlation_matrix)


# Plot correlation heatmap

ggplot(correlation_long, aes(x = Var1, y = Var2, fill = value)) +

  geom_tile() +

  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 

                       midpoint = 0, limit = c(-1,1),

                       name = "Correlation") +

  theme_minimal() +

  labs(title = "Correlation Heatmap of mtcars Variables")

```


After generating the visualization, you can save the plot to a file using the `ggsave()` function:


```R

ggsave("correlation_heatmap.png", plot = last_plot(), width = 8, height = 6, dpi = 300)

```




Comments

Popular posts from this blog

Visual Analytics Final Project

Module # 6 Assignment

Module 7 Assignment