How to re-organize the charts in R Shiny? - r

I am using R Shiny to visualize data as some pie charts using Plotly package. The problem is that I would like to re-size the pie charts and navigate them horizontally not vertically, picture is shown below.
I will appreciate for helps experts!
Here is the code
#UI part
dashboardBody(
fluidRow(
plotlyOutput("PieDistribution"),
plotlyOutput("PieHealth")
)
#server part
output$PieDistribution <- renderPlotly({
plot_ly(PD, labels = ~PD$VP, values = ~PD$V1, type = 'pie',
textposition = 'inside',
textinfo = 'percent',
insidetextfont = list(color = '#FFFFFF'),
hoverinfo = 'text',
text = ~paste(PD$VP),
marker = list(colors = colors,
line = list(color = '#FFFFFF', width = 1))) %>%
layout(title = 'Project Distribution by Vice President',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
showlegend = FALSE,
legend = list(x = 50, y = 0.5))
})
output$PieHealth <- renderPlotly({
plot_ly(PD2, labels = ~PD2$HealthName, values = ~PD2$V1, type = 'pie',
textposition = 'inside',
textinfo = 'percent',
insidetextfont = list(color = '#FFFFFF'),
hoverinfo = 'text',
text = ~paste(PD2$HealthName),
marker = list(colors = c('#229954', '#d32f2f','#ffc107'),
line = list(color = '#FFFFFF', width = 1))) %>%
layout(title = "Project Health",
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
showlegend = FALSE)
})
Image of the pie charts

Create new columns within fluidrow in your ui. Something like:
fluidRow(column(6,
plotlyOutput("PieDistribution")),
column(6,
plotlyOutput("PieHealth"))
)

Related

Trouble with creating bar & pie subplot with R plotly

I've created a Plotly bar and pie chart and want to combine them to form one chart.
When I use subplot() to combine these Plotly charts, the pie & bar charts overlap.
Any advice on how to present these plots so that each is in its own row? Thank you.
Here's a picture of what I'm currently experiencing:
Reprex below:
#Pie chart example
pie_tibble <- tibble(donuts = c(49050, 66924),
group = c("Group A", "Group B"))
pie <- plot_ly(data = pie_tibble, labels = ~group, values = ~donuts, type = 'pie',
showlegend = F,
hoverinfo = "none",
marker = ~list(colors = c('#404040', '#24608B'))) %>%
layout(xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
bar_tibble <- tibble(category = c("Cat 1", "Cat 1", "Cat 2", "Cat 2"),
pct_increase = c(0.17, 0.25, 0.64, 0.85),
week = c(1, 2, 1, 2))
#Bar chart example
bar <- plot_ly(data = bar_tibble, hoverinfo = "none") %>%
layout(
barmode = 'stack',
showlegend = F) %>%
add_trace(
x = ~pct_increase,
y = ~category,
type = "bar",
transforms = list(
list(
type = "aggregate",
groups = ~category,
aggregations = list(
list(
target = "x", func = "avg", enabled = T)))))
#Combine charts
subplot(bar, pie, nrows = 2)
Pie charts and plotly::subplot() are notoriously challenging - though you can get around many of the issues by specifying the domain manually. Below I have changed the pie code by specifying the domain = list(...) as so:
pie <- plot_ly(data = pie_tibble, labels = ~group, values = ~donuts, type = 'pie',
# Specify the domain here
domain = list(x = c(0.5, 0.5), # centered on x axis
y = c(0.0, 0.4)),
showlegend = F,
hoverinfo = "none",
marker = ~list(colors = c('#404040', '#24608B'))) %>%
layout(xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
subplot(bar, pie, nrows = 2) now gives:
Sorry I don't have a more elegant answer, but hoping someone else might!

How to horizontally center a pie chart with plotly in R?

I'm new to plotly, trying to figure out how to get a piechart to be aligned at the center of the entire plot area.
library(dplyr)
library(plotly)
data_for_plot <-
mtcars %>%
count(cyl)
> data_for_plot
## cyl n
## 1 4 11
## 2 6 7
## 3 8 14
plot_ly(data_for_plot, labels = ~cyl, values = ~n, type = 'pie', hole = 0.05 ,textposition = 'outside',textinfo = 'percent') %>%
layout(title = list(text = "my nice title is here", xanchor = "center"),
showlegend = F,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE)) %>%
layout(paper_bgcolor = "pink")
So I get this pie chart, which is not centered:
I assume that inside layout() I need something that refers to the piechart itself, to assign it with xanchor = "center". But I researched this and couldn't find an answer.
Update on my attempts - 2020-01-18
I've tested the majority of attributes in layout() and still couldn't find something that would work with xanchor = "center". I've alse examined plotly's reference guide but so far to no avail.
Seems that the solution involves setting up the margin attribute within layout(). The solution is based on this hint, referring to this post. Implementing this to R is done using the following code:
plot_ly(data_for_plot, labels = ~cyl, values = ~n, type = 'pie', hole = 0.05 ,textposition = 'outside',textinfo = 'percent') %>%
layout(title = list(text = "my nice title is here", xanchor = "center"),
showlegend = F,
margin = list(l = 20, r = 20),
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE)) %>%
layout(paper_bgcolor = "pink")

How to hard code the color for the displayed variable in plotly pie chart?

Do we have something similar to our ggplot where
we define the color panel for each variable ?
scale_manual <- function(...){
ggplot2::manual_scale(
"fill",
values = setnames(c("green","red","blue","yellow","grey"),
c("var1","var2","var3","var4","var5")),
...
)
}
Although this Q seems to answer,
How can I change the colors of the slices in pie charts in plotly for r using hexadecimal strings?
but it is not working here.
Please consider an reprex below:
library(plotly)
USPersonalExpenditure <- data.frame("Categorie"=rownames(USPersonalExpenditure), USPersonalExpenditure)
data <- USPersonalExpenditure[,c('Categorie', 'X1960')]
p <- plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie') %>%
layout(title = 'United States Personal Expenditures by Categories in 1960',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
#trial 1
plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie', marker = list(color = rainbow(5))) %>%
layout(title = 'United States Personal Expenditures by Categories in 1960',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
# trial 2
plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie', marker = list(color = brewer_pal(5, "Set3"))) %>%
layout(title = 'United States Personal Expenditures by Categories in 1960',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
Since there are many plots using the same data,
color need to be consistent.
So, trying to hard code for each variable.
I usually use Color mapping functions from the leaflet package before making interactive plots for shiny apps and plotly charts:
Hard code the color variable using your desired palette.
data$color <- leaflet::colorFactor(
palette = "Dark2", domain = data$Categorie
)(data$Categorie)
plot_ly(
data, labels = ~Categorie, values = ~X1960, type = 'pie',
marker = list( colors = ~color)
) %>%
layout(
title = 'United States Personal Expenditures by Categories in 1960',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
)
You may also hardcode it manually:
colors_list <- list(
"Food and Tobacco" = "#1B9E77",
"Household Operation" = "#D95F02",
"Medical and Health" = "#7570B3",
"Personal Care" = "#E7298A",
"Private Education" = "#66A61E"
)
data$color <- dplyr::recode(data$Categorie, !!!colors_list)
plot_ly(
data, labels = ~Categorie, values = ~X1960, type = 'pie',
marker = list( colors = ~color)
) %>%
layout(
title = 'United States Personal Expenditures by Categories in 1960',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
)

Show specific plane (XY) in plotly 3D Scatterplot in R

With showgrid = TRUE for x-axis and y-axis, is there any way to get rid of the VERTICAL lines extending from the x and y-axis so that there is a single plane showing?
library(plotly)
itemName = c("Alpha", "Bravo", "Charley", "Delta", "Echo", "Foxtrot")
security = c(0.8583241, 0.7516891, 0.5390520, 0.4569594, 0.3228843, 0.1722286)
miniX = c(-885.108, -1030.000, -805.889, -695.755, -887.871, -640.986)
miniY = c(-445.135, -298.563, -797.709, -806.598, -963.091, -1010)
miniZ = c(423.694, 417.075, 616.456, 571.223, 575.304, 412.72)
df = data.frame(itemName, security, miniX, miniY, miniZ)
# set a range of colors
colors <- c('#f42f28', '#f2e30e', '#2d9b37', '#31a6f3')
######### set up 3d scatter plot #########
p1 <- plot_ly(df,
x = miniX,
y = miniY,
z = miniZ,
mode = "markers",
size = security,
color = security,
colors = colors,
marker = list(symbol = 'circle', sizemode = 'diameter'), sizes = c(10, 20),
text = paste('Item:', itemName, '<br>Security:', security),
hoverlabel = list(bgcolor = '#ffffff', bordercolor = 'ffffff', font = list(color = '#000000')),
hoverinfo = "text"
) %>%
add_markers() %>%
layout(title = 'Locales',
scene = list(xaxis = list(title = '', autorange = TRUE, showgrid = TRUE, zeroline = FALSE, showline = FALSE, autotick = TRUE, ticks = '', showticklabels = FALSE),
yaxis = list(title = '', autorange = TRUE, showgrid = TRUE, zeroline = FALSE, showline = FALSE, autotick = TRUE, ticks = '', showticklabels = FALSE),
zaxis = list(title = '', autorange = TRUE, showgrid = FALSE, zeroline = FALSE, showline = FALSE, autotick = TRUE, ticks = '', showticklabels = FALSE)
)
)
# Output the plot
p1

How do I set the color of slices of a pie chart according to text?

I have a data representing the health of projects such as green, red, yellow, and none.
The problem is that color does not really match the texts. it matches when there are green, red, yellow exists in the data set. However, other cases, they don't match the text such as for red, it gets colored with yellow.
is there any way that I can fix the problem?
I appreciate for helps!
output$PieHealth <- renderPlotly({
Projects <- Projects[Projects$VP %in% c(input$checkbox),]
PieData2 <- Projects[,c(1,4)]
PieData2$ID <- 1
PD2 <- aggregate(cbind(PieData2$ID)~HealthName, data = PieData2, FUN=sum)
#PD2 <- PD2[-2,] #Delete 'None' Value
plot_ly(PD2, labels = ~PD2$HealthName, values = ~PD2$V1, type = 'pie',
textposition = 'inside',
textinfo = 'percent',
insidetextfont = list(color = '#FFFFFF'),
hoverinfo = 'text',
text = ~paste(PD2$HealthName),
marker = list(colors = c('#229954', '#ffc107','#d32f2f'),
line = list(color = '#FFFFFF', width = 1))) %>%
layout(title = "Project Health",
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
showlegend = FALSE)
})

Resources