Tips
Here’s a few general tips. In addition, we strongly recommend using from Data to Viz, which guides you through choosing a visualisation, some caveats with various visualisation types, and examples in both Python and R.
For language-specific tips:
- Python: the pandas cheatsheets gives a quick and easy reference for common packages and functions
- R: Posit compiles cheatsheets about different packages, notably for the Tidyverse.
Hotkeys
Find all the Positron shortcuts with “Manage (cog icon) - Keyboard Shortcuts”. Here are some important ones:
| Code | Hotkey | Description |
|---|---|---|
| Ctrl+Enter | Run current command (when in Script) | |
<- |
Alt+- | Assignment operator (in R scripts) |
|> |
Ctrl+Shift+M | Pipe (in R scripts) |
| Esc | Cancel current operation (when in Console) | |
| F1 | Help documentation for selected function |
Interface Customisation
You can make Positron a little nicer and suited to yourself by changing its appearance, colour and using Snippets
Appearance
Manage > Themes > Colour Themes
Choose an application theme that suits your eyes.
Snippets
Snippets allow you to create your own code-driven shortcuts.
For example, if you type shinyapp into a .R file in Positron (with the Shiny extension installed), and then press the Tab key, it will add a Shiny app scaffold to your script.
You can find more Snippets, and add your own custom ones in Manage > Command Palette... > Snippets: Configure snippets... > r.json.
Data manipulation
Import packages to make data manipulation easier
library(dplyr)import pandas as pdImporting and exporting data
Read your data with an I/O (input/output) function.
dataset <- read.csv("data/dataset.csv")df = pd.read_csv("data/dataset.csv")You can also export your data to a CSV file.
write.csv(dataset, "data/output_name.csv")df.to_csv("data/output_name.csv")Initial exploration
You’ll want to explore the data to start with - below are a few functions to get started.
| Function | Example | Description |
|---|---|---|
names() |
names(dataset) |
Returns the variable names |
str() |
str(dataset) |
Returns the structure of the dataset (variable names, types and first entries) |
$ |
dataset$variable |
Returns a specific variable |
unique() |
unique(dataset$variable) |
Returns the unique values of a variable |
summary() |
summary(dataset$variable) |
Returns a statistical summary of a variable |
| Function | Example | Description |
|---|---|---|
df.columns |
Returns the variable names | |
df.info() |
Returns the structure of the dataset (variable names, counts and types) | |
df["variable"] |
Returns a specific column | |
df["variable"].unique() |
Returns the unique values of a variable | |
df.describe() or df["variable"].describe() |
Returns a statistical summary of the dataset or a variable |
Removing missing data
We can remove missing data by filtering the rows out.
dataset <- dataset |>
filter_out(is.na(variable_to_check_for_NAs))Alternatively, the tidyr package (also from the Tidyverse) provides a function to drop rows with missing data:
library(tidyr)
dataset <- dataset |>
drop_na(variable_to_check_for_NAs)See the tidyr website for more functions to deal with missing data.
df = df[df["variable"].notna()]Time series data
If you’ve picked a dataset with time-series data (e.g. a “date” variable), it might have been imported as a string, which is not very useful. You should transform that variable to the correct data type so that you can use it as a continuous variable.
Using a base R function, you can replace the column with the data converted to the correct type. For example, for a date:
dataset$variable <- as.Date(dataset$variable)The Tidyverse can make it easier with the lubridate package. For example, for a column that contains date as strings in the format “2026-12-31”, you could use:
library(lubridate)
dataset <- dataset |>
mutate(variable = ymd(variable))Having the right data type then allows to extract parts of the date (or date-time), to for example create per-year summaries:
library(lubridate)
# extract the year into a new column
dataset <- dataset |>
mutate(year = year(variable))
# use the year for a summary
dataset |>
group_by(year) |>
summarise(summary_variable = mean(variable_to_summarise))See the lubridate website for more date-time parsing and extracting functions.
df["variable"] = pd.to_datetime(df["variable"])Categorical and ordered data
If you’re dealing with categorical data, you can specify this explicitly to keep track of the levels.
dataset$variable <- factor(dataset$variable)df["variable"] = df["variable"].astype("category")To manually specify the order of categories,
Specify the order by sending in an ordered list of the levels joined with c():
dataset$variable <- factor(dataset$variable, levels = c("first_val", "second_val", ... ))Alternatively, if you only need to specify the first (reference) level, use
dataset$variable <- relevel(factor(dataset$variable), ref = "reference_level")Use the df["variable"].cat.reorder_categories() function and use the ordered = True parameter,
df["variable"] = df["variable"].cat.reorder_categories(["cat1", "cat2", ...], ordered = True)If you’re dealing with categorical data, look at the pandas guide for inspiration and help.
This is particularly useful for the Coffee survey dataset.
Renaming variables
Some datasets have cumbersome names for their variables which we can rename.
df <- df |>
rename(new_name = old_name)Use df.rename(), sending a dictionary to the columns = parameter:
df = df.rename(columns = {"old_name": "new_name"})A dictionary is a Python variable with key-value pairs. The structure is key: value, so above we have a dictionary with one key, "old_name" and corresponding value "new_name". They are created as follows:
example_dictionary = {"key1": "value1",
"key2": "value2",
"key3": "value3",
...}
Note that multiple lines are used purely for readability, you could just as well do this on one line.
This is particularly useful for the World population dataset.
Visualisation
We can make simple visualisations of our data.
Use ggplot2’s ggplot() function, with
data =the datasetmapping =the variables, provideed as anaes(...)objectgeom_...the geometries, e.g.geom_line(),geom_point()etc.
library(ggplot2)
ggplot(data = dataset,
mapping = aes(x = ..., y = ..., colour = ..., ...)) +
geom_first_layer() +
geom_second_layer() +
...Take a look at the ggplot2 documentation for more information.
Plotly workaround
If you’re having issues using ggplotly (it’s producing a blank plot), you can use this workaround to view it in your browser.
plot <- ggplotly(saved_ggplot_image)
htmlwidgets::saveWidget(as_widget(plot), "plots/name_of_plot.html")Opening that file will show you the image.
Use seaborn’s relplot(), catplot() and displot() functions. For example,
import seaborn as sns
sns.relplot(data = df, x = "variable_x", y = "variable_y", hue = "variable_colour", ...)We can add additional customisations to our plots, such as axis labels.
Generally, ggplot2 lets you do this with additional elements added to the plot. For example, to add axis labels,
ggplot(data = dataset,
mapping = aes(x = ..., y = ..., colour = ..., ...)) +
geom_first_layer() +
geom_second_layer() +
labs(title = ..., var1 = ...)The simplest way to do this in Python is to use the matplotlib.pyplot module’s functions. Generally, this has the format plt.<some_customisation>. For example, to add axis labels,
import seaborn as sns
import matplotlib.pyplot as plt
sns.relplot(data = df, x = "variable_x", y = "variable_y", hue = "variable_colour", ...)
plt.xlabel("x axis label")
plt.ylabel("y axis label")