visualization basicswebspace.ship.edu/lebryant/mat219/pdf/slides-09-vis...every geom has a position...

Post on 07-Jul-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Visualization Basics!Part II!

position adjustments

Every geom has a position argument that controls how the object is placed in the plot

movies %>% mutate(fresh = critics_score >= 75) %>% ggplot(aes(mpaa_rating, fill = fresh)) + geom_bar()

Every geom has a position argument that controls how the object is placed in the plot

movies %>% mutate(fresh = critics_score >= 75) %>% ggplot(aes(mpaa_rating, fill = fresh)) + geom_bar(position = “fill”)

Your Turn 8Make the plot below by adjusting the position argument of geom_bar. See the help file (?geom_bar).

movies %>% mutate(fresh = critics_score >= 75) %>% ggplot(aes(mpaa_rating, fill = fresh)) + geom_bar(position = “dodge”)

movies %>% mutate(audience_rded = round(audience_score, -1), critics_rded = round(critics_score, -1)) %>% ggplot(aes(audience_rded, critics_rded)) + geom_point()

Also, geom_point has a useful position adjustment. When points appear on a grid, there might be several points being plotted on top of one another.

movies %>% mutate(audience_rded = round(audience_score, -1), critics_rded = round(critics_score, -1)) %>% ggplot(aes(audience_rded, critics_rded)) + geom_point(position = “jitter”)

Jitter adds a small amount of random change to the position of each point.

movies %>% mutate(audience_rded = round(audience_score, -1), critics_rded = round(critics_score, -1)) %>% ggplot(aes(audience_rded, critics_rded)) + geom_jitter(width = 1, height = 1)

geom_jitter is geom_point with position set to jitter. It makes it easier to control the amount of jittering using width and height

Your Turn 9Add a geom_jitter layer to a boxplot to make the plot below.

ggplot(movies,aes(mpaa_ra1ng,cri1cs_score))+geom_boxplot(outlier.color=“red”)+geom_ji?er(height=0,alpha=0.5,size=0.75)

coordinate systems

ggplot(movies, aes(audience_score, critics_score)) + geom_point()

The default coordinate system is the Cartesian coordinate system, where x and y act independently

ggplot(movies, aes(audience_score, critics_score)) + geom_point() + coord_fixed()

coord_fixed locks x and y with a certain ratio (default is 1).

ggplot(map_data("italy"), aes(long, lat, group = group)) + geom_polygon(fill = "white", color = "black")

Aspect ratio is important for spatial data.

ggplot(map_data("italy"), aes(long, lat, group = group)) + geom_polygon(fill = "white", color = "black") + coord_quickmap()

Aspect ratio is important for spatial data.

ggplot(movies, aes(genre)) + geom_bar()

Flipping coordinates is helpful for bar plots and boxplots.

ggplot(movies, aes(genre)) + geom_bar() + coord_flip()

Flipping coordinates is helpful for bar plots and boxplots.

ggplot(movies, aes(mpaa_rating, fill = mpaa_rating)) + geom_bar(width = 1, show.legend = FALSE)

The other most common coordinate system is polar coordinates

ggplot(movies, aes(mpaa_rating, fill = mpaa_rating)) + geom_bar(width = 1, show.legend = FALSE) + coord_polar()

This leads to a Coxcomb plot

grammar of graphics

ggplot(data = <DATA>) + <GEOM_FUNCTION>( mapping = aes(<MAPPINGS>), stat = <STAT>, position = <POSITION> ) + <COORDINATE_FUNCTION> + <FACET_FUNCTION>

This is not only a template for the most common named plots (scatterplot, bar plot, etc.), but also a template for any plot.

A Grammar of Graphics

top related