#create a bar plot using ggplot2ggplot(summary_data, aes(x=cyl,y=avg_mpg,fill=cyl ))+geom_bar(stat="identity")+labs(title="Average MPG by cylinder count",x="Number of cylinders", y="Average MPG")+theme_minimal()
# Load the iris datasetdata<- iris# display first few rowshead(data, n=10)
ggplot(data, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+geom_point(size =3, alpha =0.7) +labs(title ="Scatter plot ofsepal Dimensions", x ="Sepal Length",y ="Sepal Width",color ="Species") +theme_minimal() +theme(legend.position ="top")
# Convert time-series data to a dataframedata <-data.frame(Date =seq(as.Date("1949-01-01"), by ="month", length.out =length(AirPassengers)),Passengers =as.numeric(AirPassengers),Year =as.factor(format(seq(as.Date("1949-01-01"), by ="month", length.out =length(AirPassengers)), "%Y")))# Display first few rowshead(data, n=20)
# Function to plot time-series trendplot_time_series <-function(data, x_col, y_col, group_col, title="Air Passenger Trends") {ggplot(data, aes_string(x = x_col, y = y_col, color = group_col, group = group_col)) +geom_line(size =1.2) +# Line graphgeom_point(size =2) +# Add points for claritylabs(title = title,x ="Year",y ="Number of Passengers",color ="Year") +# Legend titletheme_minimal() +theme(legend.position ="top")}# Call the functionplot_time_series(data, "Date", "Passengers", "Year", "Trend of Airline Passengers Over Time")
Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
ℹ Please use tidy evaluation idioms with `aes()`.
ℹ See also `vignette("ggplot2-in-packages")` for more information.
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
# Load datasetdata <- mtcars# Display first few rowshead(data)
# Create a bar graphggplot(data, aes(x = cyl, fill = gear)) +geom_bar(position ="dodge") +# Grouped bar chartlabs(title ="Frequency of Cylinders Grouped by Gear Type",x ="Number of Cylinders",y ="Count",fill ="Gears") +# Legend titletheme_minimal()
p <-ggplot(data = iris,aes(x=Petal.Length,fill=Species))p
p <- p +geom_histogram(aes(y = ..density..),alpha =0.4,position ="identity",bins =30)p
Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(density)` instead.
p <- p +geom_density(aes(colour = Species),size=1.2)p
p<-p+labs(title="Distribution of Petal Length with Group-wise Density curves",x="Petal Length",y="Density")+theme_minimal()p
# Initialize ggplot with data and aesthetic mappingsp <-ggplot(data = iris, aes(x = Species, y = Petal.Width, fill = Species))p
# Add the box plot layerp <- p +geom_boxplot()p
# Add title and labels and use a minimal themep <- p +labs(title ="Box Plot of Petal Width by Species",x ="Species",y ="Petal Width") +theme_minimal()p
#Create a sequence of x values ringing from -2pi to 2pix <-seq(-2*pi, 2*pi, length.out =500)#Evaluate sin(x) and cos(x) over the x rangey1 <-sin(x)y2 <-cos(x)#Combine data into one data framedf <-data.frame(x =rep(x,2),y =c(y1,y2),group =rep(c("sin(x)", "cos(x)"), each =length(x)))df