Skip to content

Commit

Permalink
"custom plots" tab is now more intuitive and consistent (#242)
Browse files Browse the repository at this point in the history
* custom plots interface more intuitive/logical

* delete comments

* add hist to density plots, listen to bruno
  • Loading branch information
JohnnyDoorn authored Feb 19, 2024
1 parent 5ce8483 commit 12782a2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 59 deletions.
41 changes: 27 additions & 14 deletions R/descriptives.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ DescriptivesInternal <- function(jaspResults, dataset, options) {
"heatmapHorizontalAxis", "heatmapVerticalAxis",
"heatmapDisplayValue", "heatmapTileWidthHeightRatio", "heatmapLegend",
"heatmapStatisticContinuous", "heatmapStatisticDiscrete",
"colorPalette", "splitBy", "variables"
"colorPalette", "splitBy", "variables", "heatmapPlot"
))
jaspResults[["heatmaps"]]$position <- 14
}
Expand Down Expand Up @@ -308,21 +308,21 @@ DescriptivesInternal <- function(jaspResults, dataset, options) {
}
}
}

# Density plots
if (options[["densityPlot"]]) {
if (is.null(jaspResults[["densityPlot"]])) {
jaspResults[["densityPlot"]] <- createJaspContainer(gettext("Density Plots"))
jaspResults[["densityPlot"]]$dependOn(c(
"densityPlot", "densityPlotSeparate",
"densityPlot", "densityPlotSeparate", "densityPlotType", "customHistogramPosition",
"colorPalette", "splitBy", "variables", "densityPlotTransparency"
))
jaspResults[["densityPlot"]]$position <- 17
}

.descriptivesDensityPlots(jaspResults[["densityPlot"]], dataset.factors, variables, options)
}

