1

I am trying to make a barplot in the Plotly package in R. By default Plotly stacks the values belonging to the same categories. I would like to plot the values sepeartely even if they belong to the same category. Following is an example:

value<-c(1,2,3,4,5)
Feature_Names <- c("A","B","C","D","C")
df1 <- data.frame(Feature_Names, value)
p7<-plot_ly(df1,x=~Feature_Names,y=~value,type='bar') %>%
layout(title= "Feature and values", xaxis= list(title='Feature_Names'))
show(p7)

The output of the above code is : enter image description here

I would like a way to display the values of Feature Name "C" separately as two individual bars. How do I achieve that? Thanks.

Aditya Lahiri
  • 409
  • 3
  • 11

1 Answers1

1

You can create a group column to distinguish between number of values for each Feature_Names.

df1 <- transform(df1, group = ave(Feature_Names, Feature_Names, FUN = seq_along))

Here are two suggestions that you can use after creating group columns

  1. Using ggplot :
library(ggplot2)
library(plotly)

ggplotly(ggplot(df1, aes(Feature_Names, value, fill = group)) + 
         geom_col(position = 'dodge'))

enter image description here

  1. Using plotly :

For this option you can get the data in wide format and then plot.

df2 <- tidyr::pivot_wider(df1, names_from = group, values_from = value)

plot_ly(df2,x=~Feature_Names,y=~`1`,type='bar', name = 'group 1') %>%
  add_trace(y = ~`2`, name = 'group 2') %>%
  layout(title= "Feature and values", 
         xaxis= list(title='Feature_Names'), yaxis = list(title = 'value'))

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 1
    Thank you for your efforts. I tried replicating your first method can got the following error message: `Error: Must request at least one colour from a hue palette.` For the second method, I got the following error message: `Error in eval(expr, data, expr_env) : object '1' not found` I followed the method suggested [here](https://stackoverflow.com/questions/55142313/plot-a-barplot-with-repeated-labels) to solve my issue for the time being, but a solution along the lines of what you have done above will be ideal: – Aditya Lahiri Jan 25 '21 at 19:37
  • @AdityaLahiri The code works fine for me without any error for the data you have shared. You might be on older version of R (R < 4.0.0) which is causing the error because your columns are of type factor. You could fix it by specifying `stringsAsFactors = FALSE` in `data.frame` call i.e `df1 <- data.frame(Feature_Names, value, stringsAsFactors = FALSE)` – Ronak Shah Jan 26 '21 at 00:22
  • Yes, I am using R 3.6.3, you are right! I implemented the changes to the code as you suggested. With method 1 I am still getting an error: `Error in FUN(X[[i]], ...) : object 'group' not found` With method 2, upon running the first line I get the following error: `Error: Can't subset columns that don't exist. x The column `group` doesn't exist.` I was able to solve my issue using the method suggested [here](https://stackoverflow.com/questions/55142313/plot-a-barplot-with-repeated-labels) – Aditya Lahiri Jan 28 '21 at 23:27
  • 1
    @AdityaLahiri I guess you did not ran the first line in my answer to create column `group`. `df1 <- transform(df1, group = ave(Feature_Names, Feature_Names, FUN = seq_along))`. – Ronak Shah Jan 28 '21 at 23:39
  • Perfect ! Thanks for solving it! My bad missing out on the line. – Aditya Lahiri Jan 28 '21 at 23:43