library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
data<-mtcars
attach(mtcars)
## The following object is masked from package:ggplot2:
##
## mpg
plot<- ggplot(data, aes(x = mpg, y = disp)) + geom_point(color = "violet")
plot<- plot + ggtitle("mtcars Dataset")
plot
plot<-plot + labs(y = "Miles per gallon", x = "Displacement")
plot
data<-mtcars
attach(data)
## The following objects are masked from mtcars:
##
## am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt
## The following object is masked from package:ggplot2:
##
## mpg
install.packages("plotly")
## Installing package into 'C:/Users/krish/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'plotly' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\krish\AppData\Local\Temp\RtmpodDvtu\downloaded_packages
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
## Registered S3 method overwritten by 'data.table':
## method from
## print.data.table
## Registered S3 method overwritten by 'htmlwidgets':
## method from
## print.htmlwidget tools:rstudio
##
## 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
fig = data%>%plot_ly(x=~mpg, y=~disp)%>%layout(title = "Miles per gallon vs Displacement",
xaxis = list(title="Miles per gallon"),
yaxis=list(title = "Displacement"))
fig
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
# Create the scatter plot with hover info
plot_ly(data = mtcars,
x = ~disp,
y = ~mpg,
type = 'scatter',
mode = 'markers',
text = ~paste("Car: ", rownames(mtcars), "<br>",
"Cylinders: ", mtcars$cyl, "<br>",
"Horsepower: ", mtcars$hp)) %>%
layout(title = "MPG vs Displacement",
xaxis = list(title = "Displacement"),
yaxis = list(title = "Miles per Gallon (MPG)"))
#text = ~paste(…): Adds hover text showing the car name, number of cylinders, and horsepower. #mode = ‘markers’: Plots the points as markers.
# Summarize the data for the pie chart
cyl_counts <- table(mtcars$cyl)
# Create the pie chart
plot_ly(
labels = names(cyl_counts),
values = cyl_counts,
type = 'pie'
)
```