# Likert plots
if (options[["likertPlot"]] && !all(lapply(dataset.factors[variables], is.double))) {
if (is.null(jaspResults[["likertPlot"]])) {
Expand Down Expand Up @@ -1994,7 +1994,7 @@ DescriptivesInternal <- function(jaspResults, dataset, options) {
axesNames <- c(options[["heatmapHorizontalAxis"]], options[["heatmapVerticalAxis"]])

# we are not ready to plot
if (length(variables) == 0)
if (length(variables) == 0 || isFALSE(options[["heatmapPlot"]]))
return()

axes <- .readDataSetToEnd(columns.as.factor = axesNames)
Expand Down Expand Up @@ -2417,38 +2417,51 @@ DescriptivesInternal <- function(jaspResults, dataset, options) {

.descriptivesDensityPlotsFill <- function(container, data, plotName, axeName, position, options) {
data$split <- NULL
trans <- 1 - (options[["densityPlotTransparency"]] / 100)

if (options[["densityPlotSeparate"]] != "") {

trans <- 1 - (options[["densityPlotTransparency"]] / 100)
p <- ggplot2::ggplot(data, ggplot2::aes(x = variable, fill = separator))
scale_fill <- jaspGraphs::scale_JASPfill_discrete(
palette = options[["colorPalette"]],
name = options[["densityPlotSeparate"]]
)
name = options[["densityPlotSeparate"]])
if (options[["densityPlotType"]] == "histogram" && (options[["customHistogramPosition"]] != "identity")) {
trans <- 1
}

} else {
p <- ggplot2::ggplot(data, ggplot2::aes(x = variable, fill = factor(1)))
scale_fill <- jaspGraphs::scale_JASPfill_discrete(palette = options[["colorPalette"]])
trans <- 1
}

densPlot <- createJaspPlot(title = plotName, width = 480, height = 320, position = position)
if (options[["densityPlotSeparate"]] != "" && any(table(data$separator) == 1)) {
densPlot$setError(gettext("Levels within variable require at least two or more data points!"))
} else {
p <- p + ggplot2::geom_density(alpha = trans)
# p <- p + ggplot2::geom_histogram(position = "identity", alpha = trans)
if (options[["densityPlotType"]] == "density") {
p <- p + ggplot2::geom_density(alpha = trans)
thisYlab = "Density"
} else if (options[["densityPlotType"]] == "histogram") {
p <- p + ggplot2::geom_histogram(position = options[["customHistogramPosition"]], alpha = trans)
thisYlab = "Frequency"
}

# Determine range of axes to generate pretty breaks
yRange <- ggplot2::ggplot_build(p)$layout$panel_scales_y[[1]]$range$range
xRange <- ggplot2::ggplot_build(p)$layout$panel_scales_x[[1]]$range$range
yBreaks <- jaspGraphs::getPrettyAxisBreaks(yRange)
xBreaks <- jaspGraphs::getPrettyAxisBreaks(xRange)

p <- p + ggplot2::scale_y_continuous(name = "Density", breaks = yBreaks, limits = range(yBreaks)) +
p <- p + ggplot2::scale_y_continuous(name = thisYlab, breaks = yBreaks, limits = range(yBreaks)) +
ggplot2::scale_x_continuous(name = axeName, breaks = xBreaks, limits = range(xBreaks)) +
scale_fill +
jaspGraphs::geom_rangeframe() +
jaspGraphs::themeJaspRaw(legend.position = if (options[["densityPlotSeparate"]] != "") "right" else "none")

densPlot$plotObject <- p
}

container[[plotName]] <- densPlot
}
112 changes: 67 additions & 45 deletions inst/qml/Descriptives.qml
Original file line number Diff line number Diff line change
Expand Up @@ -382,84 +382,102 @@ Form
}
}

Group
CheckBox
{

name: "densityPlot"
label: qsTr("Frequency plots")
columns: 2

VariablesForm
{
preferredHeight: 100 * preferencesModel.uiScale

AvailableVariablesList
{
name: "densityPlotVariables"
label: qsTr("Density plots")
source: [{ name: "allVariablesList", discard: ["variables", "splitBy"], use: "type=ordinal|nominal|nominalText"}]
}

AssignedVariablesList
{
name: "densityPlotSeparate"
id: densityPlotSeparate
singleVariable: true
title: qsTr("Separate densities:")
title: qsTr("Separate frequencies:")
suggestedColumns: ["ordinal", "nominal"]
}
}

CheckBox
{
name: "densityPlot"
label: qsTr("Display density plots")

DoubleField
{
name: "densityPlotTransparency"
label: qsTr("Transparency")
fieldWidth: 32
defaultValue: 20
min: 0
max: 100
RadioButtonGroup
{
name: "densityPlotType"
id: densityPlotType
title: qsTr("Type:")
RadioButton { value: "density"; label: qsTr("Density"); checked: true }
RadioButton
{
value: "histogram"
label: qsTr("Histogram")
RadioButtonGroup
{
name: "customHistogramPosition";
id: customHistogramPosition
title: qsTr("How to combine separate frequencies")
RadioButton { value: "stack"; label: qsTr("Stack"); checked: true }
RadioButton { value: "identity"; label: qsTr("Identity") }
RadioButton { value: "dodge"; label: qsTr("Dodge") }
}
}
}
}

VariablesForm
{
preferredHeight: 100 * preferencesModel.uiScale
AvailableVariablesList
{
name: "heatMapVariables"
label: qsTr("Tile heatmaps for selected variables")
source: [{ name: "allVariablesList", discard: ["variables", "splitBy"], use: "type=ordinal|nominal|nominalText"}]
}
AssignedVariablesList
{
name: "heatmapHorizontalAxis"
label: qsTr("Horizontal axis:")
singleVariable: true
}
AssignedVariablesList
DoubleField
{
name: "heatmapVerticalAxis"
label: qsTr("Vertical axis:")
singleVariable: true
name: "densityPlotTransparency"
label: qsTr("Transparency")
fieldWidth: 32
defaultValue: 20
min: 0
max: 100
enabled: densityPlotSeparate.count > 0 && ((densityPlotType.value === "density") || (densityPlotType.value === "histogram" && customHistogramPosition.value === "identity"))
}
}

Group
CheckBox
{
indent: true
CheckBox { name: "heatmapLegend"; label: qsTr("Display legend") }
CheckBox

name: "heatmapPlot"
label: qsTr("Tile heatmaps for selected variables")
columns: 1

VariablesForm
{
name: "heatmapDisplayValue"; label: qsTr("Display value"); childrenOnSameRow: false;
DoubleField { name: "heatmapDisplayValueRelativeTextSize"; label: qsTr("Relative text size"); negativeValues: false; defaultValue: 1 }
preferredHeight: 100 * preferencesModel.uiScale
AvailableVariablesList
{
name: "heatmapVariables"
source: [{ name: "allVariablesList", discard: ["variables", "splitBy"], use: "type=ordinal|nominal|nominalText"}]
}
AssignedVariablesList
{
name: "heatmapHorizontalAxis"
label: qsTr("Horizontal axis:")
singleVariable: true
}
AssignedVariablesList
{
name: "heatmapVerticalAxis"
label: qsTr("Vertical axis:")
singleVariable: true
}
}

DoubleField { name: "heatmapTileWidthHeightRatio"; label: qsTr("Width to height ratio of tiles"); negativeValues: false; defaultValue: 1}

Group
CheckBox
{
columns: 2
title: qsTr("Statistic to plot")
name: "heatmapDisplayValue"
label: qsTr("Display value")
RadioButtonGroup
{
name: "heatmapStatisticContinuous"
Expand All @@ -478,7 +496,11 @@ Form
RadioButton { value: "identity"; label: qsTr("Value itself") }
RadioButton { value: "length"; label: qsTr("Number of observations") }
}
DoubleField { name: "heatmapDisplayValueRelativeTextSize"; label: qsTr("Relative text size"); negativeValues: false; defaultValue: 1 }

}
CheckBox { name: "heatmapLegend"; label: qsTr("Display legend") }

}
}

Expand Down

0 comments on commit 12782a2

Please sign in to comment.