R Programming Language Certification Data Visualization & Graphics in R 2 — Questions and Answers
Question 1: Which ggplot2 geom creates a bar chart by counting occurrences of each category?
- geom_bar() (Correct answer)
- geom_col()
- geom_histogram()
- geom_count()
Correct answer: geom_bar()
geom_bar() automatically counts observations per category and plots those counts as bar heights.
Question 2: What argument in geom_bar() or geom_col() maps the fill color to a variable?
- fill inside aes() (Correct answer)
- color= argument
- palette= argument
- hue= argument
Correct answer: fill inside aes()
Mapping fill inside aes(), e.g. aes(fill = variable), assigns bar fill colors based on a data variable.
Question 3: Which function in ggplot2 adds a smooth trend line (e.g., loess or linear) to a plot?
- geom_smooth() (Correct answer)
- geom_line()
- geom_trend()
- stat_fit()
Correct answer: geom_smooth()
geom_smooth() adds a smoothed conditional mean line with optional confidence interval shading.
Question 4: What does coord_flip() do in ggplot2?
- Swaps the x and y axes (Correct answer)
- Flips the color scale
- Reverses data order
- Mirrors the plot
Correct answer: Swaps the x and y axes
coord_flip() exchanges the x and y axes, which is useful for creating horizontal bar charts.
Question 5: Which R package extends ggplot2 to create interactive web-based plots?
- plotly (Correct answer)
- shiny
- ggvis
- htmlwidgets
Correct answer: plotly
plotly's ggplotly() function converts ggplot2 objects into interactive plotly visualizations with hover and zoom.
Question 6: In ggplot2, which scale function controls color mapping for continuous variables?
- scale_color_gradient() (Correct answer)
- scale_color_manual()
- scale_color_discrete()
- scale_fill_brewer()
Correct answer: scale_color_gradient()
scale_color_gradient() defines a two-color gradient scale for mapping continuous numeric values to colors.
Which ggplot2 geom creates a bar chart by counting occurrences of each category?