Skip to content

Commit

Permalink
show_plot now returns ggplot2 objects instead of printing
Browse files Browse the repository at this point in the history
  • Loading branch information
alastair rushworth committed Jul 25, 2019
1 parent 47189b2 commit a8fadcc
Show file tree
Hide file tree
Showing 25 changed files with 1,479 additions and 961 deletions.
1 change: 1 addition & 0 deletions NEWS.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ knitr::opts_chunk$set(echo = TRUE)

<!-- # `inspectdf` 0.0.3.9000 -->

<!-- - `show_plot()` now returns a `ggplot2` object rather than printing the plot - thanks to [Garrick Aden-Buie](https://github.com/gadenbuie) for the [suggestion](https://github.com/alastairrushworth/inspectdf/issues/14). -->
<!-- - Dramatic speed up of `inspect_cat` plotting by avoiding text labels for small regions. -->
<!-- - Added `tech` dataset. -->
<!-- - Fix for text annotation of `inspect_cat()` plots when labels are empty strings. By default `""` will be shown. Thanks to [Michael Swenson](https://github.com/mwswenson) for the [report](https://github.com/alastairrushworth/inspectdf/issues/12) -->
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

<!-- # `inspectdf` 0.0.3.9000 -->

<!-- - `show_plot()` now returns a `ggplot2` object rather than printing the plot - thanks to [Garrick Aden-Buie](https://github.com/gadenbuie) for the [suggestion](https://github.com/alastairrushworth/inspectdf/issues/14). -->

<!-- - Dramatic speed up of `inspect_cat` plotting by avoiding text labels for small regions. -->

<!-- - Added `tech` dataset. -->

<!-- - Fix for text annotation of `inspect_cat()` plots when labels are empty strings. By default `""` will be shown. Thanks to [Michael Swenson](https://github.com/mwswenson) for the [report](https://github.com/alastairrushworth/inspectdf/issues/12) -->
Expand Down
45 changes: 21 additions & 24 deletions R/plot_cat.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ plot_cat <- function(levels_df, df_names, text_labels, high_cardinality,

# add new keys and arrange
lvl_df2 <- lvl_df %>%
# arrange(col_name, prop, value) %>%
mutate(new_level_key = paste0(level_key, "-", dfi))

# # move high cardinality to the end of each column block
Expand Down Expand Up @@ -114,10 +113,8 @@ plot_cat <- function(levels_df, df_names, text_labels, high_cardinality,
colour_vector[lvl_df2$value == min_freq_label] <- cols[3]
# generate plot
plt <- lvl_df2 %>%
ggplot(aes(x = col_name, y = prop, fill = new_level_key,
label = value)) +
geom_bar(position = "stack", stat = "identity",
colour = "black", size = 0.2) +
ggplot(aes(x = col_name, y = prop, fill = new_level_key)) +
geom_bar(stat = "identity", position = "stack", colour = "black", size = 0.2) +
scale_fill_manual(values = colour_vector) +
guides(fill = FALSE) +
coord_flip() +
Expand Down Expand Up @@ -167,23 +164,24 @@ plot_cat <- function(levels_df, df_names, text_labels, high_cardinality,
lvl_df4 <- lvl_df3 %>% sum_small_cats(prop_thresh = label_thresh)

plt <- plt +
suppressWarnings(
ggfittext::geom_fit_text(
data = lvl_df4,
aes(x = col_name,
y = prop,
label = value,
fill = new_level_key,
colour = col_vec),
inherit.aes = FALSE,
na.rm = TRUE,
position = "stack",
place = "middle",
grow = FALSE,
outside = FALSE,
show.legend = FALSE
)) +
scale_colour_manual(values = c("white", "gray55"))
suppressWarnings(ggfittext::geom_fit_text(
data = lvl_df4,
aes(x = col_name,
y = prop,
label = value,
fill = new_level_key,
colour = col_vec,
ymin = 0,
ymax = prop),
inherit.aes = FALSE,
na.rm = TRUE,
position = "stack",
place = "middle",
grow = FALSE,
outside = FALSE,
show.legend = FALSE
)) +
scale_colour_manual(values = c("white", "gray55"))
}

# if this is a comparison, then add x-axis labels and descriptive title
Expand All @@ -200,7 +198,6 @@ plot_cat <- function(levels_df, df_names, text_labels, high_cardinality,
}
# add title
plt <- plt + labs(title = ttl)

suppressWarnings(print(plt))
plt
}

11 changes: 5 additions & 6 deletions R/plot_cor.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ plot_cor_single <- function(out, alpha, df_names, text_labels, col_palette, meth
coord_flip() + ylim(min(out$lower), max(out$upper)) +
labs(x = "", y = xlab, title = paste0("Correlation of columns in df::",
df_names$df1))
# print plot
print(plt)
plt
}


Expand Down Expand Up @@ -100,8 +99,7 @@ plot_cor_pair <- function(out, alpha, df_names, text_labels, col_palette, method
title = paste0("Comparison of \u03C1 between df::", df_names$df1,
" and ", df_names$df2)) +
scale_fill_manual(name = "Data frame", values = bcols)
# print plot
print(plt)
plt
}

plot_cor_grouped <- function(out, df_names, text_labels, col_palette, method,
Expand All @@ -121,12 +119,13 @@ plot_cor_grouped <- function(out, df_names, text_labels, col_palette, method,
series = "pair", group = group_name,
plot_type = plot_type,
col_palette = col_palette,
text_labels = text_labels)
text_labels = text_labels,
ylab = "Correlation")
plt <- plt +
geom_hline(yintercept = 0, alpha = 0.5, linetype = "dashed") +
geom_hline(yintercept = 1, alpha = 0.3, linetype = "dashed") +
geom_hline(yintercept = -1, alpha = 0.3, linetype = "dashed")
print(plt)
plt
}


Expand Down
16 changes: 8 additions & 8 deletions R/plot_grouped.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plot_grouped <- function(df, value, series, group, plot_type,
col_palette, text_labels){
col_palette, text_labels, ylab){
if(plot_type == "line"){
n_df <- length(unlist(unique(df[series])))
vcols <- c("gray50", user_colours(9, col_palette)[9])
Expand All @@ -15,7 +15,7 @@ plot_grouped <- function(df, value, series, group, plot_type,
geom_line(size = 1.5, alpha = 0.65) +
geom_point(size = 2) +
scale_colour_manual(name = "Pair", values = bcols) +
labs(y = "Correlation", x = group)
labs(y = ylab, x = group)
}
if(plot_type == "bar"){
n_df <- length(unlist(unique(df[group])))
Expand All @@ -31,16 +31,16 @@ plot_grouped <- function(df, value, series, group, plot_type,
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = bcols) +
guides(fill = FALSE) +
labs(y = "Correlation", x = "")
labs(y = ylab, x = "")
if(text_labels){
plt <- plt + geom_bar_text(position = 'dodge',
plt <- plt + geom_bar_text(position = 'dodge',
color = "white",
stat = 'identity',
angle = 90,
# grow = TRUE,
# reflow = TRUE,
place = "top",
min.size = 1)
grow = TRUE,
reflow = TRUE,
place = "top",
min.size = 3)
}
}
return(plt)
Expand Down
4 changes: 2 additions & 2 deletions R/plot_imb.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ plot_imb_1 <- function(df_plot, df_names, text_labels, col_palette){
parse = TRUE)
}
# return plot
print(plt)
plt
}

plot_imb_2 <- function(df_plot, df_names, alpha, text_labels, col_palette){
Expand Down Expand Up @@ -79,5 +79,5 @@ plot_imb_2 <- function(df_plot, df_names, alpha, text_labels, col_palette){
suppressWarnings()

# return the plot
print(plt)
plt
}
4 changes: 2 additions & 2 deletions R/plot_mem.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ plot_mem_1 <- function(df_plot, df_names, sizes, text_labels, col_palette){
plt = plt, thresh = 0.2)
}
# print plot
print(plt)
plt
}


Expand Down Expand Up @@ -85,5 +85,5 @@ plot_mem_2 <- function(df_plot, df_names, sizes, text_labels, col_palette){
dodged = 1, fill = z_tall$df_input)
}

print(plt)
plt
}
9 changes: 5 additions & 4 deletions R/plot_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ plot_na_single <- function(df_plot, df_names, text_labels, col_palette){
z = df_plot$cnt,
plt = plt)
}
print(plt)
plt
}

