r - Names Argument Plotly Barplot -
so using plotly in shiny create bar chart. x labels not unique example (dog twice):
let df be:
x y 1 dog 2 cat 3 dog 3 ant 4 bee
i rows plotted won't work because of repeated variable dog in x column:
p = plot_ly(df, x = df[,2], y = df[,1], type = "bar", marker = list(color = torgb("blue"))) p
so thinking of getting past doing like: able plot 5 rows removing x argument. want able modify these names separately fill in x column variables.
p = plot_ly(df, y = x, type = "bar", marker = list(color = torgb("blue"))) p
how manually change x labels in plotly barplot?
thank you
straightforward official method:
use tickmode
, tickvals
, , ticktext
arguments in layout
xaxis
:
library(plotly) plot_ly(df, y = x, type = "bar", marker = list(color = torgb("blue"))) %>% layout(xaxis = list(tickmode = "array", tickvals = 0:4, ticktext = c("dog", "cat", "dog", "ant", "bee")))
i not sure how y = x
in above command generalize in other kinds of plots. hence, recommend approach:
# change y labels (i assigned "a", "b" etc.) df$y <- letters[1:nrow(df)] df %>% plot_ly(x = y, y = x, type = "bar", marker = list(color = torgb("blue"))) %>% layout(xaxis = list(tickmode = "array", tickvals = c("a", "b", "c", "d", "e"), ticktext = c("dog", "cat", "dog", "ant", "bee")))
short hack:
just add whitespace @ end of second occurrence of dog:
df$y <- as.character(df$y) df[3,2] <- "dog "
you can use first plot command:
plot_ly(df, x = df[,2], y = df[,1], type = "bar", marker = list(color = torgb("blue")))
i checked plots both approaches, , same. obviously, real application might not simple , hack won't feasible.
plus, can use ggplot2
, make plots interactive plotly::ggplotly
Comments
Post a Comment