draw plot in r

One way to draw plots in R is using the ggplot2 library. First, make sure you have installed the ggplot2 package by running install.packages("ggplot2"). Then, load the library using library(ggplot2).

Next, create a data frame with the data you want to plot. For example, let's say you have a data frame of student grades called grades_df with columns "name", "exam1", and "exam2". You can create a scatter plot of exam1 scores on the x-axis and exam2 scores on the y-axis using:

main.r
ggplot(grades_df, aes(x = exam1, y = exam2)) +
  geom_point()
62 chars
3 lines

This will create a basic scatter plot of the data.

You can customize the plot with additional layers and aesthetics, such as adding labels, adjusting color and size of points, and adding trend lines. Here is an example of a plot with added labels and a trend line:

main.r
ggplot(grades_df, aes(x = exam1, y = exam2)) +
  geom_point(color = "blue", size = 3) +
  geom_text(aes(label = name), hjust = 0, vjust = 0) +
  geom_smooth(method = "lm")
172 chars
5 lines

This code creates a scatter plot with blue points of size 3, label each point with the student's name, and adds a trend line using linear regression. There are many options and customization features available in ggplot2, allowing you to create highly flexible and professional-looking plots.

gistlibby LogSnag