plot_na_pair <- function(df_plot, df_names, alpha, text_labels, col_palette){
Expand Down Expand Up @@ -57,7 +57,7 @@ plot_na_pair <- function(df_plot, df_names, alpha, text_labels, col_palette){
labs(y = "% of column that is NA", x = "") +
guides(color = guide_legend(override.aes = list(fill = NA)))

print(plt)
plt
}

plot_na_grouped <- function(df_plot, df_names, text_labels, col_palette, plot_type){
Expand All @@ -67,8 +67,9 @@ plot_na_grouped <- function(df_plot, df_names, text_labels, col_palette, plot_ty
series = "col_name", group = group_name,
plot_type = plot_type,
col_palette = col_palette,
text_labels = text_labels)
print(plt)
text_labels = text_labels,
ylab = "% missing")
plt
}


4 changes: 2 additions & 2 deletions R/plot_num.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ plot_num_1 <- function(df_plot, df_names, plot_layout, text_labels){
nrow = plot_layout[[1]],
ncol = plot_layout[[2]])
# print plot
print(plt)
plt
}


Expand Down Expand Up @@ -120,5 +120,5 @@ plot_num_2 <- function(df_plot, df_names, plot_layout, text_labels, alpha){
facet_wrap(~ cname, scales = "free",
nrow = plot_layout[[1]],
ncol = plot_layout[[2]])
print(plt)
plt
}
5 changes: 2 additions & 3 deletions R/plot_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ plot_types_1 <- function(df_plot, df_names, text_labels, col_palette){
z = df_plot$cnt,
plt = plt, thresh = 0.1)
}

# print plot
print(plt)
plt
}

plot_types_2 <- function(df_plot, df_names, text_labels, col_palette){
Expand Down Expand Up @@ -82,5 +81,5 @@ plot_types_2 <- function(df_plot, df_names, text_labels, col_palette){
values = user_colours(3, col_palette)[c(1, 3)])

# return plot
print(plt)
plt
}
Loading

0 comments on commit a8fadcc

Please sign in to comment.