Plotly: Bar and pie charts side by side - r

I would like to plot a bar and a pie chart side by side using the subplot function in the plotly package in R. However, a big pie chart is plotted in the center of the graph, overlaying the bar plot.
Here is a sample code:
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
bar <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar') %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
pie <- plot_ly(data, labels = ~Animals, values = ~LA_Zoo, type = 'pie', hole = 0.6)
subplot(bar, pie)
How can I fix it?

Let's try this -
library(plotly)
#sample data
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
#plot
plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar') %>%
layout(yaxis = list(title = 'Count'), xaxis = list(domain = c(0, 0.5)), barmode = 'stack') %>%
add_trace(data, labels = ~Animals, values = ~LA_Zoo, type = 'pie', hole = 0.6,
domain = list(x = c(0.5, 1)))

Related

Adding footnote in Plotly

I’m trying to add an asterisk footnote to a chart plotted with Plotly. This is my code:
library(plotly)
fig <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(20, 14, 23),
name = "SF Zoo",
type = "bar"
)
fig
Now I want to put an asterisk on "giraffes*" and after that give some additional explanation below this bar plot, let’s say: Note: * The tallest animal.
You can use layout(annotations) to add "caption" to Plotly.
library(plotly)
plot_ly(
x = c("giraffes*", "orangutans", "monkeys"),
y = c(20, 14, 23),
name = "SF Zoo",
type = "bar"
) |>
layout(annotations =
list(x = 0, y = -0.1,
text = "Note: * The tallest animal.",
showarrow = F,
xref='paper',
yref='paper')
)

Plotly: Different setups for two plots in one frame

library(dplyr)
library(plotly)
library(plyr)
df <- data.frame(
Category = c('foo', 'bar', 'bar', 'foo'),
x = c(2.1, 3.4, 4, 4),
y = c(16, 21, 10, 17)
)
palette <- c("green", "red")
fig <- plot_ly(
data = df, x = ~x, y = ~y, size = 10, color = ~Category, colors = palette,
symbol = ~Category, hoverinfo = 'y', symbols = c(15, 17)
)
fig
That's what I have so far. What I want to do is to make the foo plot, and only the foo plot, visible in the legend. I've tried using add_trace() with showlegend = F, but that sets bar invisible in the legend as well.
Isn't it better to use ggplot2?
library(tidyverse)
df <- tibble(
Category = c('foo', 'bar', 'bar', 'foo'),
x = c(2.1, 3.4, 4, 4),
y = c(16, 21, 10, 17)
)
df %>% ggplot(aes(x, y))+
geom_point(data = df %>% filter(Category=='foo'), aes(x, y, col=Category, shape=Category))+
geom_point(data = df %>% filter(Category=='bar'), aes(x, y), col="green", shape=1)

Plotly: different symbols on scatterplots

library(dplyr)
library(plotly)
library(plyr)
df <- data.frame(
Category = c('foo', 'bar', 'bar', 'foo'),
x = c(2.1, 3.4, 4, 4),
y = c(16, 21, 10, 17)
)
palette <- c("#007700", "#cc0000")
fig <- plot_ly(data = df, x = ~x, y = ~y, size = 10, color = ~Category,
colors = palette)
fig
How would I get the foo points to plot as squares and the bar points as triangles?
Add symbol=~Category and define symbols=:
plotly::plot_ly(
data = df, x = ~x, y = ~y, size = 10, color = ~Category, colors = palette,
symbol = ~Category, symbols = c(15, 17)
)

Display values with NAs in xaxis of plotly chart

How can I display in plotly bar chart a x value with NAs in y column?
library(plotly)
fig <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(20, 14, NA),
name = "SF Zoo",
type = "bar"
)
fig

Add title to each y-axis with Plotly subplots

Let's consider the following plot:
p1 <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(20, 14, 23),
name = "Size",
type = "bar")
p2 <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(10, 2, 3),
name = "Weight",
type = "bar"
)
p <- subplot(p1, p2, nrows = 2, shareX = TRUE)
p %>% layout(yaxis = list(title = "Size"))
I would like to control the y-axis title of each subplot. It looks like the way I am doing it I only manage to control the top subplot. How can I also add a title for the y-axis of the bottom subplot?
titleY = TRUE will make the difference, such that:
p1 <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(20, 14, 23),
name = "Size",
type = "bar")%>%layout(yaxis = list(title = c("Size1")))
p2 <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(10, 2, 3),
name = "Weight",
type = "bar"
)%>%layout(yaxis = list(title = c("Size2")))
p <- subplot(p1, p2, nrows = 2, shareX = TRUE, titleY= TRUE)
p

Resources