Skip to content

Instantly share code, notes, and snippets.

@Ze1598
Last active July 14, 2020 13:58
Show Gist options
  • Save Ze1598/6dc148e4f74c7ddb72c8f80deb58243d to your computer and use it in GitHub Desktop.
Save Ze1598/6dc148e4f74c7ddb72c8f80deb58243d to your computer and use it in GitHub Desktop.
Unpivot data with delimiters (R): count frequencies and plot
data_counts <- unpivot_wide_data %>%
group_by(`Used Social Networks`) %>%
summarize(Frequency = n())
# Order the data by descending order of the frequency
data_counts <- data_counts[
order(data_counts$Frequency, decreasing = TRUE)
,]
# Order the X axis by the current order of the values in the "Frequency" column
fig <- plot_ly(data_counts) %>%
add_bars(
x = ~ `Used Social Networks`,
y = ~ Frequency) %>%
layout(
title = "Users of Social Networks",
xaxis = list(
title = "Social Networks",
categoryorder = "array",
categoryarray = data_counts$Frequency
),
yaxis = list(title = "Users")
)
fig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment