Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
86 views

Assignment

The document discusses using ggplot2 to create different types of plots in R. It provides examples of mapping categorical and continuous variables to different aesthetics like color, size and shape. It also discusses the purpose of the se argument in geom_smooth() and provides at least 8 examples of different types of graphs that can be created using ggplot2 like scatter plots, histograms, bar charts, stacked bar charts, box plots and area charts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Assignment

The document discusses using ggplot2 to create different types of plots in R. It provides examples of mapping categorical and continuous variables to different aesthetics like color, size and shape. It also discusses the purpose of the se argument in geom_smooth() and provides at least 8 examples of different types of graphs that can be created using ggplot2 like scatter plots, histograms, bar charts, stacked bar charts, box plots and area charts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1. Which variables in mtcarsare categorical? Which variables are continuous?

(Hint: type ?mtcars)

To get the columns of database mtcars


> ?mtcars

To understand the values of the database (mtcars)

Categorical data: gear, cyl, carb


Continuous data: mpg, hp, drat, wt, qsec, disp

2. What geom would you use to draw a line chart? A boxplot? A histogram? An
area chart?

Line chart = geom_point()


Box Plot = geom_boxplot()
Histogram = geom_histogram()
Area Chart = geom_area()

ggplot(data = mtcars) +
geom_line(mapping = aes(x = wt, y = mpg)) +
geom_point(mapping = aes(x = wt, y = mpg))
3. Map a continuous variable to color, size & shape. How do these aesthetics behave
differently for categorical verses continuous variables?

For continuous variables a scale is shown, otherwise the category names.


ggplot(data=mtcars) + geom_point(mapping = aes(x=wt,y=mpg,color=disp,size=
gear,shape=factor(cyl)))

Continuous Variable = mpg, wt


> ggplot(data=mtcars)+geom_point(mapping=aes(x=wt, y=mpg,color=disp))
ggplot(data=mtcars)+geom_point(mapping=aes(x=wt, y=mpg,size=disp))

ggplot(data=mtcars)+geom_point(mapping=aes(x=wt, y=mpg,shape=model))
ggplot(data=mtcars)+geom_point(mapping=aes(x=disp, y=mpg,shape=factor(cyl)
))

Categorical Data:

ggplot(data=mtcars)+geom_point(mapping=aes(x=cyl, y=carb,color=disp))

ggplot(data=mtcars)+geom_point(mapping=aes(x=cyl, y=carb,size=disp))
4. What happen if same variable would be mapped on multiple aesthetics?
Simply both layers are applied.
ggplot(data=mtcars) + geom_point(mapping = aes(x=wt, y=mpg, color=cyl, si
ze=cyl))

5. What will be the outcome of :


ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) +
geom_point() + geom_smooth(se = FALSE)
Didn’t expected that there would be multiple lines. Maybe because grouped by “color =
drv”.
6. What is the purpose of se argument in geom_smooth()?

Shows the confidence interval around the line. (the grey area)

7. Create different types of graphs using ggplot2? (atleast8 )

 Scatter Plot
ggplot(mtcars,aes(mpg,wt)) + geom_point()

 Histogram
ggplot(mtcars , aes(mpg)) +
geom_histogram(binwidth = 2) +
labs(title = "Histogram")
 Bar Chart
ggplot(mtcars,aes(carb)) +
geom_bar(fill="blue") +
theme_bw() +
coord_flip() +
labs(title="Bar Chart") +
theme_classic()
 Stack Bar Chart

ggplot(mtcars,aes(cyl,fill = factor(gear))) +
geom_bar() +
labs(title = "stacked Bar Chart", X="cyl" , y= "gear")

 Box Plot
ggplot(mtcars, aes(factor(cyl),mpg)) +
geom_boxplot(fill="blue") +
labs(title = "Box PLot" , X="MPG" , Y="Wt")
 Area Chart

You might also like