
nyhackr.org
plotnineSt. Louis FED
“The representation of numbers, as physically measured on the surface of the graphic itself, should be directly proportional to the numerical quantities represented.” (1983, p.56) Edward Tufte, The Visual Display of Quantitative Information

Wrong

Or make them line charts:
Right


nycflights |> mutate(month = as.factor(month)) |> ggplot(aes(x=month)) +
geom_bar() +
theme_bw(base_size = 36) +
labs(title = "NYC Flights by Month") +
coord_cartesian(ylim = c(0, 3000))
nycflights |> group_by(month) |>
summarise(count = n()) |> ggplot(aes(x=month,y=count)) +
geom_line() +
theme_bw(base_size = 36) +
labs(title = "NYC Flights by Month") +
scale_x_continuous(breaks = seq(1,12,by=1))Wrong

Or transform data to represent a net change:
Right


sp500_1950_2018 |> mutate(Date = as.Date(Date)) |>
filter(Date > as.Date("2015-01-01") ) |> ggplot(aes(x=Date,y=Close)) +
geom_area(fill = "blue",alpha = 0.5) +
geom_line() +
theme_bw(base_size = 24) +
labs(title = "S&P 500 Closing Price") +
coord_cartesian(ylim = c(0, 3000))
Initial_Date = sp500_1950_2018 |> filter(Date == "2015-01-02")
Initial_Open = Initial_Date$Open[1]
sp500_1950_2018 |> mutate(Date = as.Date(Date)) |>
filter(Date > as.Date("2015-01-03") ) |>
mutate(Price_Change = Close - Initial_Open) |> ggplot(aes(x=Date,y=Price_Change)) +Graphical Excellence is that which gives to the viewer the greatest number of ideas in the shortest time with the least ink in the smallest space (Tufte)
male_Aus |> ggplot(aes(x=ht,y=pcBfat,color=sport,fill=sport,shape=sport)) +
geom_point(size=2.5) +
theme_excel(base_size = 24) +
theme(
panel.grid.major = element_line(color = "black", size = 1),
panel.grid.minor = element_line(color = "black", size = 0.5),
panel.border = element_rect(color = "black", size = 3)) +
labs(title = "Height and Bodyfat of Australian Athletes") +
xlab("Height") +
ylab("Bodyfat")theme_excel lots of poor decisions therepanel.grid.majorpanel.grid.minorpanel.bordermale_Aus |> ggplot(aes(x=ht,y=pcBfat,color=sport,fill=sport,shape=sport)) +
geom_point(size=2.5,alpha=0.7,stroke=1) +
theme_bw(base_size = 24) +
scale_shape_manual(values = 21:25) +
scale_color_colorblind() +
scale_fill_colorblind() +
labs(title = "Height and Bodyfat of Australian Athletes") +
xlab("height (cm)") +
ylab("%bodyfat")male_Aus |> ggplot(aes(x=ht,y=pcBfat,color=sport,fill=sport,shape=sport)) +
geom_point(size=2.5,alpha=0.7,stroke=1.) +
theme_minimal(base_size = 24) +
scale_shape_manual(values = 21:25) +
scale_color_colorblind() +
scale_fill_colorblind() +
labs(title = "Height and Bodyfat of Australian Athletes") +
xlab("height (cm)") +
ylab("%bodyfat")male_Aus |> ggplot(aes(x=ht,y=pcBfat,color=sport,fill=sport,shape=sport)) +
geom_point(size=2.5,alpha=0.7,stroke=1.) +
theme_tufte(base_size = 24) +
scale_shape_manual(values = 21:25) +
scale_color_colorblind() +
scale_fill_colorblind() +
labs(title = "Height and Bodyfat of Australian Athletes") +
xlab("height (cm)") +
ylab("%bodyfat")Indmar 2007
data("Titanic")
titanic = as_tibble(Titanic)
titanic |>
filter(Class != "Crew") |>
mutate(Survived =
if_else(Survived == "No", "died","survived")) |>
ggplot(aes(Sex,n,fill = Sex)) +
geom_col() +
facet_grid(Class ~ Survived,scales = "free_x") +
scale_x_discrete(name = NULL) +
scale_y_continuous(limits = c(0, 195), expand = c(0, 0)) +
scale_fill_manual(values = c("#D55E00D0", "#0072B2D0"), guide = "none") +
theme_tufte(base_size = 24)p +
theme_gray(base_size = 24) +
theme(axis.line = element_blank(),
#axis.line.y = element_line(color = colors[2]),
axis.ticks = element_blank(),
axis.ticks.length = grid::unit(0, "pt"),
#axis.ticks.y = element_line(color = colors[2]),
axis.text.x = element_text(margin = margin(7, 0, 3.5, 0)),
strip.text = element_text(margin = margin(3.5, 3.5, 3.5, 3.5)),
strip.background = element_blank(),
panel.background = element_rect(fill = "#E6E6E3",
linewidth =0),
panel.grid.major.x = element_blank(),
panel.grid.minor.y = element_line(color="white",linewidth = 0.3),
panel.grid.major.y = element_blank(),
panel.grid.minor.x = element_blank())tech_stocks |>
ggplot(aes(x=date,y=return,group=Company,color=Company)) + geom_line(size=0.66,na.rm=TRUE) +
geom_hline(yintercept = 1, size = 1.0, color="grey70") +
theme_gray(base_size = 24) +
scale_color_manual(
values = c( "#009E73","#E69F00","#000000", "#56B4E"),
name = NULL,
breaks = c("NVDA", "META", "AAPL", "MSFT"),
labels = c("NVidia", "Meta", "Apple", "Microsoft")) +
theme(
strip.background = element_blank(),
panel.background = element_rect(fill = "#E6E6E3",
linewidth =0),
panel.grid.minor.y = element_line(color="white",linewidth = 0.5),
panel.grid.major.y = element_line(color="white",linewidth = 0.5),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
)tech_stocks = tech_stocks |> group_by(Company) |> mutate(daily_return = return/lag(return))
tech_stocks |> select(Company,date,daily_return) |> pivot_wider(names_from=Company,values_from = daily_return) |>
ggplot(aes(x=MSFT,y=AAPL)) +
geom_point(alpha = 0.5,fill = "#0072B2D0",color="#0072B2D0",size=2) +
stat_smooth(method="lm",color="gray",se = FALSE,linewidth=2) +
theme_bw(base_size = 24) +
xlab("MSFT Return") +
ylab("AAPL Return") +
theme(
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
)ggplot2 based advice:
theme_x(base_size = ?)geom_point(size = ?)geom_line(linewidth=?)panel.grid.major.y = element_rect(linewidth = ?)axis.text.x = element_text(size=?)
DATA 608