Big_Data_Slip_6

Slip No.6

1) Write an R program to create a simple bar plot of five subject’s marks.

1. Type these code in RScript.

    # Load necessary package
    if (!requireNamespace("ggplot2", quietly = TRUE)) {
    install.packages("ggplot2")
    }

    library(ggplot2)

    # Data for the bar plot
    subjects <- c("Math", "Science", "History", "Geography", "English")
    marks <- c(85, 90, 75, 80, 95)

    # Create a data frame
    data <- data.frame(subjects, marks)

    # Create the bar plot
    ggplot(data, aes(x=subjects, y=marks)) +
    geom_bar(stat="identity", fill="skyblue") +
    theme_minimal() +
    labs(title="Marks of Five Subjects",
    x="Subjects",
    y="Marks") +
    theme(plot.title = element_text(hjust = 0.5))

    # Display the plot
    print(ggplot(data, aes(x=subjects, y=marks)) +
    geom_bar(stat="identity", fill="skyblue") +
    theme_minimal() +
    labs(title="Marks of Five Subjects",
    x="Subjects",
    y="Marks") +
    theme(plot.title = element_text(hjust = 0.5)))



2. To run this program, Ensure you have the ggplot2 package installed. If not, the program will attempt to install it for you. This script will create a bar plot with the marks of five subjects and display it.

3. To install ggplot2 in RStudio, you can follow these steps:

i) Open RStudio.

ii) Open the Console (usually located at the bottom left of the RStudio interface).

4. Type the following command to install ggplot2 and press Enter:
i) install.packages("ggplot2")
Wait for the installation to complete. R will download the package from CRAN (the Comprehensive R Archive Network) and install it.

5. Load the package into your R session by typing:

i) library(ggplot2)
6. Save the Script: Save the script by going to File > Save, or press Ctrl + S (Windows). Name the file something like bar_plot.R.

7. Run the Script:
You can run the script by clicking the Source button at the top of the script editor, or by selecting the entire script and pressing Ctrl + Enter (Windows).

8. View the Plot:
After running the script, check the "Plots" pane, usually located in the bottom right of the RStudio interface. The bar plot will be displayed there.
If the "Plots" pane is not visible, you can open it by going to View > Panes > Plots.

No comments:

Post a Comment