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
Related
I have the stacked bar below in which everything works fine except from the values in the axes and the hovertext which seem to have been multiplied by 100. I tried to to trick it by dividing my initial values with 100 but then I was taking rounded values back (20 instead of 19.8). I think that happened after I added tickformat=%
# data
Category<-c("First dose","Full vaccination")
`Uptake first dose`<-c(19.8,0)
`Uptake full vaccination`<-c(0,7.6)
`Not vaccinated`<-c(80.2,92.4)
ch5<-data.frame(Category,`Uptake first dose`,`Uptake full vaccination`,`Not vaccinated`)
# transform data
data.long <- ch5 %>%
pivot_longer(cols = -Category,
names_to = "vac",
values_to = "percent") %>%
mutate(vac = str_replace_all(vac, "\\.", " "),
vac = fct_rev(factor(vac)))
# add plot
plot_ly(data.long) %>%
add_bars(y = ~Category,
x = ~percent,
color = ~vac,
text = ~vac,
colors = c("#458d35", "#63bb47", "#e6e7e8"),
hovertemplate = paste('<b>%{y}</b>',
'<br>%{text}: %{x} ',
'<extra></extra>')) %>%
layout(font = list(color = '#a2a2a2'),barmode = "stack",
bargap = 0.7,
yaxis = list(fixedrange = TRUE,autorange="reversed",
title = "",
showticklabels = FALSE,
showgrid = FALSE,
showline = FALSE,
zeroline = FALSE),
xaxis = list(fixedrange = TRUE,title = "",
tickformat = "%",
zeroline = FALSE,
showgrid = FALSE),
hoverlabel = list(bgcolor = "black",
bordercolor = "black",
font = list(color = "white")),
shapes = list(type = "line",
y0 = 0, y1 = 1, yref = "paper",
x0 = 70, x1 = 70),
annotations = list(text = "Target (70%)",
showarrow = FALSE,
x = 70,
y = 1.05,
yref = "paper"),
legend = list(orientation = 'h'))
Use ticksuffix = '%'.
library(plotly)
plot_ly(data.long) %>%
add_bars(y = ~Category,
x = ~percent,
color = ~vac,
text = ~vac,
colors = c("#458d35", "#63bb47", "#e6e7e8"),
hovertemplate = paste('<b>%{y}</b>',
'<br>%{text}: %{x} ',
'<extra></extra>')) %>%
layout(font = list(color = '#a2a2a2'),barmode = "stack",
bargap = 0.7,
yaxis = list(fixedrange = TRUE,autorange="reversed",
title = "",
showticklabels = FALSE,
showgrid = FALSE,
showline = FALSE,
zeroline = FALSE),
xaxis = list(fixedrange = TRUE,title = "",ticksuffix = '%',
zeroline = FALSE,
showgrid = FALSE),
hoverlabel = list(bgcolor = "black",
bordercolor = "black",
font = list(color = "white")),
shapes = list(type = "line",
y0 = 0, y1 = 1, yref = "paper",
x0 = 70, x1 = 70),
annotations = list(text = "Target (70%)",
showarrow = FALSE,
x = 70,
y = 1.05,
yref = "paper"),
legend = list(orientation = 'h'))
I hope I explain it correctly but I have 2 graphs in 1 plot (so 2 y-axes). I would like to have y-axis2 lowered, so thats it not plotted in the whole plot (so I guess have variable values for the same tick distances). How is this possible?
df <- data.frame(x = 1:50,
y1 = c(20:5,6:10,9:3,4:20, 19:15),
y2 = c(100:115,200:204,50:44,4:20, 66:62))
plot_ly(df) %>%
add_lines(type = 'scatter', mode = "lines",
name = "test",
x = ~x, y = ~y1,
line = list(color = '#999999'),
hoverinfo = "text",
text = ~paste(round(y1, 1), x)) %>%
add_lines(type = 'scatter', mode = "lines",
name = "test", yaxis = 'y2',
x = ~x, y = ~y2,
line = list(color = '#CC79A7'),
hoverinfo = "text",
text = ~paste(round(y2, 1), x)) %>%
#layout
layout(title = "test",
xaxis = list(titel = "Date",
rangeslider = list(thickness = 0.05)),
yaxis = list(side = 'left', title = 'test',
showgrid = F, zeroline = F, type = "log",
showline = T),
yaxis2 = list(side = 'right', overlaying = "y",
title = 'test',
showgrid = F, zeroline = F,
showline = T))
You can expand the axis with range and therefore lower the 200 position. And if you like to hide the values over 200, you may use ticktext and tickvals for showing just the values between 0 and 200.
plot_ly(df) %>%
add_lines(type = 'scatter', mode = "lines",
name = "test",
x = ~x, y = ~y1,
line = list(color = '#999999'),
hoverinfo = "text",
text = ~paste(round(y1, 1), x)) %>%
add_lines(type = 'scatter', mode = "lines",
name = "test", yaxis = 'y2',
x = ~x, y = ~y2,
line = list(color = '#CC79A7'),
hoverinfo = "text",
text = ~paste(round(y2, 1), x)) %>%
#layout
layout(title = "test",
xaxis = list(titel = "Date",
rangeslider = list(thickness = 0.05)),
yaxis = list(side = 'left', title = 'test',
showgrid = F, zeroline = F, type = "log",
showline = T),
yaxis2 = list(side = 'right', overlaying = "y",
title = 'test',
showgrid = F, zeroline = F,
showline = T,
# added code
range = list(0, 700),
tickvals = seq(0, 200, 50),
ticktext = seq(0, 200, 50)))
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)
)
I have created donut charts from plotly from the following:
library(plotly)
library(RColorBrewer)
test<-data_frame(Score=c("Green","Green","Yellow","Yellow","Clear","Clear","Red","Red"),Lang=c(rep("Eng",4),rep("Esp",4)))
test1<-data_frame(Score=c("Green","Yellow","Yellow","Yellow","Clear","Clear","Red","Red"),Lang=c(rep("Eng",4),rep("Esp",4)))
color_order<-c("Green","Clear","Yellow","Red")
colors<-c("#31a354","#bdbdbd","#fec44f","#de2d26")
a<-test %>%
mutate(Score=factor(Score,levels=color_order))%>%
arrange(Score)%>%
group_by(Score)%>%
summarize(count = n()) %>%
plot_ly(labels = ~Score,
values = ~count,
hoverinfo="skip",
text = ~count,
marker = list(colors = colors),
legendgroup = ~Score) %>%
add_pie(hole = 0.6) %>%
layout(title = "test chart1", showlegend = TRUE,
font=list(family="sans serif",color="#000"),
plot_bgcolor="#f0f0f0",
legend = list(orientation = 'h',font=list(size=28)),
xaxis = list(title=paste0("Total: ",nrow(test)), showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
b<-test1 %>%
mutate(Score=factor(Score,levels=color_order))%>%
arrange(Score)%>%
group_by(Score)%>%
summarize(count = n()) %>%
plot_ly(labels = ~Score,
values = ~count,
hoverinfo="skip",
text = ~count,
marker = list(colors = colors),
legendgroup = ~Score) %>%
add_pie(hole = 0.6) %>%
layout(title = "test chart2", showlegend = FALSE,
font=list(family="sans serif",color="#000"),
plot_bgcolor="#f0f0f0",
legend = list(orientation = 'h',font=list(size=28)),
xaxis = list(title=paste0("Total: ",nrow(test1)),showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
I am attempting to put them side by side with the common legend from plot a, and I run the following:
subplot(a,b,nrows = 1)
However, I only see one plot which appears to be a combination of the two. I also tried the approach here: Plotly: Bar and pie charts side by side, but it just gave me inception style donut within a donut. How can I put them next to each other with the common legend? Thanks.
According to this (https://plot.ly/r/pie-charts/), in order to create pie chart subplots, you need to use the domain attribute. You could try something like this (adjust domain for your needs):
a <- test %>%
mutate(Score=factor(Score,levels=color_order))%>%
arrange(Score)%>%
group_by(Score)%>%
summarize(count = n())
b<-test1 %>%
mutate(Score=factor(Score,levels=color_order))%>%
arrange(Score)%>%
group_by(Score)%>%
summarize(count = n())
p <- plot_ly() %>%
add_pie(data = a, labels = ~Score, values = ~count, hole = 0.6,
name = "a", domain = list(x = c(0, 0.4), y = c(0.4, 1))) %>%
add_pie(data = b, labels = ~Score, values = ~count, hole = 0.6,
name = "b", domain = list(x = c(0.6, 1), y = c(0.4, 1))) %>%
layout(title = "test chart1", showlegend = TRUE,
font=list(family="sans serif",color="#000"),
plot_bgcolor="#f0f0f0",
legend = list(orientation = 'h',font=list(size=28)),
xaxis = list(title=paste0("Total: ",nrow(test)), showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
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"))
)